How to plot this while loop?

2 visualizzazioni (ultimi 30 giorni)
Desiree
Desiree il 1 Ago 2019
Commentato: Star Strider il 1 Ago 2019
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.

Risposta accettata

Star Strider
Star Strider il 1 Ago 2019
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 Commenti
Desiree
Desiree il 1 Ago 2019
Thanks a lot!
Star Strider
Star Strider il 1 Ago 2019
As always, my pleasure!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Line Plots in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by