Hello all,(adsbygoogle = window.adsbygoogle || []).push({});

I am new to the ODE solvers in Matlab and am trying to learn:

First, I am solving a 2nd order ODE to determine x(t), x'(t), and x''(t). No problem. Then, I am using these solutions to calculate two coefficients (that are functions of time) that are used in a second, second order ODE. The output solution is a vector of NaN's.

To explain... the first ODE script:

and first ODE function (where c1 and c2 are arbitrary, constant coefficients):Code (Text):% Initial Conditions:

x10 = x0; %x0 is some initial value

x20 = 0;

tspan = linspace(0,tmax+dt, tmax/dt+1);

% Solve ODE

[t,x] = ode23('ode', tspan, [x10,x20]);

So now I have solutions for x(t), x'(t), x"(t) (that I name x, xp, xp2 respectively).Code (Text):function xp = ode(t,x)

xp = zeros(2,1);

xp(1) = x(2);

xp(2) = c2*x(1)-c1*x(2)

Ok, so no problem until I get to the second ODE. Following the same approach:

Script:

ODE function:Code (Text):% Initial Conditions:

a10 = a0; %a0 is some initial value

a20 = 0;

tspan = linspace(0,tmax+dt, tmax/dt+1);

% Solve ODE

[t,a] = ode23('ode2', tspan, [a10,a20]);

Using the above ODE function, I will get an indicie mismatch error. If I try to apply indices to K1 and K2 [(floor(t/dt)+1)], the output will all be NaN's.Code (Text):function ap = ode2(t,a)

K1 = xp2./x;

K2 = xp1./x; % the length of K1 and K2 is the length of x from solution above

ap = zeros(2,1);

ap(1) = a(2);

ap(2) = K2*a(1)+ K1*a(2)

It seems like there should be a simple answer as to how to solve a second order ODE that has coefficients (that are functions of time) that I have already solved for. Thanks in advance. (Hopefully my explanation is clear).

**Physics Forums - The Fusion of Science and Community**

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Matlab ODE45: solving with coefficients that are functions of time

Loading...

Similar Threads - Matlab ODE45 solving | Date |
---|---|

Matlab Plotting the tragectory of an asteroid in MATLAB | Mar 2, 2017 |

Matlab Finite difference numerical integration or ode45? | May 17, 2016 |

Projectile motion with friction in MATLAB (ODE45) | Apr 26, 2014 |

Help me with coupled ode45 equations in matlab | Nov 26, 2012 |

Solving four ordinary dif equations simultaneously using ode45 matlab | Nov 29, 2006 |

**Physics Forums - The Fusion of Science and Community**