Adding a plot to my code

2 views (last 30 days)
Ollie
Ollie on 15 Aug 2015
Commented: Star Strider on 16 Aug 2015
I've attached my code which performs the Jacobi method and is working correctly. However, I need to calculate the residual, rk which is the infinity norm of (Ax(k) - b ) divided by the infinity norm of b. What I need to do is calculate the natural log of rk and plot it against the iteration number K. I then need to be able to access the values on the y-axis.
I believe the code I need is
rk = (norm(A*x - b, inf)) / (norm(b,inf));
plot(log(rk),K);
but I have no idea how to incorporate this into my existing code or how to access the y-axis values. Is there anyone who could return the code to me with plotting commands? Thanks

Accepted Answer

Star Strider
Star Strider on 15 Aug 2015
I can’t run your code because I’m not certain what the arguments are.
However, I would make these changes in the loop, then put the plot call after the end of the ‘K’ loop:
X(:,1) = x0;
rk(K) = norm(x-xtemp,inf)/norm(xtemp,inf);
if rk(K)<tol
break;
end
K = 1:length(rk);
figure(1)
plot(log(rk),K)
The ‘X(:,1)’ assignment is unchanged. I just put it in to show where I would put the ‘rk’ assignment.
NOTE: This is untested code. You may have to experiment with it to get the result you want.
  4 Comments
Ollie
Ollie on 16 Aug 2015
For some reason, whatever I do rk, K or y come up as undefined in the command window even though I am defining them in the script. What I have done is just changed the function line to function[y] and that is producing the desired results. Thank you for your help!
Star Strider
Star Strider on 16 Aug 2015
My pleasure!
The problem may be that they exist only in the function workspace, not your script workspace. (A function’s workspace is local only to it.) You may have to return them from the function to your script workspace to access them.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!