Hi everybody. I’m kind of new to MATLAb and looking to get more efficient in my coding. This feels clunky to me and I feel like I could definitely shorten it. Any ideas?
Engine = ["LO2/LH2";"LO2/CH4";"STP low ISP";"STP high ISP"];
ISP = [450;312;250;500];
DryMass = [3;6;6;12];
MaxPropellantMass = [50;50;50;50];
g = 9.81;
ve = (g*ISP)/1000;
mi = DryMass + MaxPropellantMass;
mf = DryMass;
DeltaV = zeros(1,4);
mf_DV2 = zeros(4,1);
Prop_Delivered = zeros(1,4);
DV2 = 2;
Pd_LOx_LH2 = zeros(1,8);
Pd_LOx_CH4 = zeros(1,8);
Pd_STP_H = zeros(1,8);
Pd_STP_L = zeros(1,8);
for f = 1:4
DeltaV(f) = ve(f,1)*log(mi(f,1)/mf(f,1));
mf_DV2(f) = mi(f,1)/(exp(DV2/(ve(f,1))));
Prop_Delivered(f) = mf_DV2(f,1) – DryMass(f,1);
end
DeltaV = DeltaV’;
Problem_1 = table(Engine,ISP,DryMass,MaxPropellantMass,DeltaV);
Problem_1 = renamevars(Problem_1,["DryMass","MaxPropellantMass","DeltaV"], …
["Dry Mass (mT)","Max Propellant Mass (mT)","DeltaV (km/s)"])
Prop_Delivered = Prop_Delivered’;
Problem_2 = table(Engine,mf_DV2,Prop_Delivered);
Problem_2 = renamevars(Problem_2,["mf_DV2","Prop_Delivered"], …
["Final Mass (mT)","Delivered Propellant (mT)"])
for dv = 1:8
Pd_LOx_LH2(dv) = (mi(1,1)/exp(dv/ve(1,1)))-DryMass(1,1);
Pd_LOx_CH4(dv) = (mi(2,1)/exp(dv/ve(2,1)))-DryMass(2,1);
Pd_STP_L(dv) = (mi(3,1)/exp(dv/ve(3,1)))-DryMass(3,1);
Pd_STP_H(dv) = (mi(4,1)/exp(dv/ve(4,1)))-DryMass(4,1);
end
x = 1:0.5:8;
LOx_LH2 = (mi(1)./exp(x/ve(1)))-DryMass(1);
LOx_CH4 = (mi(2)./exp(x/ve(2)))-DryMass(2);
STP_L = (mi(3)./exp(x/ve(3)))-DryMass(3);
STP_H = (mi(4)./exp(x/ve(4)))-DryMass(4);
plot(x,LOx_LH2,x,LOx_CH4,x,STP_L,x,STP_H)
% Add title and axis labels
title(‘Propellant Delivered (mT) Per DeltaV (km/s)’);
xlabel(‘DeltaV’);
ylabel(‘Propellant Delivered’);
% Add legend
legend(‘LOx/LH2′,’LOx/CH4′,’STP Low ISP’,’STP High ISP’);
Problem_3 = table((1:8)’,Pd_LOx_LH2′,Pd_LOx_CH4′,Pd_STP_L’,Pd_STP_H’);
Problem_3 = renamevars(Problem_3,["Var1","Var2","Var3","Var4","Var5"], …
["DeltaV","LOx/LH2","LOx/CH4","STP Low ISP","STP High ISP"])Engine = ["LO2/LH2";"LO2/CH4";"STP low ISP";"STP high ISP"];
ISP = [450;312;250;500];
DryMass = [3;6;6;12];
MaxPropellantMass = [50;50;50;50];
g = 9.81;
ve = (g*ISP)/1000;
mi = DryMass + MaxPropellantMass;
mf = DryMass;
DeltaV = zeros(1,4);
mf_DV2 = zeros(4,1);
Prop_Delivered = zeros(1,4);
DV2 = 2;
Pd_LOx_LH2 = zeros(1,8);
Pd_LOx_CH4 = zeros(1,8);
Pd_STP_H = zeros(1,8);
Pd_STP_L = zeros(1,8);
for f = 1:4
DeltaV(f) = ve(f,1)*log(mi(f,1)/mf(f,1));
mf_DV2(f) = mi(f,1)/(exp(DV2/(ve(f,1))));
Prop_Delivered(f) = mf_DV2(f,1) – DryMass(f,1);
end
DeltaV = DeltaV’;
Problem_1 = table(Engine,ISP,DryMass,MaxPropellantMass,DeltaV);
Problem_1 = renamevars(Problem_1,["DryMass","MaxPropellantMass","DeltaV"], …
["Dry Mass (mT)","Max Propellant Mass (mT)","DeltaV (km/s)"])
Prop_Delivered = Prop_Delivered’;
Problem_2 = table(Engine,mf_DV2,Prop_Delivered);
Problem_2 = renamevars(Problem_2,["mf_DV2","Prop_Delivered"], …
["Final Mass (mT)","Delivered Propellant (mT)"])
for dv = 1:8
Pd_LOx_LH2(dv) = (mi(1,1)/exp(dv/ve(1,1)))-DryMass(1,1);
Pd_LOx_CH4(dv) = (mi(2,1)/exp(dv/ve(2,1)))-DryMass(2,1);
Pd_STP_L(dv) = (mi(3,1)/exp(dv/ve(3,1)))-DryMass(3,1);
Pd_STP_H(dv) = (mi(4,1)/exp(dv/ve(4,1)))-DryMass(4,1);
end
x = 1:0.5:8;
LOx_LH2 = (mi(1)./exp(x/ve(1)))-DryMass(1);
LOx_CH4 = (mi(2)./exp(x/ve(2)))-DryMass(2);
STP_L = (mi(3)./exp(x/ve(3)))-DryMass(3);
STP_H = (mi(4)./exp(x/ve(4)))-DryMass(4);
plot(x,LOx_LH2,x,LOx_CH4,x,STP_L,x,STP_H)
% Add title and axis labels
title(‘Propellant Delivered (mT) Per DeltaV (km/s)’);
xlabel(‘DeltaV’);
ylabel(‘Propellant Delivered’);
% Add legend
legend(‘LOx/LH2′,’LOx/CH4′,’STP Low ISP’,’STP High ISP’);
Problem_3 = table((1:8)’,Pd_LOx_LH2′,Pd_LOx_CH4′,Pd_STP_L’,Pd_STP_H’);
Problem_3 = renamevars(Problem_3,["Var1","Var2","Var3","Var4","Var5"], …
["DeltaV","LOx/LH2","LOx/CH4","STP Low ISP","STP High ISP"]) Engine = ["LO2/LH2";"LO2/CH4";"STP low ISP";"STP high ISP"];
ISP = [450;312;250;500];
DryMass = [3;6;6;12];
MaxPropellantMass = [50;50;50;50];
g = 9.81;
ve = (g*ISP)/1000;
mi = DryMass + MaxPropellantMass;
mf = DryMass;
DeltaV = zeros(1,4);
mf_DV2 = zeros(4,1);
Prop_Delivered = zeros(1,4);
DV2 = 2;
Pd_LOx_LH2 = zeros(1,8);
Pd_LOx_CH4 = zeros(1,8);
Pd_STP_H = zeros(1,8);
Pd_STP_L = zeros(1,8);
for f = 1:4
DeltaV(f) = ve(f,1)*log(mi(f,1)/mf(f,1));
mf_DV2(f) = mi(f,1)/(exp(DV2/(ve(f,1))));
Prop_Delivered(f) = mf_DV2(f,1) – DryMass(f,1);
end
DeltaV = DeltaV’;
Problem_1 = table(Engine,ISP,DryMass,MaxPropellantMass,DeltaV);
Problem_1 = renamevars(Problem_1,["DryMass","MaxPropellantMass","DeltaV"], …
["Dry Mass (mT)","Max Propellant Mass (mT)","DeltaV (km/s)"])
Prop_Delivered = Prop_Delivered’;
Problem_2 = table(Engine,mf_DV2,Prop_Delivered);
Problem_2 = renamevars(Problem_2,["mf_DV2","Prop_Delivered"], …
["Final Mass (mT)","Delivered Propellant (mT)"])
for dv = 1:8
Pd_LOx_LH2(dv) = (mi(1,1)/exp(dv/ve(1,1)))-DryMass(1,1);
Pd_LOx_CH4(dv) = (mi(2,1)/exp(dv/ve(2,1)))-DryMass(2,1);
Pd_STP_L(dv) = (mi(3,1)/exp(dv/ve(3,1)))-DryMass(3,1);
Pd_STP_H(dv) = (mi(4,1)/exp(dv/ve(4,1)))-DryMass(4,1);
end
x = 1:0.5:8;
LOx_LH2 = (mi(1)./exp(x/ve(1)))-DryMass(1);
LOx_CH4 = (mi(2)./exp(x/ve(2)))-DryMass(2);
STP_L = (mi(3)./exp(x/ve(3)))-DryMass(3);
STP_H = (mi(4)./exp(x/ve(4)))-DryMass(4);
plot(x,LOx_LH2,x,LOx_CH4,x,STP_L,x,STP_H)
% Add title and axis labels
title(‘Propellant Delivered (mT) Per DeltaV (km/s)’);
xlabel(‘DeltaV’);
ylabel(‘Propellant Delivered’);
% Add legend
legend(‘LOx/LH2′,’LOx/CH4′,’STP Low ISP’,’STP High ISP’);
Problem_3 = table((1:8)’,Pd_LOx_LH2′,Pd_LOx_CH4′,Pd_STP_L’,Pd_STP_H’);
Problem_3 = renamevars(Problem_3,["Var1","Var2","Var3","Var4","Var5"], …
["DeltaV","LOx/LH2","LOx/CH4","STP Low ISP","STP High ISP"]) shortening, efficiency, learning MATLAB Answers — New Questions