Adding a plot to my code

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

 Risposta accettata

Star Strider
Star Strider il 15 Ago 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 Commenti

Ollie
Ollie il 15 Ago 2015
Thank you so much for the reply. I've taken your advice and updated the code which I've attached here and the plot produced seems to be correct. (I tested a few values). I have put the ln(rk) values on the y-axis. My final difficulty is that I need to assign the ln(rk) values that appear on the y-axis of the plot to a new matrix, y which I can then call upon in another function (ie I need to be able to type somefunction(y) in the command window and for y to be the collection of ln(rk) values from the plot) This should be simple but I can't seem to get it working. Could you please explain what code I type into my attached code in order to assign the values to a new matrix, y?
I should have specified what the arguments are also. The code is to solve the linear system Ax = b where A and b are given matrices and x is the solution vector. x0 is the initial guess of the solution, maxIts are the number of max iterations and tol is the tolerance. I have also attached a values file.
My pleasure.
Putting the log(rk) values on the y-axis is easy:
plot(K, log(rk))
If you want to use the log(rk) (and I assume K) values in another function, there are at least two possible ways depending on what you want to do:
1. Change the function to allow it to return them in its output:
function [X,rk,K] = myjacobimod(A,b,x0,maxIts,tol) % Change Only The First Line Of The Function
calling it subsequently as:
[X,rk,K] = myjacobimod(A,b,x0,maxIts,tol);
to return all the outputs to the script or function workspace that called it. You would then assign it to ‘y’ as either:
y = rk;
y = log(rk);
depending on what you want to do.
2. Save the results in a .mat file to load in other scripts when you need to:
save('Jacobi_rk.mat', 'rk', 'K');
you would then load it back into your workspace as:
load('Jacobi_rk.mat');
and all the variables magickally appear in your script or function workspace (depending on what script or function loads the file).
If they are in your workspace and you want to display them in the Command Window, in the Command Window, just type at the prompt:
rk
and again they will magickally appear.
Ollie
Ollie il 16 Ago 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 il 16 Ago 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.

Accedi per commentare.

Più risposte (0)

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by