Matlab ODE45: solving with coefficients that are functions of time

In summary, the conversation discusses the process of solving a second order ODE using Matlab's ODE solvers. The user first solves a 2nd order ODE to determine x(t), x'(t), and x''(t), and then uses these solutions to calculate two coefficients that are functions of time for a second, second order ODE. However, the output solution for the second ODE is a vector of NaN's. After some troubleshooting, the user realizes that the issue lies with the coefficient calculation, not the ODE solver. They fix the issue by indexing the coefficients as a function of time in the ODE function.
  • #1
hasidim
15
0
Hello all,

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:

Code:
% 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]);

and first ODE function (where c1 and c2 are arbitrary, constant coefficients):

Code:
function xp = ode(t,x)
xp = zeros(2,1);
xp(1) = x(2); 
xp(2) = c2*x(1)-c1*x(2)

So now I have solutions for x(t), x'(t), x"(t) (that I name x, xp, xp2 respectively).

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

Script:
Code:
% 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]);

ODE function:

Code:
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)

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.

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).
 
Last edited:
Physics news on Phys.org
  • #2
Hi All,

I found my issue. It was with the coefficient calculation, not with the ODE solver.

For what its worth, the coefficients in the second ODE (the coefficients that are functions of time) do need to be indexed as a function of t. So, something like this:

Code:
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(t/dt+1)*a(1)+ K1(t2/dt+1)*a(2)
 

Related to Matlab ODE45: solving with coefficients that are functions of time

What is Matlab ODE45?

Matlab ODE45 is a function in Matlab that solves ordinary differential equations (ODEs) using a variable-step Runge-Kutta method. It is commonly used in scientific and engineering applications to model and simulate systems.

How does Matlab ODE45 handle coefficients that are functions of time?

Matlab ODE45 can handle coefficients that are functions of time by using the 'odefun' input argument. This argument allows the user to specify the differential equation as a function of both time and the dependent variable, making it possible to include coefficients that vary with time.

Can Matlab ODE45 solve stiff systems?

Yes, Matlab ODE45 is capable of solving stiff systems. It uses an adaptive step size control to handle both stiff and non-stiff systems, making it a versatile tool for solving various types of ODEs.

How do I specify initial conditions for Matlab ODE45?

The initial conditions for Matlab ODE45 can be specified using the 'y0' input argument. This argument should be a vector that contains the initial values for all the dependent variables in the ODE system. Alternatively, if the ODE system is a first-order system, the initial conditions can be specified as a scalar value.

What are the limitations of using Matlab ODE45 to solve ODEs with time-varying coefficients?

One limitation of using Matlab ODE45 to solve ODEs with time-varying coefficients is that it may not be the most efficient method for certain types of problems. In some cases, using a specialized solver or implementing a custom algorithm may be more effective. Additionally, the user must be careful to properly define the time-varying coefficients in the 'odefun' function to ensure accurate results.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
933
Back
Top