i want to put n and counter in one matrix

조회 수: 3 (최근 30일)
yonatan friedman
yonatan friedman 2020년 12월 12일
답변: Abhishek Gupta 2020년 12월 15일
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