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 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