How to solve an equation of motion with the ode solver when the spring constant is dependent on the position of the solution of the equation of motion

3 views (last 30 days)
I have the following equation of motion of a mass m and a position dependable springconstant k(x):
m*x''(t)+k(x)*x(t)=0
Where k(x) is the spring constant that is dependable on the position of x at a the time step that x is solved.
I can solve this with the Forward Euler Method by substituting the position x(n) at time step n into k(x(n)) and calculating the next position x(n+1) ect.
But with the ode solver it seems I can't do this, because the solver continues from the first time step to the last without letting me do anything in between.
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,
Maarten

Accepted Answer

Teja Muppirala
Teja Muppirala on 2 Aug 2012
There is absolutely no problem in using MATLAB's ODE solvers to solve a problem like this. Say for example
k(x) = 1+x^2
Then you can solve your ODE
m*x''+(1+x^2)*x=0
just like this:
ode45(@(t,x) [x(2); -x(1)*(1 + x(1)^2) ] ,[ 0 10 ] ,[ 1 0 ])
  1 Comment
Maarten
Maarten on 2 Aug 2012
Thank you so much, this was exactly what i was looking for. I knew their had to be a simple solution for this problem, but i just didn't see it. Thanx for helping me out.
If your're ever in Holland, i will buy you a beer;)

Sign in to comment.

More Answers (1)

Star Strider
Star Strider on 1 Aug 2012
The ODE solvers let you specify a vector of specific times to integrate the differential equation rather than simply specifying the range. I refer you to the documentation for ode23 and the rest of the ode solvers, specifically:
tspan A vector specifying the interval of integration, [t0,tf]. The solver imposes the initial conditions at tspan(1), and integrates from tspan(1) to tspan(end). To obtain solutions at specific times (all increasing or all decreasing), use tspan = [t0,t1,...,tf].
For tspan vectors with two elements [t0 tf], the solver returns the solution evaluated at every integration step. For tspan vectors with more than two elements, the solver returns solutions evaluated at the given time points. The time values must be in order, either increasing or decreasing.
I put the relevant sections in bold to make them easier to find.

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!