Question: Translate this MATLAB code to Excel VBA if app.DieselButton.Value = = true if string ( app . ThrottleTypeSwitch.Value ) = = WOT if string

Translate this MATLAB code to Excel VBA 
 

if app.DieselButton.Value == true
if string(app.ThrottleTypeSwitch.Value)== "WOT"
if string(app.StrokeSwitch.Value)=="2"
if app.NaturallyAspiratedButton.Value == true
%--------DIESEL WOTHROTTLE 2STROKE NATURALLYASPIRATED
nst=1;
Vd=(en_vol/noc)*(1/1000); %displacement volume in m^3
Vc = Vd/(rc-1); %clearence volume in m^3
S=((4*Vd)/((bts^2)*(pi)))^(1/3); %stroke length in m
B=bts*S; %bore length in m
V1= Vd + Vc; %v1 in m^3
V2= Vc; %v2 in m^3
P2= P1*rc^k; %p2 in kPa
T2= T1*rc^(k-1); %t2 in K
m=(P1*V1)/(R*T1); %m in kg
ma=(af/(af+1))*(1-(exr/100))*m; %mass of air in kg
mf=(1/(af+1))*(1-(exr/100))*m; %mass of fuel in kg
mex=(exr/100)*m; %mass of exhaust in kg
T3=((mf*hv*(nc/100))/(m*cv))+T2; %t3 in K
P3=P2; %p3 in kPa
V3=V2*(T3/T2);
V4=V1;
T4=T3*((V3/V4)^(k-1)); %t4 in K
P4=P3*((V3/V4)^k); %p4 in kPa
T_1=linspace(T1,T2,1000);
T_2=linspace(T3,T4,1000);
Ap=(pi*(B^2))/4;
p=1.225;
V_12=linspace(V1,V2,1000);
V_34=linspace(V3,V4,1000);
P_12=P1.*(V1./V_12).^k;
6
P_34=P3.*(V3./V_34).^k;
Qin=mf*hv*(nc/100);
Qout=m*cv*(T1-T4);
W12=(m*R*(T2-T1))/(1-k);
W34=(m*R*(T4-T3))/(1-k);
Wnet=W12+W34;
nt=(1-(T1/T2))*100;
imep=Wnet/(V1-V2);
Wdoti=((Wnet*(rpm/60))/nst)*noc;
Up_avg=2*S*(rpm/60);
Wdotb=(nm/100)*Wdoti;
Wi=(Wdoti*nst)/(rpm/60);
Wb=Wi*(nm/100);
Wf=Wi-Wb;
Wdotf=Wdoti-Wdotb;
bmep=(nm/100)*imep;
Torque=((bmep*(en_vol/1000))/(2*nst*pi))*1000;
BSP=(Wdotb/Ap)*noc;
OPD=(Wdotb/en_vol)*noc;
bsfc=((mf*(rpm/60)*0.5)/Wdotb)/noc;
nv=(ma/(p*Vd))*100;
V5=V1;
V6=V2;
W61=P1*(V1-V6);
W56=P1*(V6-V5);
T=[T1 T2 T3 T4 T1];
P=[P1 P2 P3 P4 P1];
V=[V1 V2 V3 V4 V1 V5 V6 V1];
TT=[T2 T3];
V23=[V2 V3];
P23=[P2 P3];
TT1=[T4 T1];
V41=[V4 V1];
P41=[P4 P1];
Bore =0.01*B;
Stroke =0.01*S;
a = Stroke/2 ;
r = con_rod*0.01 ;
r2= r/a;
figure(1)
semilogy([V1 V2],[P1 P1],'c',V_12,P_12,'b',V23,P23,'r',V_34,P_34,'g',V41,P41,'y')
7
ylim([1010^4])
xlabel('Volume [m^3]')
ylabel('Pressure [Pa]')
tetha2a=linspace(0,720,5);
figure(2)
plot(tetha2a,P)
title('Crank angle vs pressure')
xlabel('Crank Angle')
ylabel('pressure')
tetha2b=linspace(0,720,8);
figure(3)
plot(tetha2b,V)
title('Crank angle vs Volume')
xlabel('Crank Angle')
ylabel('Volume')
U=ones(1,720);
H=ones(1,720);
tetha=(1:1:720);
for tetha2=1:1:720
pistonspeed =(pi/2)*sind(tetha2)*(1+(cosd(tetha2)/sqrt(r2^2-(sind(tetha2)).^2)))*Up_avg;
pistonposition = a.*cosd(tetha2)+ sqrt(r^2-(a^2).*sind(tetha2).^2);
U(tetha2)= pistonspeed;
H(tetha2)= pistonposition;
end
figure(4)
plot(tetha,U)
title('Crank angle vs Piston speed')
xlabel('Crank Angle')
ylabel('Piston speed')
figure(5)
plot(tetha,H)
title('Crank angle vs Piston Position')
xlabel('Crank Angle')
ylabel('Piston Position')
app.OutputValuesTextArea.Value=sprintf([' T1=%.0f K
T2=%.0f K
T3=%.0f K
T4=%.0f K

'...
' P1=%.0f kPa
P2=%.0f kPa
P3=%.0f kPa
P4=%.0f kPa

'...
' V1=%.7f m3
V2=%.7f m3
V3=%.7f m3
V4=%

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

Here is the equivalent Excel VBA code vba Sub CalculateEnginePerformance Dim app As Object Set app CreateObjectappapplication If appDieselButtonValue True Then If appThrottleTypeSwitchValue WOT Then I... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Mechanical Engineering Questions!