Message Boards Message Boards

0
|
23990 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:
GROUPS:

How to remove curly brackets from NDSolve/Evaluate output

Posted 10 years ago
s=NDSolve[{y'[x]==y[x]Cos[x+y]],y[0]0==1},y,{x,0,30}]
{{y-> InterpolatingFunction[{{0.,30.}},<>]}}
f[x_]:=Evaluate[y[x] /. s]
f[1]
{0.991387}
Dear All,

I am trying to get the output of a numerical solution to a differential euqation as shown in the example above. As usual I defined the function f using the Evaluate function to get the outpout from the resulting interpolating function. I notice that when substiuting a numerical value for the indepdenent variable, the output is surrounded by a curly bracket suggesting that the output is a list.
Although the presence of the curly brackets doesn't seem to affect functions like Plot[f, {x,0,30}], I notice that it is causing me some trouble when I try to use output of the function f in other computations like in parametric plot or in peicewise functions. Please advise how to remove the curly brackets from this output?
Thanks,
Abdullah

POSTED BY: Abdullah Alkudsi
3 Replies

use

f[x_]:=Evaluate[y[x] /. s[[1]]]

instead

POSTED BY: Guilherme Correa
Dear Abdullah,

I am not sure whether this answers your question, because your input does not seem to be valid Mathematica input. I changed it a bit to make it work on my comptuer.
s = NDSolve[{y'[x] == y[x] Cos[x + y[x]], y[0] == 1},
  y[x], {x, 0, 30}]

If I do this and then useĀ 
f[x_] := Evaluate[y[x] /. s]
f[1]
I get your output.
{0.991387}

As you see this is a list of one element. If you only want that element you need to ask Mathematica for element one of that list.
f[1][[1]]

This gives 0.991387 without the curly brackets.

M.
POSTED BY: Marco Thiel
Thank you very much. That was helpful
POSTED BY: Abdullah Alkudsi
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