second order differential equation with variable coefficients

I have to solve this differential equation: m*d^2x/dt^2 +k*x= F(x) where F(x) is known for points

 Accepted Answer

Write your equation as a first-order system
y1'=y2
y2'=(-k*y1+F(y1))/m
use "interp1" to interpolate F(y1) from your known values and call "ode45" to solve.
Best wishes
Torsten.

More Answers (2)

Thank you for your answer, I have written the algorithm in this way but it doesn't work
clc; close all; clear all;
xx=[0,2,4,6,8,10,12];
F=[0.65,0.75,0.80,0.81,0.80,0.50];
tspan=[0,0.1];
x0=[0,0];
Fc=@(x) interp1(xx,F,x);
m=15E-3;
k=50;
function dxdt=myfun(t,x,m,k,Fc);
dxdt(1)=x(2);
dxdt(2)=(-k*x(1)+Fc(x(1)))/m;
end
[t,x]=ode45(@(t,x) myfun(t,x,m,k,Fc),tspan,x0);
I've solved, xx and F hadn't the same length.
Thank you.

1 Comment

... and dxdt has to be a column vector:
function dxdt=myfun(t,x,m,k,Fc);
dxdt=zeros(2,1);
dxdt(1)=x(2);
dxdt(2)=(-k*x(1)+Fc(x(1)))/m;
end
Best wishes
Torsten.

Sign in to comment.

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!