3

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.

Mostafa
  • 33
  • 1
  • 3
  • 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 – Jason B. Nov 24 '15 at 09:36
  • @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. – Mostafa Nov 24 '15 at 09:55
  • You forgot to clear previous definitions if you're hitting that error. Look up Clear[]. – J. M.'s missing motivation Nov 24 '15 at 10:04
  • @JasonB Oh, thanks a lot. That solved the problem. – Mostafa Nov 24 '15 at 10:28

1 Answers1

2

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] := ...
Ted Ersek
  • 7,124
  • 19
  • 41