How to plot this while loop?

2 ビュー (過去 30 日間)
Desiree
Desiree 2019 年 8 月 1 日
コメント済み: Star Strider 2019 年 8 月 1 日
Hello. I’m trying to plot this while loop with no success. Each time it gives me a blank plot. Tried using hold on at the end of the loop, before the loop, or just using plot but without results. The codes work perfectly fine without plotting as it displays all the results that I want. Here I wish to plot both sigma and u in one same plot (but even tried only sigma, still nothing). Here it is:
t=0;
x=[1,1,1];
v=x(1);
vc=sin(t+1)-cos(0.5*t-2);
sigma=v-vc;
alpha=30;
tau=0.0001;
while t<=10
f=-sin(t+5)-0.5*cos(t)-x(2);
v=x(1);
vc=sin(t+1)-cos(0.5*t-2);
vcc=cos(t+1)+0.5*sin(0.5*t-2);
sigma=v-vc;
sigmadot=f-vcc;
u=alpha*((abs(sigmadot)).^2*sign(sigmadot)+sigma)/((sigmadot).^2+abs(sigma));
dx=[f,cos(1+x(1)+x(3))+(2-cos(x(1)+x(3)+1))*u,cos(x(1)+0.5*x(3)-t)-x(3)+u];
x=x+dx*tau;
t=t+tau;
end
Please if someone can tell me how to plot this properly or if maybe there is a way to rewrite the code without the while loop so I can plot it without a problem hopefully, It will be appreciated.

採用された回答

Star Strider
Star Strider 2019 年 8 月 1 日
Try this:
t=0;
x=[1,1,1];
v=x(1);
vc=sin(t+1)-cos(0.5*t-2);
sigma=v-vc;
alpha=30;
tau=0.0001;
k = 1; % Counter
while t<=10
f=-sin(t+5)-0.5*cos(t)-x(2);
v=x(1);
vc=sin(t+1)-cos(0.5*t-2);
vcc=cos(t+1)+0.5*sin(0.5*t-2);
sigma=v-vc;
sigmadot=f-vcc;
u=alpha*((abs(sigmadot)).^2*sign(sigmadot)+sigma)/((sigmadot).^2+abs(sigma));
dx=[f,cos(1+x(1)+x(3))+(2-cos(x(1)+x(3)+1))*u,cos(x(1)+0.5*x(3)-t)-x(3)+u];
x=x+dx*tau;
t=t+tau;
sigmav(k) = sigma; % Vector
uv(k) = u; % Vector
tv(k) = t; % Vector
k = k + 1; % Increment Countr
end
figure
plot(tv, uv)
grid
figure
plot(tv, sigmav)
grid
  4 件のコメント
Desiree
Desiree 2019 年 8 月 1 日
Thanks a lot!
Star Strider
Star Strider 2019 年 8 月 1 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by