12 views (last 30 days)

Hello, I am quite new to MATLab, this is my very first program, which is also a uni assignment. This is my program thus far:

m=2;

l=5;

g=9.81;

c=0.5;

theta=45;

theta_dot=5;

t=0;

dt=(0.1/theta_dot);

while t<50

theta_dotdot=-(g/l)*sin(theta) - (c/m*l^2)*theta_dot; %motion eqn

theta=theta+dt*theta_dot;

theta_dot=theta_dot+dt*theta_dotdot;

figure(1);

axis([0 0 -2*pi -2*pi]);

hold on;

plot(theta_dotdot, theta);

t=t+dt;

end

What I have done so far (the code above explained): 1.Giving some values (the first part of the code). 2.The equation of motion (%motion eqn). 3.Theta and Theta_dot increasing in value (recommended method/code I was given in the assignment). 4. Figure - doesn't work, and I am not quite sure how it should work. 5. Plot - no reason for it not to work, I assume it doesn't plot anything because of the figure error above. 6. t increasing. 7. Everything above is looped, with t=t+dt What I need to do: Firstly, I need it to give me a figure of the pendulum positions (don't know how to explain this better)- the figure part of the code Secondly, I need it to plot theta_dotdot, theta. I think I got this part right, but the program does not run that part yet, im assuming because of the errors in the figure part.

There are the erros im getting:

>> run Untitled2

Error using set

Bad property value found.

Object Name: axes

Property Name: 'XLim'

Values must be increasing and non-NaN.

Error in axis>LocSetLimits (line 201)

set(ax,...

Error in axis (line 93)

LocSetLimits(ax(j),cur_arg);

Error in Untitled2 (line 17)

axis([0 0 -2*pi -2*pi]);

Error in run (line 63)

evalin('caller', [script ';']);

>>

If an answer can not be provided, I could really use some help on the errors, why am I getting them, what do they mean.

Youssef Khmou
on 16 Feb 2015

Edited: Youssef Khmou
on 16 Feb 2015

The implementation is acceptable, the first remark is that visualization commands of results should be outside the loop, otherwise you obtain N plots where N is the number of iterations. While loop in this case can be changed into for loop but not necessary, the following version is working , but the physical interpretation still needs correction of the code, try to adjust it :

m=2;

l=5;

g=9.81;

c=0.5;

thet=45;

dtheta=5;

t=0;

dt=(0.1/dtheta);

cc=1;

for t=1:dt:50

d2theta(cc)=-(g/l)*sin(thet) - (c/m*l^2)*dtheta; %motion eqn

theta(cc)=thet+dt*dtheta;

dtheta=dtheta+dt*d2theta(cc);

cc=cc+1;

end

figure(1);

% axis([0 0 -2*pi -2*pi]);

% hold on;

plot(d2theta, theta);

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/178765-driven-damped-pendulum-axes-question#comment_266546

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/178765-driven-damped-pendulum-axes-question#comment_266546

## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/178765-driven-damped-pendulum-axes-question#comment_266552

⋮## Direct link to this comment

https://in.mathworks.com/matlabcentral/answers/178765-driven-damped-pendulum-axes-question#comment_266552

Sign in to comment.