i want to put n and counter in one matrix

2 views (last 30 days)
for n=4:50
a=[];
e = ones(n,1);
A1 = spdiags([-e 0*e -e 4*e -e 0*e -e],-3:3,n,n);
full(A1);
A=full(A1);
b=zeros(n,1);
b(1)=1; b(end)=1;
x=zeros(n,1);
L=tril(A,-1);
U=triu(A,1);
D=diag(A);
B=-1./D.*(L+U);
c=(1./D).*b;
flag=0;
counter=0;
epsilon=10^(-3);
while flag==0
counter=counter+1;
if counter>100000
error('too much')
end
x_new=B*x+c;
if max(abs((x_new-x)./x_new))<epsilon
flag=1;
end
x=x_new;
end
fprintf('%g %g\n',counter,n)
end

Answers (1)

Abhishek Gupta
Abhishek Gupta on 15 Dec 2020
Hi,
As per my understanding, you want to create a matrix containing 'n' and 'counter' variables. One can do this task by appending a row, containing 'n' and 'counter' variables, at the end of the matrix on every iteration of the loop.
out = []; % output matrix
for n = 4:50
% Your Code Here %
out = [out; [n, counter]]; % append row containing n and counter
end
Also, referring you to the following link for more information: -

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!