I want to produce a code that compares both Cholesky in-built function with calculated values, but my current code is not returning to line 1. i can't figure out why.
this is my current code
A=input('Matrix= ');
[n,m]=size(A);
if m==n
if issymmetric(A,'nonskew')
if eig(A)>=0
fprintf('matlab inbuilt function "chol" is: ')
R=chol(A)
[n,m]=size(A);
R1=zeros(n,m);
h=1;
for j=1:n
if j==1
R1(j,1)=sqrt(A(j,1));
else
R1(j,1:h)=((A(j,1:h))/(R1(1:h,1:h))')';
sum=R1(j,1:h)*(R1(j,1:h))';
R1(j,j)=sqrt(A(j,j)-sum);
h=h+1;
end
end
else
fprintf('matrix does not have positive eigenvalues, ')
return
end
fprintf('Calculated value is: ')
Q=R1'
else
fprintf('the input is not a symmetrical matrix, try again')
return
end
else
fprintf('the format of input matrix is wrong')
return
end

댓글 수: 3

Komel Kaur
Komel Kaur 2020년 12월 19일
i want the code to return to line 1 if the input values is not m=n, is not symmetric and does not have eigenvalues>=0
VBBV
VBBV 2020년 12월 19일
Komel Kaur
Komel Kaur 2020년 12월 19일
noted with thanks. this helped me understand 'return' better

댓글을 달려면 로그인하십시오.

 채택된 답변

Les Beckham
Les Beckham 2020년 12월 19일
편집: Les Beckham 2020년 12월 19일

0 개 추천

return does not mean 'restart'. return in the context of a script actually means 'abort' or 'return to the command line and stop executing this script'.
If you wish this code to repeat until a valid matrix is entered, wrap it in a while(true) loop and replace all of your return statments with continue (which will restart the loop, skipping any statements after that one).
Also, add a break after the last line of code for success (Q = R1'). This will terminate the while loop.
You should also add '\n' at the end of each of your error messages so the command prompt will start on a new line.
So, this should work better. Note that this is still a rather fragile way to do this. Using the input function leaves lots of opportunities for the user to enter stuff that doesn't make sense (especially leaving off the square brackets in this case).
while (true)
A=input('Matrix= ');
[n,m]=size(A);
if m==n
if issymmetric(A,'nonskew')
if eig(A)>=0
fprintf('matlab inbuilt function "chol" is: ')
R=chol(A)
[n,m]=size(A);
R1=zeros(n,m);
h=1;
for j=1:n
if j==1
R1(j,1)=sqrt(A(j,1));
else
R1(j,1:h)=((A(j,1:h))/(R1(1:h,1:h))')';
sum=R1(j,1:h)*(R1(j,1:h))';
R1(j,j)=sqrt(A(j,j)-sum);
h=h+1;
end
end
else
fprintf('matrix does not have positive eigenvalues, \n')
continue
end
fprintf('Calculated value is: ')
Q=R1'
break
else
fprintf('the input is not a symmetrical matrix, try again\n')
continue
end
else
fprintf('the format of input matrix is wrong\n')
continue
end
end

댓글 수: 4

Komel Kaur
Komel Kaur 2020년 12월 19일
would it be better if i used a function instead of input?
Les Beckham
Les Beckham 2020년 12월 19일
편집: Les Beckham 2020년 12월 19일
That is how I would do it.
For example, put your original code (with your original return calls) in a function. Delete the input line but keep the added '\n' that I suggested in the error/warning messages). This is not as interactive, as you lose the prompt from the input function.
You would then define your matrix on the command line and then call the function.
>> mat = [1 0; 0 2];
>> [R, Q] = compareChol(mat);
Note that this also returns R and Q into your base workspace so you can do other things with them if you wish (otherwise they would disappear when the function exited).
You can also get rid of the returns completely by using error (which will print your message and exit the function).
See example below.
function [R, Q] = compareChol(A)
[n,m] = size(A);
if m == n
if issymmetric(A,'nonskew')
if eig(A)>=0
fprintf('matlab inbuilt function "chol" is: ')
R = chol(A)
[n,m]=size(A);
R1=zeros(n,m);
h=1;
for j=1:n
if j==1
R1(j,1)=sqrt(A(j,1));
else
R1(j,1:h)=((A(j,1:h))/(R1(1:h,1:h))')';
sum=R1(j,1:h)*(R1(j,1:h))';
R1(j,j)=sqrt(A(j,j)-sum);
h=h+1;
end
end
else
error('matrix does not have positive eigenvalues')
end
fprintf('Calculated value is: ')
Q = R1'
else
error('the input is not a symmetrical matrix, try again')
end
else
error('the format of input matrix is wrong')
end
Komel Kaur
Komel Kaur 2020년 12월 20일
ohh.. understood. thank you for all your help
Les Beckham
Les Beckham 2020년 12월 20일
You are welcome.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Linear Algebra에 대해 자세히 알아보기

질문:

2020년 12월 19일

댓글:

2020년 12월 20일

Community Treasure Hunt

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

Start Hunting!

Translated by