Group Abstract Group Abstract

Message Boards Message Boards

Assign values to an NDSolve inside a For loop

Posted 5 years ago
Attachments:
POSTED BY: Agapi Pop
2 Replies
Posted 5 years ago

I cannot thank you enough. Your coding proposal is so elegant. Thank you again!

POSTED BY: Agapi Pop

Hello Agapi Pop,

to make your code run you need to rewrite the second line like so:

t = First[p /. Solve[p - 2 i == 0]];

This is, because here the result of Solve is a List containing one element, and you want that element only.

But - if I may add - a more Mathematica like coding would read:

t = Flatten@Table[p /. Solve[p - 2 i == 0, p], {i, 1, 4}];
s = NDSolveValue[{y'[x] == #*y[x] Cos[x + y[x]], y[0] == 1}, y, {x, 0, 30}] & /@ t;
Plot[Evaluate@Through[s[x]], {x, 0, 30}, PlotRange -> All]
(* or likewise: *)
Plot[#[x], {x, 0, 30}, PlotRange -> All] & /@ s

enter image description here

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard