plot multiple equations in a for loop

1 view (last 30 days)
Hi everyone. I am trying to plot multiple graph in Matlab. it seems that matlab over wrights the values in each loop so when i try to plot it doesn't give any results ....so how can i save the values for every loop to plot them ?? here is the code
tc=40;
te=15;
c1=137.402;
c2=4.60437;
c3=0.061652;
c4=-1.118157;
c5=-.001525;
c6=-.0109119;
c7=-.00040148;
c8=-.00026682;
c9=.000003873;
d1=1.00618;
d2=-.893222;
d3=-.01426;
d4=0.870024;
d5=-.0063397;
d6=.033889;
d7=-.00023875;
d8=-.00014746;
d9=.0000067962;
for ta=[20:5:50];
qe=c1+c2*te+c3*te^2+c4*tc+c5*tc^2+c6*te*tc+c7*te^2*tc+c8*te*tc^2+c9*te^2*tc^2;
p=d1+d2*te+d3*te^2+d4*tc+d5*tc^2+d6*te*tc+d7*te^2*tc+d8*te*tc^2+d9*te^2*tc^2;
a=1;
b=(0.54*10+6)/0.27;
c=10^2+(6*10)/0.27-qe/0.27;
te=(-b+sqrt(b^2-4*a*c))/(2*a);
qc=qe+p;
tc=(qc/9.6)+ta;
ta,qe,p,te,qc,tc,
end
plot(ta,qe,ta,p,ta,te,ta,qc,ta,tc)

Accepted Answer

Michael Haderlein
Michael Haderlein on 17 Apr 2015
Please, use the {} Code button above the field you're writing your question to make the post be readable.
Also, you should learn about the array concept of Matlab. Your equations look kind of like scalar products, so most likely you can significantly shorten your code and avoid typos this way.
With respect to your question, in this case it's easier to loop with a counter from 1:7, create the respective ta from this and instead of writing data to scalars, save them in arrays. Such as
ta=zeros(1,7);
qe=zeros(1,7);
for cnt=1:7
ta(cnt)=cnt*5+15;
qe(cnt)=...;
...
end
plot(ta,qe)
  3 Comments
Michael Haderlein
Michael Haderlein on 17 Apr 2015
Better than before, but simply using the {} Code button right above the edit control would even increase readability.
Could my answer solve your problem? It does not seem as if you had inserted my suggestion in this code.
majid eid
majid eid on 17 Apr 2015
yes thank you very much michael, it worked :)

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!