MATLAB: I want to save all the values of e1 and e2 and then I want to plot them

save variables from for loop in array

%clc
%close all
%clear
%Steel
n = 0.22;
r = 0.9;
for a = -1:1;
nr1 = (1+r*(1-a))*(1-(2*r*a/1+r)+a^2);
nr2 = ((1+r)*a-r)*(1-(2*r*a/1+r)+a^2);
dr = (1+r)*(1+a)*(1-((1+4*r+2*r^2)*a/(1+r)^2)+a^2);
e1 = (nr1/dr)*n;
e2 = (nr2/dr)*n;
end;
plot(e2,e1);

Best Answer

There are several ways to approach this.
This causes the least disruption to the code you posted:
n = 0.22;
r = 0.9;
av = linspace(-1, 1);
for k = 1:numel(av)
a = av(k);
nr1 = (1+r*(1-a))*(1-(2*r*a/1+r)+a^2);
nr2 = ((1+r)*a-r)*(1-(2*r*a/1+r)+a^2);
dr = (1+r)*(1+a)*(1-((1+4*r+2*r^2)*a/(1+r)^2)+a^2);
e1(k) = (nr1/dr)*n;
e2(k) = (nr2/dr)*n;
end
figure
plot(e2,e1)
grid
Experiment to get the result you want.