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

Click For Summary

Discussion Overview

The discussion revolves around solving a second-order ordinary differential equation (ODE) using Mathematica's NDSolve function. Participants explore methods to automate the process of solving the ODE for multiple sets of initial conditions and corresponding coefficients, rather than solving them individually.

Discussion Character

  • Technical explanation, Homework-related

Main Points Raised

  • One participant describes the challenge of solving a second-order ODE with varying initial conditions and constant coefficients, seeking a more efficient method than solving each set individually.
  • Another participant provides a code snippet that utilizes a For loop and the Reap/Sow functions to automate the process of solving the ODE for multiple sets of values.
  • A clarification is made regarding the use of variable names, specifically the distinction between 'Xo' and 'X_o' in Mathematica.
  • A later reply confirms that the provided solution worked well for the original poster's problem, indicating a successful application of the suggested method.

Areas of Agreement / Disagreement

Participants appear to agree on the effectiveness of the proposed solution for automating the ODE solving process, with no significant disagreement noted.

Contextual Notes

Participants mention the use of specific Mathematica functions (Reap and Sow) that may be unfamiliar to some users, indicating a potential learning curve for those new to these functions.

Who May Find This Useful

This discussion may be useful for Mathematica users dealing with differential equations, particularly those looking to automate repetitive tasks involving multiple sets of parameters and initial conditions.

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 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
Replies
8
Views
4K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 3 ·
Replies
3
Views
2K