Mathematica MATHEMATICA: NDSolve, 2nd order ODE, Table of IC HELP

AI Thread Summary
The discussion centers on automating the solution of a second-order ordinary differential equation (ODE) using NDSolve in Mathematica, particularly when dealing with multiple sets of constant coefficients and initial conditions. The user seeks a method to efficiently solve the ODE for a table of values rather than inputting each set individually, which is time-consuming. A provided code snippet demonstrates the use of a loop combined with the Reap and Sow functions to achieve this automation. The user clarifies the notation for initial conditions, confirming the use of 'Xo' instead of 'X_o'. After implementing the suggested approach, the user reports success in solving the ODE efficiently.
hasidim
Messages
15
Reaction score
0
Hi all,

I have a 2nd order ODE I am trying to solve using NDSolve. In the ODE there are two constant coefficients and an initial condition that I want to 'vary'; meaning, I have a table of initial conditions with corresponding constant coefficients.

It is straight forward to solve the ODE using each corresponding set of coefficients and IC individually by inputing the sets individually. However, this is time consuming if I have, say, 100 corresponding sets or more.

Is there a way to 'automate' this process?

To be more clear:

Code:
NDSolve[{k* X''[t] + k2* X'[t] + X[t] == 0, X[0] ==X_o, X'[0]==0},X[t],{t,0,tmax}]

I have a table of values for k, k2, and X_o. I would like to solve the ODE for each set of corresponding values.

Thanks!
 
Last edited:
Physics news on Phys.org
Can you adapt this to your problem.

(*Table of {k, k2, Xo}*)
tbl = {{0, 1, 0}, {1, 0, 1}, {3, 4, 5},{1,1,3}};tmax = 2Pi;
Reap[
For[i = 1, i <= Length[tbl], i++,
{k, k2, Xo} = tbl[];
Sow[NDSolve[{k*X''[t]+k2*X'[t]+X[t]==0,X[0]==Xo,X'[0]==0},X[t],{t,0,tmax}]];
]
][[2, 1]]

Underscore is a special character for Mathematica and I'm not sure you want X_o instead of Xo. If X_o is what you really want then you should know enough to be able to fix what I've shown here.
 
Last edited:
Bill Simpson, I indeed mean 'Xo' not 'X_o' (I commonly use an underscore in Matlab).

I will give your recommendation a shot and see how it goes. Thanks a lot!
 
Bill Simpson,

That worked beautifully for my problem. Thanks again for the advice ('Reap' and 'Sow' are new functions to me).
 

Similar threads

Replies
1
Views
537
Replies
1
Views
2K
Replies
1
Views
2K
Replies
7
Views
3K
Replies
4
Views
3K
Replies
3
Views
8K
Replies
3
Views
2K
Back
Top