How to solve the differential equation for a mass-damper-spring system without ode45?

I am trying to solve the differential equation for a mass-damper-spring system when y(t) = 0 meters for t ≤ 0 seconds and x(t) = 10 Newtons for t > 0 seconds. Assume that M = 1 kg, D = 0.5 N-s/m, and K = 2 N/m.
So far I have this code
x = 10
m = 1
D = 0.5
K = 2
delta_t=0.1
a=0
t=0:0.1:25
y(1)=1
for i = 5.0:0.1:25.0
a = 2*y(i) + (y(i)-y(i-1))/(2*delta_t) +((y(i)-y(i-1))-(y(i-2)-y(i-3)))/delta_t
end
but I keep getting an Index exceeds matrix dimensions error.
Can anyone help? Thank you!

Answers (1)

Looking at your code, it has to be something like this:
clc; clear all ;
x = 10 ;
m = 1 ;
D = 0.5 ;
K = 2 ;
delta_t=0.1 ;
a=0 ;
t=0:delta_t:25 ;
y = zeros(1,length(t)) ;
y(1)=1 ;
y(2) = rand ;
y(3) = rand ;
for i = 4:length(t)
y(i) = 2*y(i) + (y(i)-y(i-1))/(2*delta_t) +((y(i)-y(i-1))-(y(i-2)-y(i-3)))/delta_t ;
end
As there was y(i-1),y(i-2),y(i-3), you must have y defined three values; loop should run from 4. I am not sure about your equation inside the loop.
There are other methods like Newmark Beta method, Wilson theta method etc.. to solve your equation. Refer Dynamics of Structures - A. K. Chopra

5 Comments

Thank you so much! If you are trying to compute this ODE using a time step and for loop, do you have a suggestion of what code should run inside the loop? thanks!
I wonder what's the ODE behind your discretization.
Best wishes
Torsten.
@Torsten second order differential equation
And you have initial conditions for y and y' at t=0 ?
Best wishes
Torsten.

Sign in to comment.

Tags

Asked:

on 17 Feb 2017

Commented:

on 17 Feb 2017

Community Treasure Hunt

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

Start Hunting!