Line plot not showing up in for loop iteration

21 vues (au cours des 30 derniers jours)
Jonathan Gerges
Jonathan Gerges le 14 Nov 2021
Commenté : Star Strider le 15 Nov 2021
I looked everywhere for a solution to my problem, but for some reason I just cannot get this to work. All I want to do, is to make a normal line plot inside my for loop to show how values change with each iteration. I can make a plot with something like this:
plot(i,f,'*')
But I don't just want a scatter plot, I would like for there to be an actual line connecting all the points. I know this question has been asked a ton before, but I couldn't find anything with my problem. I also tried to turn them into an array to plot it, but that made me run into a whole bunch of other issues. The thing is, my code gives me the answers I expect but I'm just totally stumped. I appreciate any help I can get. For reference, the variable i is a user input, that will dictate how many iterations to step through. Also the variables G,
for i = 1:i
syms A B C D %stages
eq1 = (g5 * (45 + D + A))/f == D;
eq2 = (g4*D)/f == C;
eq3 = (g3*C+B)/f == B;
eq4 = (g2 * B)/f == A;
%eq5 = (g1 * A)/f == Cin
sol = solve([eq1, eq2, eq3, eq4], [A, B, C, D]);
Asol = eval(sol.A);
Bsol = eval(sol.B);
Csol = eval(sol.C);
Dsol = eval(sol.D);
b1new = (Csol + Bsol)/Csol;
b2new = (Cout+Dsol + Asol)/Cout;
Branchnew = b1new*b2new;
Branch=Branchnew;
fnew = (G*H*Branchnew)^(1/N);
f = fnew;
Fnew = G*H*Branchnew;%new effort
Dnew = N*Fnew^(1/N) + P;
fprintf('Iteration %d:', i)
f
Branch
% amatrix = [Asol; {amatrix}];
% bmatrix = [bmatrix; {Bsol}];
% cmatrix = [cmatrix; {Csol}];
% dmatrix = [dmatrix; {Dsol}];
% imatrix= [imatrix; i];
hold on
figure(1);
plot(i,f, 'r-o')
figure(2);
plot(i,f)
i=i+1;
end
hold off

Réponse acceptée

Star Strider
Star Strider le 14 Nov 2021
I can’t run the code.
However it’s obvious that ‘f’ needs to be a vector in order to plot anything other than the markers. Create it as a vector by subscripting it (as ‘f(i)’), then put the plot calls after the loop.
The entry into the loop needs to be —
iv = 1:i;
for i = 1:numel(iv)
. . . CODE . . .
end
then in the loop —
f(i) = fnew;
So for example —
i = 10;
iv = 1:i;
for i = 1:numel(iv)
f(i) = exp(-0.5*i);
end
figure
plot(iv, f, '-ro', 'MarkerFaceColor','r')
grid
That’s an appropriate approach. Adapting the posted code to work correctly with those changes may require some experimentation, because I have no idea if ‘f’ is used elsewhere in the loop. In order to use a specific (scalar) value of it, rather than the entire vector, after these changes, the scalar reference would require that it be used as ‘f(i)’ rather than ‘f’.
.
  8 commentaires
Jonathan Gerges
Jonathan Gerges le 15 Nov 2021
No problem. I appreciate the effort
Star Strider
Star Strider le 15 Nov 2021
As always, my pleasure!
One problem is that the solve call fails to find solutions for i>1 so that result is empty and the loop errors and stops. Other problems are with respect to indexing.
Since I’m not certain wht the code does, I’m not able to fix it.
.

Connectez-vous pour commenter.

Plus de réponses (1)

Jan
Jan le 14 Nov 2021
What exactly is trhe problem with your code?
Maybe all you need is to insert a drawnow command after the plotting?
By the way, you do not need the i=i+1 command in Matlab. The for command does increment the counter already. so incrementing it by you own is confusing only.
  1 commentaire
Jonathan Gerges
Jonathan Gerges le 14 Nov 2021
Modifié(e) : Jonathan Gerges le 14 Nov 2021
Thanks for the info on the for loop. Good to know!
I tried adding the drawnow command after the plot command, but it still doesn't do anything.
To clarify, when I do plot(i,f,'-ro') I get this:
When I just have plot(i,f) I get this:
From what I've read, I think it's because I'm not plotting vectors but I'm still not sure why only the 1st plot command works.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by