How to convert each equation calculations into FOR Loop form

8 views (last 30 days)
This is the code for each calculations (Normal form)
p = linspace(1,5);
AdK2 = 1.4;
Eff_Ratio_17 = 1-(p.^AdK2-1)./(AdK2*(p-1)*(17^(AdK2-1)))
Eff_Ratio_19 = 1-(p.^AdK2-1)./(AdK2*(p-1)*(19^(AdK2-1)))
Eff_Ratio_21 = 1-(p.^AdK2-1)./(AdK2*(p-1)*(21^(AdK2-1)))
Eff_Ratio_23 = 1-(p.^AdK2-1)./(AdK2*(p-1)*(23^(AdK2-1)))
Eff_Ratio_25 = 1-(p.^AdK2-1)./(AdK2*(p-1)*(25^(AdK2-1)))
plot(p,Eff_Ratio_17,p,Eff_Ratio_19,p,Eff_Ratio_21,p,Eff_Ratio_23,p,Eff_Ratio_25)
ylabel('Thermal Efficiency');
xlabel('Injection Ratio');
Figure 1
I tried to convert into a for loop form to simplify the structure but I keep getting wrong graph and the equation calculations seems off
p = linspace(1,5);
AdK2 = 1.4;
for R = 17:2:25
Eff_Ratio = 1-((p.^(AdK2-1))./(AdK2*(R^(AdK2-1)*(p-1))));
plot(p,Eff_Ratio)
end
ylabel('Thermal Efficiency');
xlabel('Injection Ratio');
Figure 2
Can someone point out what's wrong with my code arrangment ?
The graph that I want to get is Figure 1.**

Accepted Answer

David Hill
David Hill on 6 Feb 2022
Edited: David Hill on 6 Feb 2022
No loop needed.
p = linspace(1.001,5);
AdK2 = 1.4;
Eff_Ratio = 1-(p.^AdK2-1)./(AdK2*(p-1))./((17:2:25).^(AdK2-1))';
plot(p,Eff_Ratio)
ylabel('Thermal Efficiency');
xlabel('Injection Ratio');
  2 Comments
Yeong Haoyee
Yeong Haoyee on 6 Feb 2022
Edited: Yeong Haoyee on 6 Feb 2022
Thanks a lot ! I follow your advice and it works! I realized my mistake when trying to do the code the equation.
p = linspace(1.001,5);
AdK2 = 1.4;
Eff_Ratio = 1-(p.^AdK2-1)./(AdK2*(p-1))./((17:2:25).^(AdK2-1))';
plot(p,Eff_Ratio)
ylabel('Thermal Efficiency');
xlabel('Injection Ratio');
Instead of using the semilogx, I change it to plot to get the figure 1 picture(Just editted the questions to show the graph I'm trying to get)
But I have a question. Why do you use 1.001 instead of just 1 ?

Sign in to comment.

More Answers (2)

Davide Masiello
Davide Masiello on 6 Feb 2022
You need a 'hold on' in order to keep previous lines in the plot at each iteration.
But you can do even better in this way
p = linspace(1,5);
AdK2 = 1.4;
R = 17:2:25;
Eff_Ratio = zeros(length(R),length(p));
for idx = 1 : length(R)
Eff_Ratio(idx,:) = 1-((p.^(AdK2-1))./(AdK2*(R(idx)^(AdK2-1)*(p-1))));
end
figure
semilogx(p,Eff_Ratio)
ylabel('Thermal Efficiency');
xlabel('Injection Ratio');

Voss
Voss on 6 Feb 2022
Eff_Ratio_17 = 1-(p.^AdK2-1)./(AdK2*(p-1)*(17^(AdK2-1)))
Eff_Ratio_19 = 1-(p.^AdK2-1)./(AdK2*(p-1)*(19^(AdK2-1)))
Eff_Ratio_21 = 1-(p.^AdK2-1)./(AdK2*(p-1)*(21^(AdK2-1)))
Eff_Ratio_23 = 1-(p.^AdK2-1)./(AdK2*(p-1)*(23^(AdK2-1)))
Eff_Ratio_25 = 1-(p.^AdK2-1)./(AdK2*(p-1)*(25^(AdK2-1)))
becomes:
for R = 17:2:25
Eff_Ratio = 1-(p.^AdK2-1)./(AdK2*(p-1)*(R^(AdK2-1)))
end
(the only change is the substitution of R in place of {17, 19, ...})
In making this change, you have introduced another set of parentheses, which changes the expression:
Eff_Ratio = 1-((p.^(AdK2-1))./(AdK2*(R^(AdK2-1)*(p-1))));
Notice: p.^(AdK2-1) vs p.^AdK2-1

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!