2
$\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$
  • $\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. Nov 24 '15 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 Nov 24 '15 at 9:55
  • $\begingroup$ You forgot to clear previous definitions if you're hitting that error. Look up Clear[]. $\endgroup$ – J. M.'s technical difficulties Nov 24 '15 at 10:04
  • $\begingroup$ @JasonB Oh, thanks a lot. That solved the problem. $\endgroup$ – Mostafa Nov 24 '15 at 10:28
1
$\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] := ...
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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