필터 지우기
필터 지우기

How to plot this while loop?

조회 수: 1 (최근 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 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