Message Boards Message Boards

0
|
4696 Views
|
7 Replies
|
0 Total Likes
View groups...
Share
Share this post:

How to solve this differential equation?

Posted 9 years ago

Hi,

I'm pasting a code of a DE with scaling parameter. I'm getting error. Plz help.

Clear[x];
r=1;
eqn=x'[t]-1-r*x[t]-(x^2)[t];sol=NDSolve[{eqn,x[0]==0},x,{t,0,100}][[1]];
tTicks=Range[-24,24 30,24];
tGrid=Range[-60,24 30,6];

ParametricPlot[Evaluate[{x[t],x'[t]}/.sol],{t,0,100},Frame->True,FrameTicks->{tTicks,Automatic},FrameTicksStyle->Directive[Red,Thick],GridLines->{tGrid,Automatic},GridLinesStyle->LightGray,FrameLabel->(Style[#,14,Bold]&/@{x,Overscript[x,"."]}),AspectRatio->1]

ParametricPlot[Evaluate[{t,x[t]/.sol}],{t,0,50},Frame->True,FrameTicks->{Range[0,50,12],Automatic},FrameTicksStyle->Directive[Red,Thick],GridLines->{tGrid,Automatic},GridLinesStyle->LightGray,FrameLabel->(Style[#,14,Bold]&/@{t,x}),AspectRatio->1]

Sincerely

POSTED BY: Rahul Chakrabory
7 Replies
Posted 9 years ago

The first thing to understand from a differential equations class is that most differential equations do not have a closed form solution. DSolve looks for closed form exact solutions, at least if you do not insert any decimal points, and I urge you to not do that because it appears to change many of the rules in hidden and complicated ways.

By changing x'[t]-1-rx[t]-x[t]^2==0 to x'[t]-rx[t]-x[t]*e^x[t]==0 you have replaced an equation which did have a simple closed form solution with one that appears does not, or at least does not have one that could be found in the time I was willing to wait, and I have little patience to wait.

The second thing to understand from a differential equations class is that numerical solutions to differential equations present a wide variety of ways to get incorrect results. You must be very careful to avoid getting meaningless results without knowing it.

When I then try

sols = Table[x /. NDSolve[{x'[t] - r*x[t] - x[t]*e^x[t] == 0, x[0] == 0}, x, {t, 0, 100}][[1, 1]], {r, 1, 3}] ;
Show[Table[ParametricPlot[Evaluate[{sols[[r]][t], D[sols[[r]][t], t]}], {t, 0, 100}], {r, 1, 3}]]

I seem to get an empty plot. When I seem to get an empty plot I ALWAYS FIRST substitute Table for Plot so that I can look at the numbers.

In[2]:= Table[Evaluate[{sols[[1]][t], D[sols[[1]][t], t]}], {t, 0, 100}]

Out[2]= {{0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.},
  {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.},
  {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.},
  {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.},
  {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.},
  {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.},
  {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.},
  {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.},
  {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.}, {0., 0.},
  {0., 0.}, {0., 0.}, {0., 0.}}

When I inspect your DE I realize that x[0]==0 is a valid solution, the plot was actually correct and was just being hidden by the axis.

So it appears that NDSolve did find an approximately correct numerical solution to your DE. Why DSolve did not immediately find this might be a question to think about later.

Hopefully you are learning some of the skills to be able to solve DE using Mathematica. There is an old book that has this as the only subject. You might be able to get an old used inexpensive copy mailed from a bookstore. I have meant to buy a copy and your examples have pushed me into doing this. Thank you.

POSTED BY: Bill Simpson
Posted 9 years ago

I am not certain I understand what you wish to plot. Perhaps this will help

In[1]:= sols = Table[x /. DSolve[{x'[t] - 1 - r*x[t] - x[t]^2 == 0, x[0] == 0}, x, t][[1, 1]], {r, 1, 3}]

During evaluation of In[1]:= Solve::ifun: Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information. >>

During evaluation of In[1]:= Solve::ifun: Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information. >>

Out[1]= {
 Function[{t}, 1/2 (-1 + Sqrt[3] Tan[1/2 (\[Pi]/3 + Sqrt[3] t)])], 
 Function[{t}, -(t/(-1 + t))], 
 Function[{t}, -(((-3 + Sqrt[5]) (-1 + E^(Sqrt[5] t)))/(2 - 7 E^(Sqrt[5] t) + 3 Sqrt[5] E^(Sqrt[5] t)))]
}

Then perhaps you can do something like this

Table[ParametricPlot[Evaluate[{sols[[r]][t], D[sols[[r]][t], t]}], {t, 0, 100}], {r, 1, 3}]

or perhaps something like this

Show[Table[ParametricPlot[Evaluate[{sols[[r]][t], D[sols[[r]][t], t]}], {t, 0, 100}], {r, 1, 3}]]

I am uncertain whether these plots are precisely correct and you should study all these results very carefully to make certain that there are no errors.

POSTED BY: Bill Simpson

Hi Bill,

The above code has helped me immensely. Thanks. But when I change the equation, like the following one it does not work.

sols=Table[x/.DSolve[{x'[t]-r*x[t]-x[t]*e^x[t]==0,x[0]==0},x,t][[1,1]],{r,1,3}] Show[Table[ParametricPlot[Evaluate[{sols[[r]][t],D[sols[[r]][t],t]}],{t,0,100}],{r,1,3}]]

regards,

POSTED BY: Rahul Chakrabory

Dear Bill,

For more then one curve in a single plot , for different values of r , (r=1,2,3....) , how shall I code it?

Regards,

POSTED BY: Rahul Chakrabory
Posted 9 years ago

You might want to be cautious with your numerical solution.

Clear[x]; r = 1; eqn = x'[t] - 1 - r*x[t] - x[t]^2 == 0;
sol = x /. NDSolve[{eqn, x[0] == 0}, x, {t, 0, 100}][[1, 1]];

NDSolve::ndsz: At t == 1.2091994106422708`, step size is effectively zero; singularity or stiff system suspected. >>

And

ParametricPlot[Evaluate[{sol[t], D[sol[t], t]}], {t, 0, 100}]

enter image description here

And

Plot[sol[t], {t, 0, 100}]

enter image description here

But

Clear[x]; r = 1; eqn = x'[t] - 1 - r*x[t] - x[t]^2 == 0;
sol = x /. DSolve[{eqn, x[0] == 0}, x, t][[1, 1]];

Solve::ifun: Inverse functions are being used by Solve, so some solutions may not be found; use Reduce for complete solution information. >>

And

ParametricPlot[Evaluate[{sol[t], D[sol[t], t]}], {t, 0, 100}]

enter image description here

And

Plot[sol[t], {t, 0, 100}]

enter image description here

And

In[11]:= sol

Out[11]= Function[{t}, 1/2 (-1 + Sqrt[3] Tan[1/6 (\[Pi] + 3 Sqrt[3] t)])]

Those warning messages are often there for a very good reason.

Even the warning about using Reduce is there for a good reason. Unfortunately it does not include a link to, and I have never been able to track down, a brilliantly well written paragraph in the documentation that describes exactly what that intends you to do.

POSTED BY: Bill Simpson

Sir,

I'm not getting any output. I did the correction as suggested by you.

here is the code.

Clear[x]; r=1; eqn=x'[t]-1-r*x[t]-x[t]^2==0; sol=NDSolve[{eqn,x[0]==0},x,{t,0,100}][[1]]; tTicks=Range[-24,24 30,24]; tGrid=Range[-60,24 30,6];

ParametricPlot[Evaluate[{x[t],x'[t]}/.sol],{t,0,100},Frame->True,FrameTicks->{tTicks,Automatic},FrameTicksStyle->Directive[Red,Thick],GridLines->{tGrid,Automatic},GridLinesStyle->LightGray,FrameLabel->(Style[#,14,Bold]&/@{x,Overscript[x,"."]}),AspectRatio->1]

ParametricPlot[Evaluate[{t,x[t]/.sol}],{t,0,50},Frame->True,FrameTicks->{Range[0,50,12],Automatic},FrameTicksStyle->Directive[Red,Thick],GridLines->{tGrid,Automatic},GridLinesStyle->LightGray,FrameLabel->(Style[#,14,Bold]&/@{t,x}),AspectRatio->1]

Error are:

NDSolve::dvnoarg: The function x appears with no arguments. >> ReplaceAll::reps: {-x[t]^2+(x^2)[t]==0,x[0]==0} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >> ReplaceAll::reps: {-x[0.00204082]^2+(x^2)[0.00204082]==0,x[0]==0} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >> ReplaceAll::reps: {-1. x[0.00204082]^2+(x^2)[0.00204082]==0.,x[0.]==0.} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >> General::stop: Further output of ReplaceAll::reps will be suppressed during this calculation. >>

Regards,

POSTED BY: Rahul Chakrabory
Your equation is malformed. This variation works:
eqn = x'[t] - 1 - r*x[t] - x[t]^2 == 0
POSTED BY: Gianluca Gorni
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