How to plot this while loop?

1 次查看(过去 30 天)
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 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by