Loop not working? How can i save the total values wth out getting overwrite in the save file?

clear
a=input ('what is the a value?');
r=input ('what is the a value?');
em=input ('what is the em value?');
for i=250:1:1200
y=i;
if y<=500
e1=(2.987199*10^-13*y^6) -(7.960874*10^-10*y^5) +(8.519635*10^-7*y^4) -(4.712248*10^-4*y^3) +(1.425147*10^-1*y^2) -(2.239051*10*y)+(1.429313*10^3);
e2=(1.353837*10^-12*y^6)-(2.999955*10^-9*y^5)+(2.726629*10^-6*y^4)-(1.300071*10^-3*y^3)+(3.425863*10^-1*y^2)-(4.723584*10*y)+(2.664003*10^3);
else
e1=(1.862162*10^-16*y^6)-(1.286561*10^-12*y^5)+(3.440482*10^-9*y^4)-(4.480664*10^-6*y^3)+(2.912960*10^-3*y^2)-(9.423524*10^-1*y)+(1.217245*10^2);
e2=(1.656867*10^-17*y^6)-(1.420149*10^-13*y^5)+(4.945864*10^-10*y^4)-(8.912814*10^-7*y^3)+(8.801682*10^-4*y^2)-(4.439847*10^-1*y)+(8.958838*10);
end
E=(1+2*(a^3/r^3)*(e1-em)/(e1+2*em))^2;
d=[y e1 e2 E];
save eee.dat d /ascii
plot(y,E) end
>>>finally the looped values are restricted to the last loop only...how can i save all the values instead of single (end) value out of the loop????

Answers (1)

Dear Uday, you don't need loop in this case. You can do what you need directly as follows:
a=input ('what is the a value?');
r=input ('what is the r value?');
em=input ('what is the em value?');
y1 = 250:500;
e1(1:length(y1)) = 2.987199e-13 * y1.^6 -7.960874e-10 * y1.^5 +8.519635e-7 * y1.^4 - 4.712248e-4 * y1.^3 + 1.425147e-1 * y1.^2 - 2.239051e1 * y1 + 1.429313e3;
e2(1:length(y1)) = 1.353837e-12 * y1.^6 - 2.999955e-9 * y1.^5 + 2.726629e-6 * y1.^4 - 1.300071e-3 * y1.^3 + 3.425863e-1 * y1.^2 - 4.723584e1 * y1 + 2.664003e3;
y2 = 501:1200;
e1(length(y1) + 1: length(y1) + length(y2)) = 1.862162e-16 * y2.^6 - 1.286561e-12 * y2.^5 + 3.440482e-9 * y2.^4 - 4.480664e-6 * y2.^3 +...
2.912960e-3 * y2.^2 - 9.423524e-1 * y2 + 1.217245e2;
e2(length(y1) + 1: length(y1) + length(y2)) = 1.656867e-17 * y2.^6 - 1.420149e-13 * y2.^5 + 4.945864e-10 * y2.^4 - 8.912814e-7 * y2.^3 +...
8.801682e-4 * y2.^2 - 4.439847e-1 * y2 + 8.958838e1;
y = [y1 y2];
E = (1 + 2 * (a^3 / r^3) * (e1 - em) ./ (e1 + 2 * em)).^2;
d = [y' e1' e2' E'];
plot(y, E)
save eee.dat d /ascii
I hope it helps. Good luck!

This question is closed.

Asked:

on 22 Oct 2013

Closed:

on 20 Aug 2021

Community Treasure Hunt

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

Start Hunting!