Mathematica NDSolve in matrix system with Mathematica

AI Thread Summary
The discussion focuses on solving a system of ordinary differential equations (ODEs) using Mathematica, specifically the equation dx/dt = -M x, where x is an array and M is a matrix. The user initially struggles to extract specific values from the solution generated by NDSolve, receiving only the input expression instead of numerical results. Another participant provides corrected syntax and guidance on how to properly define variables and initial conditions in Mathematica. Ultimately, the user successfully learns to apply Mathematica's rules to obtain numerical values from the solution. The exchange highlights the importance of understanding syntax and function definitions in Mathematica for solving complex mathematical problems.
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
Views
526
Replies
1
Views
2K
Replies
1
Views
2K
Replies
4
Views
3K
Replies
8
Views
3K
Replies
1
Views
1K
Replies
6
Views
2K
Back
Top