MATLAB Answers

How to plot multiple answers on the same graph

1 view (last 30 days)
I have produced the following code. However, I have multiple values for rnl = 0, 0.0015, 0.0020, 0.0025. How could I update the code so that it will run and plot the four different values for rnl on the same graph?
% Set the parameters for the test problem.
N = 64; % The image is N-by-N.
theta = 4:4:180; % Projection angles.
rnl = 0; % Relative noise level.
kmax = 800; % Number of iterations.
% Generate the test problem.
[A,b_ex,x_ex] = paralleltomo(N,theta);
% Add noise; we scale the noise vector e such that
% || e ||_2 / || b_ex ||_2 = relative noise level (rnl)
rng(0); % Initialize random number generator.
e = randn(size(b_ex)); % Gaussian white noise.
e = rnl*norm(b_ex)*e/norm(e); % Scale the noise vector.
b = b_ex + e; % Add the noise to the pure data.
% Run Kaczmarz's method and compute the error history and its minimum.
X = kaczmarz(A,b,1:kmax);
E = zeros(kmax,1);
for k=1:kmax
E(k) = norm(x_ex-X(:,k));
end
E = E/norm(x_ex);
[minE,K] = min(E);
figure(1), clf
plot(1:kmax,E,'-g',K,minE,'*g','linewidth',1.5,'markersize',6)
legend('Relative error','Minimum')
xlabel('Iterations')
ylabel('Relative error')
  3 Comments
Ryan Philips
Ryan Philips on 25 Jun 2021
The code is using AIR Tools toolbox. With the four different values for rnl, how could I change the rnl value each time to then plot the E and minE value for each change in rnl? Could a loop be used?

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 25 Jun 2021
The code is using AIR Tools toolbox.
I have no idea what that is, and I can’t find it in the File Exchange.
I have no idea what the variables are that use ‘rnl’ in their calculations, or their sizes, so this could be as easy as vector multiplication or could require a for loop.
Try something like this (loop approach):
for k = 1:numel(rnl)
e{k} = rnl(k)*norm(b_ex)*e/norm(e); % Scale the noise vector.
end
It will then be necessary to use loops to do any other calculations with the cell array ‘e’. Address elements of ‘e’ as ‘e{k}’ in any subsequent calculations using it.
This, and subsequent calculations, could easily be vectorised, if the sizes of the variables were known and they permitted vectorisation. Lacking that information, loops are the only reliable approach.
.

More Answers (0)

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!