MATLAB Answers

Error plot is starting at the wrong value in my "while loop"

1 view (last 30 days)
Caleb Hedin
Caleb Hedin on 22 Nov 2020
Edited: VBBV on 23 Nov 2020
clear all
w=(12.35)^0.5;
%theta_exact=10*exp(-2*t)*cos(t*w)+20*exp(-2*t)*sin(w*t);
theta1(1)=10; %deg
psy1(1)=0; %deg/s
g=9.81; %m/s^2
c=4; %1/s
l=0.6; %m
dt1=0.1;
t1(1)=0;
i=1;
while t1<6
theta1(i+1)=theta1(i)+psy1(i)*dt1
psy1(i+1)=(-g/l)*dt1*theta1(i)+(1-c*dt1)*psy1(i);
theta_exact=10.*exp(-2.*t1).*cos(w.*t1)+(20/w).*exp(-2.*t1).*sin(w.*t1)
epsilon1=(theta1(i)-theta_exact)
t1(i+1)=t1(i)+dt1;
i=i+1;
end
figure(1); clf;
hold on
plot(t1,theta1,'red')
plot(t1(1:end-1),theta_exact,'blue')
figure(2); clf;
plot(t1(1:end-1),(epsilon1))
If you read the code you notice that the second plot is a time vs "epsilon1". My issue is that the first value for "epsilon1" is -10 but epsilon1=(theta1(i)-theta_exact), where theta1(i) and theta_exact are the same value for their first values. The first value should read 0 for epsilon1, can anyone help me figure out why this is not the case?

  0 Comments

Sign in to comment.

Answers (1)

VBBV
VBBV on 23 Nov 2020
Edited: VBBV on 23 Nov 2020
while t1<6
theta1(i+1)=theta1(i)+psy1(i)*dt1
psy1(i+1)=(-g/l)*dt1*theta1(i)+(1-c*dt1)*psy1(i);
theta_exact(i)=10*exp(-2*t1(i))*cos(w*t1(i))+(20/w)*exp(-2*t1(i))*sin(w*t1(i))
epsilon1(i)=(theta1(i)-theta_exact(i))
t1(i+1)=t1(i)+dt1;
i=i+1;
end
As you are using while loop use the counter, i for epsilon1 and theta_exact

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!