Message Boards Message Boards

0
|
406 Views
|
4 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to evaluate solutions of a first order ODE system while using NDSolve?

Posted 1 month ago

Hello,

If one solves a single ODE by NDSolve, the following function un[t_] correctly evaluates the numerical solution obtained:

sol0=NDSolve[{u'[t]+u[t]==0,u[0]==1},u[t],{t,0,1}];
un[t_]:=Evaluate[First[u[t]/.sol0]];
un[1/2]
0.606531

My question is: how to define a corresponding function in the case of an ODE system? My following attempt to devise an analogous function failed:

sol=NDSolve[{Subscript[u, 1]'[t]+Subscript[u, 1][t]-Subscript[u, 2][t]==0,Subscript[u, 2]'[t]-Subscript[u, 1][t]+Subscript[u, 2][t]==0,Subscript[u, 1][0]==1,Subscript[u, 2][0]==2},{Subscript[u, 1][t],Subscript[u, 2][t]},{t,0,1}];
Subscript[uns, i][t_]:=Evaluate[sol[[1]][[i]][[2]]];
Part::pkspec1: The expression i cannot be used as a part specification.
Subscript[uns, 1][1/2]

It seems that "i" must be a predefined constant, so how one can access a relevant FunctionInterpolation object corresponding to a variable "i"? Or is there a better alternative?

Lesław

POSTED BY: Leslaw Bieniasz
4 Replies

Off topic. I wonder if there is a difference between

un[t_]:=Evaluate[whatever]

with delayed evaluation un-delayed by Evaluate, and

un[t_]=whatever

with immediate evaluation.

POSTED BY: Gianluca Gorni

If you believe there is no difference, then try my first example (single ODE) without calling "Evaluate". Especially when you want to plot the solution.

It would be nice if one could find some explanation in manuals, or in the online help, why in some cases "Evaluate" is necessary and in some other it is not. In my opinion, the main problem with MATHEMATICA is that almost nothing is explained, with regard to what this program actually does, and why. It would be nice if the producers took this observation into account.

Lesław

POSTED BY: Leslaw Bieniasz

I can't see any difference:

sol0 = NDSolve[{u'[t] + u[t] == 0, u[0] == 1}, u[t], {t, 0, 1}];
un[t_] := Evaluate[First[u[t] /. sol0]]
um[t_] = First[u[t] /. sol0]
Plot[un[t], {t, 0, 1}]
Plot[um[t], {t, 0, 1}]

Notice that in the definition of um I used immediate assignment. This is even shorter:

up = NDSolveValue[{u'[t] + u[t] == 0, u[0] == 1}, u, {t, 0, 1}]
Plot[{up[t], up'[t]}, {t, 0, 1}]

The order of evaluation in Mathematica can be confusing. I am always curious about some trick that I didn't know.

POSTED BY: Gianluca Gorni
Posted 1 month ago

Leslaw Bieniasz, in the RHS of

Subscript[uns, i][t_]:=Evaluate[sol[[1]][[i]][[2]]];

i is variable, but in the LHS it is part of name of variable.
You could do smth like this:

With[{fs = First@sol},
 (Subscript[uns, #][t] := Evaluate[Subscript[u, #][t] /. fs[[#]]]) & /@
   Range@Length@fs]
POSTED BY: Denis Ivanov
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract