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

In summary, to solve a 2nd order ODE with varying coefficients and initial conditions, one can use the 'Reap' and 'Sow' functions in Mathematica to automate the process. This involves creating a table of corresponding values for the coefficients and initial conditions and using a for loop to solve the ODE for each set of values.
  • #1
hasidim
15
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
  • #2
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:
  • #3
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!
 
  • #4
Bill Simpson,

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


Hi there,

Thank you for reaching out. It sounds like you are trying to solve a second order ODE using NDSolve in Mathematica and want to automate the process for a large number of initial conditions. Fortunately, there are a few ways to do this.

One option is to use a loop to iterate through each set of initial conditions and solve the ODE for each one. For example, you could create a table of your initial conditions and then use a For loop to solve the ODE for each set. Here's an example code:

initialConditions = {{k -> 1, k2 -> 2, X_o -> 3}, {k -> 2, k2 -> 3, X_o -> 4}, {k -> 3, k2 -> 4, X_o -> 5}};

For[i = 1, i <= Length[initialConditions], i++,
sol = NDSolve[{k*X''[t] + k2*X'[t] + X[t] == 0, X[0] == X_o, X'[0] == 0} /. initialConditions[], X[t], {t, 0, tmax}];
Print[sol];
]

This will solve the ODE for each set of initial conditions and print out the solution for each one. You can also save the solutions in a list or table for further analysis.

Another option is to use the "ParametricNDSolve" function, which allows you to specify a set of parameters and solve the ODE for different values of those parameters. Here's an example code:

sol = ParametricNDSolve[{k*X''[t] + k2*X'[t] + X[t] == 0, X[0] == X_o, X'[0] == 0}, X, {t, 0, tmax}, {k, k2, X_o}];

You can then use the "Table" function to solve the ODE for different values of the parameters and plot the solutions. Here's an example code:

Table[Plot[Evaluate[X[k, k2, X_o][t] /. sol], {t, 0, tmax}], {k, 1, 3}, {k2, 2, 4}, {X_o, 3, 5}]

I hope this helps. Let me know if you have any further
 

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

1. What is NDSolve in Mathematica?

NDSolve is a function in Mathematica that is used to numerically solve ordinary differential equations (ODEs) and differential-algebraic equations (DAEs).

2. What is a 2nd order ODE?

A 2nd order ODE is a type of differential equation that involves the second derivative of a function. It can be written in the form y'' = f(x,y,y'). In Mathematica, this can be solved using the NDSolve function.

3. How do I use NDSolve to solve a 2nd order ODE in Mathematica?

To use NDSolve to solve a 2nd order ODE, you need to specify the equation, initial conditions, and any other parameters. The general syntax is: NDSolve[{eqn, y[x0]==y0, y'[x0]==y0'}, y, {x, x0, xf}]. This will return a solution for y[x] over the interval [x0, xf].

4. Can I use a table of initial conditions in NDSolve?

Yes, you can use a table of initial conditions in NDSolve. This is useful when you have multiple initial conditions for the same ODE. The syntax is: NDSolve[{eqn, Table[y[x0[i]]==y0[i], {i,1,n}], Table[y'[x0[i]]==y0'[i], {i,1,n}]}, y, {x, x0, xf}].

5. How can I plot the solution to a 2nd order ODE in Mathematica?

You can use the Plot function in Mathematica to plot the solution to a 2nd order ODE obtained from NDSolve. The general syntax is: Plot[Evaluate[y[x] /. sol], {x, x0, xf}], where "sol" is the solution obtained from NDSolve and x0 and xf are the start and end points of the interval.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
170
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
246
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
292
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
7K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Back
Top