Line plot not showing up in for loop iteration

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

 采纳的回答

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 个评论

I appreciate the reponse. I'll try to experiment with it some more, since I do use f later on in the code. So far, adapting your example into my code doesn't seem to do what I want. It will just plot the latest f value after the loop exists. I think it's because nothing in my code depends on the 'i' input, mathmatically. I'm just using it to iterate through the loop. Every time it goes through the loop, and solves the new set of the system of equations I have set up, I will get a new 'f' and a new 'B'. And then those values are used later on. The final versions of those values are used later on, but I want to plot the one from each iteration. Thanks again for the help though.
It will just plot the latest f value after the loop exists.’
It shouldn’t. It should plot all of them, since they’re saved in a vector.
I think it's because nothing in my code depends on the 'i' input, mathmatically. I'm just using it to iterate through the loop.
No, that’s not the problem, since ‘f’ can be anything it’s defined to be, and —
f(i) = rand;
that is not a funciton of ‘i’ would work as well.
Plotting with lines connecting the points isn’t possible with the plot calls inside the loop, since plot only plots lines betweeen pairs of points, and the pairs have to be defined beforehand. As it currently is coded, it’s only plotting individual points (the markers) for that reason.
Also, at present, ‘f’ and ‘B’ aren’t being saved as vectors, so only the last value of each (as scalars) will be available for use later in the code. Work on defining them as vectors as I demonstrated, and the plots and everything else that uses those vector values later will work. Otherwise, it won’t.
.
f = 3.59; %intially assume best possible stage effort
g2 = 5/3;
g3 = 5/3;
g4 = 4/3;
g5 = 4/3;
Cout = 45;
N=4;
H=15;
iv=1:i;
for i = 1:numel(iv)
syms A B C D %Stage A, B, C, D
eq1 = (g5 * (45 + D + A))/f == D;
eq2 = (g4*D)/f == C;
eq3 = (g3*C+B)/f == B;
eq4 = (g2 * B)/f == A;
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;
f(i)= (G*H*Branchnew)^(1/N);
Fnew = G*H*Branchnew;%new effort
Dnew = N*Fnew^(1/N) + P;
%print values
fprintf('Iteration %d:', i)
f
Branch
end
plot(iv, f, '-ro', 'MarkerFaceColor','r')
I put the new code above just to make it a bit easier. Running that code results in the error "Unable to perform assignment because the left and right sides have a different number of elements." Did I implement your reccomendations correctly, or did I totally miss the mark haha.
I get what you're saying though, and I appreciate you clarifying why plotting inside vs plotting outslide the loop gives different results. I'll have to find another time to experiment though, since I also wanted to blot Asol, Bsol, Csol, Dsol, Fnew and Dnew vs i.
i?
G?
Others?
f = 3.59; %intially assume best possible stage effort
g2 = 5/3;
g3 = 5/3;
g4 = 4/3;
g5 = 4/3;
Cout = 45;
N=4;
H=15;
i = 5; % Supply Missing Variable
iv=1:i;
for i = 1:numel(iv)
syms A B C D %Stage A, B, C, D
eq1 = (g5 * (45 + D + A))/f == D;
eq2 = (g4*D)/f == C;
eq3 = (g3*C+B)/f == B;
eq4 = (g2 * B)/f == A;
sol = solve([eq1, eq2, eq3, eq4], [A, B, C, D]);
sol.A
Asol = double(sol.A);
Bsol = double(sol.B);
Csol = double(sol.C);
Dsol = double(sol.D);
b1new = (Csol + Bsol)/Csol;
b2new = (Cout+Dsol + Asol)/Cout;
Branchnew = b1new*b2new;
f(i)= (G*H*Branchnew)^(1/N);
Fnew = G*H*Branchnew;%new effort
Dnew = N*Fnew^(1/N) + P;
%print values
fprintf('Iteration %d:', i)
f
Branch
end
ans = 
Unrecognized function or variable 'G'.
plot(iv, f, '-ro', 'MarkerFaceColor','r')
.
Oh so sorry, G= 4.9,and i is any number, but I saw you already made it 5, which happens to be the number I typically test. I believe that should be all the variables needed, aside from the ones I didn't already mention.
Also I seriously appreciate your help, but my workaround for my issue is to just store each variable into a matrix and then plot things in excel. Crude, but I still get the results I want, incase you no longer have the time to help out.
Stopping here.
The code simply has too many errors for me to fix.
No problem. I appreciate the effort
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.
.

请先登录,再进行评论。

更多回答(1 个)

Jan
Jan 2021-11-14
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 个评论

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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

产品

版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by