Plotting results in array of a while loop

Hello,
I want to iteratively plot the results of my while loop, however, I end up with a blank graph or an error.
Here is the code:
Vlow=350;
fs=100000;
n=0.56;
Lk=41.98*0.000001;
P=10000;
Vhigh=900;
C=0;
i=0;
s=poly(0,"s");
while Vlow < 450
Polynome=((P*Lk*n*fs)/(Vhigh*Vlow))-s+s^2;
solution=roots(Polynome);
out = solution(solution<0.35 & solution>0.129);
I1=(1/(2*Lk*fs))*(2*(Vlow/n)*out+Vhigh-(Vlow/n));
I2=(1/(2*Lk*fs))*(2*Vhigh*out-Vhigh+(Vlow/n));
if I1 > I2 then
C= I1;
else
C=I2;
end
Y(i)=C;
X(i)=Vlow+20;
i=i+1;
end
plot(X,Y);
Any help would be greatly appreciated,
Thank you

8 Comments

Hicham - please copy and paste the full error message to your question. Is the problem with poly or something else?
@Hicham Lhachimi, just to be clear, the code you shared does not generate an error message? Your question states that it does. If it does, we would need the entire error message.
What is the function "ploy"? When I run your code, an error message is generated.
Do you mean "ploy" is a typo and it should read "plot" or do you mean you have a custom function named ploy that we do not know about?
When I replace ploy with plot, there is a blue square marker printed on the plot at (1,0).
Where did you see "ploy" in the code ?
Sorry for the confusion. I meant poly.
Matlab has a fuction poly but it only has 1 input. In this line of your code,
s=poly(0,"s");
you have 2 inputs which would cause an error. The syntax you're using looks like you meant to use plot instead of poly.
You can replace it by s=[1 -1 ((P*Lk*n*fs)/(Vhigh*Vlow))] to write it as a polynomial
So s is this:
s =
1 -1 0.0746311111111111
which is a 1-by-3 vector, so it throws an error when you do this:
Polynome=((P*Lk*n*fs)/(Vhigh*Vlow))-s+s^2;
Were you expecting s to be a scalar instead of a 1-by-3 vector?
Here is exactly what I got ,when I want to select only one root which will be the out variable, I don't see any value of out on the workspace:

Sign in to comment.

 Accepted Answer

Adam Danz
Adam Danz on 22 Jun 2020
Edited: Adam Danz on 22 Jun 2020
As the error message indicates, a subscript must be a real, positive integer or logical.
On the first iteration, i equals 0 which does not fulfill these requirements. 0 is not positive and it's not a logical value. So, when you index Y(i), or, Y(0), you get the error.
Instead of initializing i=0, set it to 1, i=1;
More importantly, the while-loop is defined by the value of Vlow but this value never changes within the while-loop so the while-loop will never end. Vlow will always be less than 450 because its original value is 350 which never changes within the loop.
Perhaps you meant to include something like this at the end of the while-loop.
Vlow = X(i);
If that's the case, then you could replace the while-loop with a for-loop since you know ahead of time the number of iterations. The number of iterations will be the number of values in
values = 350 : 20 : 450;

16 Comments

Thank you for your reply @Adam Danz, but I still get this error:
Subscripted assignment dimension mismatch.
Error in test (line 20)
Y(i)=C;
That's a different error.
What is c? If it's not a scalar value, you cannot store it that way.
Try,
Y(i,:) = ....
Also, this should throw an error too,
if I1 > I2 then
'then' is not part of the syntax.
Okay thank you very much but the code still generates error about plot(X,Y) which I don't understand
Adam Danz
Adam Danz on 22 Jun 2020
Edited: Adam Danz on 22 Jun 2020
I can't help without knowing more info.
The error is:
Error using plot vectors must be the same length.
Error in test Plot(X,Y);
Without seeing your updated code and without knowing the size of the input variables I have no idea what X and Y are.
Here is the updated code:
Vlow=350;
fs=100000;
n=0.56;
Lk=41.98*0.000001;
P=10000;
Vhigh=900;
C=1;
i=1;
for Vlow=350:20:450
Polynome=[1 -1 ((P*Lk*n*fs)/(Vhigh*Vlow))];
solution=roots(Polynome);
out = solution(solution < 0.35 & solution > 0.129);
I1=(1/(2*Lk*fs))*(2*(Vlow/n)*out+Vhigh-(Vlow/n));
I2=(1/(2*Lk*fs))*(2*Vhigh*out-Vhigh+(Vlow/n));
if I1 > I2
C= I1;
else
C=I2;
end
Y(i,:)=C;
X(i,:)=Vlow;
i=i+1;
end
plot(X,Y);
Adam Danz
Adam Danz on 22 Jun 2020
Edited: Adam Danz on 22 Jun 2020
It looks like you need to spend some more time exploring the data being generated within the code.
First, learn how to use debug mode. Put a break point at the beginning of your for-loop and then step through each line of the code. Look at result of each line of code.
You'll see that the line that produces the out variable is empty! That's because the solution variable contains values that are not between 0.129 and 0.35 (shown in the gif below).
So, the problem you're having with plot(x,y) is not the real problem. The problem is deeper. Your code isn't doing what you expect it to do or your input data is not what you expect. You're the only one who can fix that. So, go through the code line by line and look and think about the values of each variable.
This answer may also be helpful. It shows how to view variables in a function workspace.
Thank you very much for your help, I will spend more time exploring data and see what is happening
Glad I could help. Once you are comfortable with using debug mode, you'll be able to solve some of the minor problems much more quickly.
I don't understand why I don't get the maximum of the A (I get 1 instead of getting 29 which is the max value). I have declared C as an array, this the only problem that I had in the new code:
Vlow=350;
fs=200000;
n=0.56;
Lk=41.98*0.000001;
P=10000;
Vhigh=900;
C=[1];
i=1;
for Vlow=350:20:450
Polynome=[1 -1 ((P*Lk*n*fs)/(Vhigh*Vlow))];
solution=roots(Polynome);
out = solution(solution < 0.35 & solution > 0.129);
I1=(1/(2*Lk*fs))*(2*(Vlow/n)*out+Vhigh-(Vlow/n));
I2=(1/(2*Lk*fs))*(2*Vhigh*out-Vhigh+(Vlow/n));
A=[I1 I2];
C(i,:)=max(A);
Y(i,:)=C;
X(i,:)=Vlow;
%i=i+1;
end
plot(X,Y);
I've been editing your comments so that your code can be formatted. Going forward, when you include code, please format it using the [>] button.
On which iteration do you get this problem? On the first iterations, when Vlow=350, A=[29.97, 3.1976].
Yes, Also Y(i,:) remains 1, after one iteration, I see that C is maximum but when I want to see the value of Y(1,1) it says Index exceeds matrix dimensions.
1) I didn't understand your response. I only tested the first iteration of your loop and the A values are shown in my previous comments. I don't know where you're getting the value of 1 for A.
2) After the loop is finished, what size do you expect the variables C,Y, and X to be?
3) Here's a better way to for the loop. Instead of looping through values of Vlow, loop through the index values of Vlow. That way you can use the i-variable to store the outputs properly.
Vlow=350:20:450;
for i = 1:numel(Vlow)
Polynome=[1 -1 ((P*Lk*n*fs)/(Vhigh*Vlow(i)))]; % note, Vlow was replaced with Vlow(i)
solution=roots(Polynome);
out = solution(solution < 0.35 & solution > 0.129);
I1=(1/(2*Lk*fs))*(2*(Vlow(i)/n)*out+Vhigh-(Vlow(i)/n));% note, Vlow was replaced with Vlow(i)
I2=(1/(2*Lk*fs))*(2*Vhigh*out-Vhigh+(Vlow(i)/n));% note, Vlow was replaced with Vlow(i)
A=[I1 I2];
C(i,:)=max(A);
Y(i,:)=C;
X(i,:)=Vlow(i);% note, Vlow was replaced with Vlow(i)
end
Now it works well, looping through the index values of Vlow is much better. Thank you very much for your support (y).
Agreed. I almost always define loops as i = 1:n rather than i = some_vector.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!