Line plot not showing up in for loop iteration
78 views (last 30 days)
Show older comments
Jonathan Gerges
on 14 Nov 2021
Commented: Star Strider
on 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
0 Comments
Accepted Answer
Star Strider
on 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 Comments
Star Strider
on 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.
.
More Answers (1)
Jan
on 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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!