i want to put n and counter in one matrix

3 次查看(过去 30 天)
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

回答(1 个)

Abhishek Gupta
Abhishek Gupta 2020-12-15
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: -

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by