Borrar filtros
Borrar filtros

How to plot this while loop?

1 visualización (últimos 30 días)
Desiree
Desiree el 1 de Ag. de 2019
Comentada: Star Strider el 1 de Ag. de 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.

Respuesta aceptada

Star Strider
Star Strider el 1 de Ag. de 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 comentarios
Desiree
Desiree el 1 de Ag. de 2019
Thanks a lot!
Star Strider
Star Strider el 1 de Ag. de 2019
As always, my pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by