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

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 4K views
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:
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).