MATLAB: Calling in a function inside a for loop and storing each answer separetly

for loopfunctionstoring answer

Hello, this is my code so far. My function outputs the number of iterations needed to solve a system, and I want to use this script to output the number of iterations but with varying a parameter as shown:
A = [-10 4 1 0; 4 -10 0 1; 1 0 -10 4; 0 1 4 -10];
B = [-200;-80;-300;-180];
n = 4;
x = [0;0;0;0];
imax = 1000;
es = 1e-6;
w= 1.2;
for w=0.1: 0.1: 1.9
engr3202_gsrelax(A,B,n,x,imax,es, w);
end
I keep getting large numbers of iterations so i'm not sure if the code is just adding them all together, but i basically want 19 outputs separately so i can plot a graph with it.
Thanks a lot!

Best Answer

A = [-10 4 1 0; 4 -10 0 1; 1 0 -10 4; 0 1 4 -10];
B = [-200;-80;-300;-180];
n = 4;
x = [0;0;0;0];
imax = 1000;
es = 1e-6;
wvals = 0.1: 0.1: 1.9;
for widx = 1 : length(wvals)
w = wvals(widx);
gs(widx,:) = engr3202_gsrelax(A,B,n,x,imax,es, w);
end
plot(wvals, gs)