3
$\begingroup$

I have solved an ODE with NDSolve, and put the result in S:

S = NDSolve[{y'[x]==(y[x]^4+y[x]+1)^(1/2),y[0]==0}, y, {x, 0, 100}]

But i don't want to plot y[x] directly. I want to define a new function of y[x] and plot that function. So i tried this:

func1[x_] := (Evaluate[y[x]/.S])^-3;
Plot[func1[x], {x, 0, 100}]

But that doesn't work! I understand why this should be wrong but i don't know what's the correct way to do this.

$\endgroup$
4
  • $\begingroup$ What do you mean by "that doesn't work"? It produces a plot for me. I do get an error about your differential equations, but the basic syntax you have is fine $\endgroup$
    – Jason B.
    Commented Nov 24, 2015 at 9:36
  • $\begingroup$ @JasonB Actually i have simplified my ODE here a little. And you're right, i tested it now and i got a result too. But i still can't get the result from my original ODE. i get an error like this on defining the new function: SetDelayed::write: "Tag Times in (2.27621*10^-27/(y^3))[m_] is Protected. Now at least i know the problem is not with the syntax. thanks. $\endgroup$
    – Mostafa
    Commented Nov 24, 2015 at 9:55
  • $\begingroup$ You forgot to clear previous definitions if you're hitting that error. Look up Clear[]. $\endgroup$ Commented Nov 24, 2015 at 10:04
  • $\begingroup$ @JasonB Oh, thanks a lot. That solved the problem. $\endgroup$
    – Mostafa
    Commented Nov 24, 2015 at 10:28

1 Answer 1

2
$\begingroup$

This works:

 S=NDSolve[{y'[x]==(y[x]^2+y[x]+1)^(1/2),y[0]==0},y,{x,1,8}];
 func1[x_]:=(y[x]/.First[S])^-3;
 Plot[func1[x],{x,1,2.5}]

enter image description here Often times you need to ensure your definition of func1 is only used when x has a numeric value. In that case use

func1[x_?NumericQ] := ...
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.