How to plot multiple answers on the same graph

2 ビュー (過去 30 日間)
Ryan
Ryan 2021 年 6 月 25 日
コメント済み: Star Strider 2021 年 6 月 29 日
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 件のコメント
Star Strider
Star Strider 2021 年 6 月 25 日
Unrecognized function or variable 'paralleltomo'.
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.
.
  2 件のコメント
Ryan
Ryan 2021 年 6 月 29 日
Thank you. The loop approach is working for the multiple rnl values.
Star Strider
Star Strider 2021 年 6 月 29 日
As always, my pleasure!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDiscrete Data Plots についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by