NDSolve in matrix system with Mathematica

Click For Summary

Discussion Overview

The discussion revolves around solving a system of ordinary differential equations (ODEs) using Mathematica's NDSolve function. Participants explore the formulation of the problem, the correct syntax for defining variables and equations, and methods for extracting specific numerical values from the solution.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant describes an attempt to solve a set of N ODEs represented by the equation dx/dt = -M x, using a specific initial condition and syntax in Mathematica.
  • Another participant points out syntax errors in the original code, suggesting corrections such as avoiding the use of 'N' as a variable name and providing a clearer structure for defining the equations and initial conditions.
  • A later reply discusses the difficulty in extracting specific values from the solution, noting that attempts to use substitution yield only the original expression without numerical evaluation.
  • One participant proposes a method to define a function that allows for the extraction of numerical values from the solution, demonstrating how to implement this in Mathematica.
  • Subsequent responses indicate that the proposed solution successfully resolves the participant's issue with obtaining specific values.

Areas of Agreement / Disagreement

Participants generally agree on the need for correct syntax and the use of Mathematica's rules for variable substitution. However, there is no explicit consensus on the best approach to extract values until the final suggestion is accepted.

Contextual Notes

Limitations include the initial confusion over variable naming conventions and the specific syntax required for Mathematica, which may affect the clarity of the discussion for those unfamiliar with the software.

Who May Find This Useful

Users of Mathematica looking to solve systems of ODEs and extract numerical results, particularly those who may be new to the software or encountering similar syntax issues.

jatorre
Messages
3
Reaction score
0
Hello everyone.

This is my first post after looking for information about a simple question. Unfortunately I didn't found information in any other posts in this forum so, here we go!

I'm trying to solve numerically a set of N ODEs with Mathematica 7.0. Let me call x(t) an array of N elements and M a NxN matrix. I would like to solve the problem dx/dt = - M x.

So I firstly define the array:

Code:
X[t_] = Table[xi[i, t], {i, N}];

with a "pseudo" Dirac Delta initial condition:

Code:
X0 = SparseArray[Table[{i} -> 0, {i, N}]];
X0[[N/2]] = 1;

This system can be solved with NDSolve:

Code:
eqns = Thread /@ {X'[t] == -M.X[t], X[0] == X0};
sol = NDSolve[eqns, X[t], {t, 0, 1}];

If I plot the solution I can check the correct answer:

Code:
Plot[Evaluate[Table[xi[i, t] /. sol, {i, N}]], {t, 0, 1}]

And here comes my question. How to obtain a specific value of xi (e.g., xi[1,0.1])?

I tried to do it using directly
Code:
xi[1, 0.1] /. sol
but the answer was simply
Code:
{xi[1, 0.1]}
. Using Flatten I obtain the same result (without the braces) and if I try with
Code:
X[0.1]/.sol
I obtain an array of elements xi[i,0.1] with i=1,N.

At this point I have no idea of how to deal with this. I am quite sure that this has to be a silly question but, as far as I know, I cannot obtain the correct answer.

Thank you very much in advanced and my apologies if this is a duplicated question.


Arturo.
 
Physics news on Phys.org
Your syntax is wrong in many ways and you can't use N as a variable name since it's Mathematica's standard function. Here is your code correctly rewrited:

e.g.
Code:
n = 4;

Code:
X[t_] = Table[xi[i, t], {i, 1, n}];

Code:
X0 = Table[0, {n}];
X0[[Round[n/2]]] = 1;

e.g.
Code:
M = {{1, 2, 0, 0}, {0, 2, 3, 0}, {0, 0, 1, 2}, {1, 0, 0, 1}};

Code:
diffEq = Table[X'[t][[i]] == -(M.X[t])[[i]], {i, 1, n}];
initialCond = Table[X[0][[i]] == X0[[i]], {i, 1, n}];
Code:
sol = NDSolve[{Join[diffEq, initialCond]}, X[t], {t, 0, 1}]

And you can plot solutions with:
Code:
Plot[Evaluate[Table[xi[i, t] /. sol, {i, 1, n}]], {t, 0, 1}]

208te2h.jpg
 
Last edited:
Dear djelovin,

thank you very much for the quick answer. I chose very bad names for the example. The syntax you propose is indeed much clear than mine, thanks :)

Anyway, I still cannot print a value for xi (eg., xi[2,0.1]). The plot is already done and correctly shown, but if I try to print a value Mathematica always returns the input. I tried:

Code:
xi[2,0.1]/.sol
Evaluate[xi[2,0.1]/.sol
Evaluate[xi[2,0.1]/.Flatten[sol]
Evaluate[X[0.1][[2]]/.sol
Evaluate[X[0.1][[2]]/.sol[[2]]
but I always obtain
Code:
xi[2,0.1]
or
Code:
{xi[2,0.1]}

I already took a look to
Code:
InputForm[sol]
but all I obtained was what seems to be a nested array in which I am not able to recover any specific data.

Thanks again for your attention :)

Regards,
Arturo.
 
You have to use mathematica rules for t variable too. If you won't function that returns numerical value, you can define if like this:
Code:
x[2, x_] := (xi[2, t] /. sol /. t -> x)[[1]]
In my example
Code:
x[2, 0.1]
returns
0.818774
 
Dear djelovin,

Thank you for the clarification. Now it works perfectly :) After reading the documentation about rules, I now understand how they work and how to obtain (as in your example) numerical values.

Regards,
Arturo.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
8
Views
4K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K