How to solve an equation of motion with the ode solver with time dependent components

Hello,
I have the following equation of motion of a mass M, a damping force D, a stiffness K and a (time dependent) external Force F(t):
M*x‘‘(t)+D*x‘(t)+K*x(t)= F(t)
Is there any possibility to solve this equation of motion with ODE or another Matlab command?
Now I am new to Matlab and still a student, so i don't have a lot of experience with Matlab. I hope this is a simple problem that a more experienced Matlab user can help me to solve this problem.
Thanks,
Jenny

Answers (1)

function main
x0=[0 1]; % first component is x(0), second x'(0)
tspan=[0 20]; % timespan of integration
[T,Y] = ode45(@myfun,tspan,x0);
plot(T,Y(:,1),T,Y(:,2));
function dy = myfun(t,y)
F=...;
K=...;
D=...;
M=...;
dy=zeros(2,1);
dy(1)=y(2);
dy(2)=(F-K*y(1)-D*y(2))/M;
Best wishes
Torsten.

3 Comments

Thanks for your answer!
My problem is that F isn't constant, but time dependent (after every second it gets another value. -13.985, -103,5, -96.34, -87.98, -93.56)
If I try to define F as an function, I get an error "Function definitions are not permitted in this context."
%m*x''(t)+d*x'(t)+k*x(t)=F(t)
m=1;
d=0.5;
k=10;
load ('matlab') %a_g_elcentro(t)
function f=earthquake(t)
f=m*a_g_elcentro(t);
dy=@(t,z)[z(2);(f-d*z(2)-k*z(1))/m];
ic=[0 0];
tspan=[0 10];
[t,y] = ode23(Gleichung,tspan,ic);
plot(t,y(:,1)), title('Position vs Time')
What about
dy=@(t,z)[z(2);(earthquake(t)-d*z(2)-k*z(1))/m];
Best wishes
Torsten.
Still "Error: File: Beispiele.m Line: 6 Column: 1 Function definitions are not permitted in this context."

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 22 Nov 2015

Commented:

on 23 Nov 2015

Community Treasure Hunt

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

Start Hunting!