How to plot multiple answers on the same graph

2 次查看(过去 30 天)
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 个评论
Ryan
Ryan 2021-6-25
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?

请先登录,再进行评论。

采纳的回答

Star Strider
Star Strider 2021-6-25
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.
.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by