Plotting results in array of a while loop

4 ビュー (過去 30 日間)
Hicham Lhachimi
Hicham Lhachimi 2020 年 6 月 22 日
コメント済み: Adam Danz 2020 年 6 月 22 日
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 件のコメント
Image Analyst
Image Analyst 2020 年 6 月 22 日
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?
Hicham Lhachimi
Hicham Lhachimi 2020 年 6 月 22 日
編集済み: Hicham Lhachimi 2020 年 6 月 22 日
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:

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2020 年 6 月 22 日
編集済み: Adam Danz 2020 年 6 月 22 日
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 件のコメント
Hicham Lhachimi
Hicham Lhachimi 2020 年 6 月 22 日
Now it works well, looping through the index values of Vlow is much better. Thank you very much for your support (y).
Adam Danz
Adam Danz 2020 年 6 月 22 日
Agreed. I almost always define loops as i = 1:n rather than i = some_vector.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by