Message Boards Message Boards

[✓] Define a function using NDSolve?

GROUPS:

Hello, colleagues

I am making a simple code.

rhs[x_, y_, om_] := f2[x, y, om]/x/f1[y];
psr[ro_, om_, a_] :=  NDSolve[{f'[t] - rhs[t, f[t], om] == 0, f[a] == 1}, f, {t, a, 1}][[1]][[1]][[2]][ro];

rhs is defined based on the functions of f1 and f2.

An ODE, f ' [t] == rhs[t, f [t], om ] , is solved using NDSolve, then its calcuation result defines a new function, psr[ro_,om_,a_].

My question is what the role of [[1]][[1]][[2]][ro] at the end of psr. I just guess it may be related to the positions and dimensions of variables of psr.

Thanks for the answers in advance.

POSTED BY: Richard Edwin
Answer
18 days ago

The double brackets [[..]] mean Part and the single brackets [ro] are part of the syntax for a function call. See also the tutorial Parts of Expressions.

Breaking down the components of a computation can be an effective way to figure how code works, especially when the output is small enough to fit on a screen. To see the effect of the brackets, one might execute each of the lines below and note how Part affects the output:

NDSolve[{y''[x] + y[x] == 0, y[0] == 1, y'[0] == 0}, y, {x, 0, 10}]
NDSolve[{y''[x] + y[x] == 0, y[0] == 1, y'[0] == 0}, y, {x, 0, 10}][[1]]
NDSolve[{y''[x] + y[x] == 0, y[0] == 1, y'[0] == 0}, y, {x, 0, 10}][[1]][[1]]
NDSolve[{y''[x] + y[x] == 0, y[0] == 1, y'[0] == 0}, y, {x, 0, 10}][[1]][[1]][[2]]
NDSolve[{y''[x] + y[x] == 0, y[0] == 1, y'[0] == 0}, y, {x, 0, 10}][[1]][[1]][[2]][ro]

One can combine the Part calls into one:

NDSolve[{y''[x] + y[x] == 0, y[0] == 1, y'[0] == 0}, y, {x, 0, 10}][[1, 1, 2]][ro]

One can use the shortcut NDSolveValue, introduced in V9, to get the same output (note the y[ro] inside):

NDSolveValue[{y''[x] + y[x] == 0, y[0] == 1, y'[0] == 0}, y[ro], {x, 0, 10}]

And one can use the old-school form of ReplaceAll and First, frequently found in the documentation:

y[ro] /. First@NDSolve[{y''[x] + y[x] == 0, y[0] == 1, y'[0] == 0}, y, {x, 0, 10}]

Finally, you might want to address a potential bug in your definition of psr. It contains the global symbol f and depends on f being undefined. If somehow f gets defined, it will mess up psr. Localizing the symbol f with Module will protect it.

psr[ro, om, a_] := Module[{f},
   NDSolveValue[{ f ' [ t ] - rhs[ t, f [ t ], om ] == 0, f [ a ] == 1}, f[ro], {t, a, 1}]
  ];
POSTED BY: Michael Rogers
Answer
17 days ago

Thank you so much for the perfect answer !!!

I am sure your advice will improve my performace to code.

Thank you again.

POSTED BY: Richard Edwin
Answer
17 days ago

Me too I have not see this function....

POSTED BY: Agate Lancon
Answer
17 days ago

Dear @Richard Edwin, welcome to Wolfram Community! Please make sure you know the rules: https://wolfr.am/READ-1ST

The rules explain how to format your code properly. If you do not format code, it may become corrupted and useless to other members. Please EDIT your posts and make sure code blocks start on a new paragraph and look framed and colored like this.

int = Integrate[1/(x^3 - 1), x];
Map[Framed, int, Infinity]

enter image description here

POSTED BY: Moderation Team
Answer
17 days ago

I am sorry. The problem you pointed out is just corrected.

Thank you for the comment.

POSTED BY: Richard Edwin
Answer
17 days ago

Group Abstract Group Abstract