Graph the solution of 3 different variables vs # iterations given 3 initial condition guesses on three separate graph obtained from an iterative solving while loop.
조회 수: 2 (최근 30일)
이전 댓글 표시
I need to get three graphs of the solutions vs iterations it took to get them given three different initial conditon guesses from the user. I have a working code base that plots the solutions vs itereatons for one user input (although they only show up as dots and not connected lines), but I'm not sure how to scale it for three different user guesses.
it looks like this
% Deinfne vaibles
a= ...
b=...
% ask and store user inputs
Promt 1...
f_1= inoput(promt) "Initial guess"
Promt 2...
f_2
Promt 3...
f_3
max # of runs = ...
max error tolerance= ...
i=0;
while i<= max # runs
*** do math***
Z=...
X=...
C=...
calc error between 1st guess and new solution
if error > max error tolerance
initial guess= new val
i= i+1
figure 1 , title, xlabel y label etc
plot ( i , Z) <---( I have to add '.' to get anything to display in graph, otherwise blank or just one dot)
hold on
figure 2 , title, xlabel y label etc
plot ( i , x)
hold on
figure 2 , title, xlabel y label etc
plot ( i , x)
hold on
figure 3 , title, xlabel y label etc
plot ( i , C)
hold on
else
Display results
break
end
end
hold off
It works great for one input great, the graphs all make sense (although they dont form connected lines only dots not sure why). I want it to rerun and plot results for a new guess onto the same graphs, should I move where I put the plot commands/ hold commands? The easiest way I thought of was to just copy/ paste the whole while loop and rename variables for the equivalent promt variables but It doesnt look like its going to save the data from the previous run to overlap on the graphs.
Thank you in advance
댓글 수: 0
답변 (1개)
Clayton Gotberg
2021년 4월 19일
편집: Clayton Gotberg
2021년 4월 19일
One possibility for why your charts only appear when you use point markers is what you are using for your plot data. You set i as zero, then the while loop adds 1 to it each time it runs. At the end, i is still just a scalar value representing the number of runs. To plot a line, you need a value of i for each value of Z, X and C that you want to appear on the chart.
You also appear to write over Z, X and C on each loop. If you want to plot them, you'll need to save the value in each loop. For example,
C(i) = <value> % sets the i-th element of C equal to <value>
C = [C <value>]. % adds <value> as the new last element of C
If you want to have multiple guesses show up on the same figure, you can specify which figure each of the output charts should arrive at.
figure(1)
% first plot, title, etc
figure(2)
% second plot, title, etc
figure(1)
% returns focus to first plot and lets you make changes there again
If you don't overwrite the data from earlier runs, you can also always do the plotting after all of the calculation.
Suppose you want a user to make three guesses, then have a program do something for each of those three guess and save the results. Here is some basic code that should give you an idea of the structure:
guesses = [1 7 15];
max_runs = 1000;
error_tol = 1e-8;
for k = 1:size(guesses,2) % as many times as there are columns in guesses
Z = []; X = []; C = []; % Empty the saved Z, X and C to prepare for the new loop
initial_guess = guesses(k);
i = 1;
while i<= max_runs
% insert math part
%
%
Z(i)= %
X(i)= %
C(i)= %
guess_soln_diff = abs(new_solution - initial_guess); % solve for error
if guess_soln_diff < error_tol % if the difference is less than tolerance
break % stop execution of while loop
end
initial_guess = new_solution; % if not, change the guess to the new one
i= i+1;
end
Z_results{k} = Z; % Save Z results for later plotting
X_results{k} = X; % Cell arrays have less strict requirements for saving multiple things
C_results{k} = C; % to the same array - no same size or type, for example.
iterations_vector{k} = 1:i; % Make a vector counting from 1 to the number of iterations
end
figure(1)
plot(Z_results{1},iterations_vector{1})
hold on
plot(Z_results{2},iterations_vector{2})
plot(Z_results{3},iterations_vector{3})
% and so on with other figures
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Line Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!