2
$\begingroup$

I do not have enough reputation to comment on other questions, hence I have to ask my own one

I want to solve the following nonlinear first order ODE,
$$(1 + x) (0.5 y[x]^{-0.5} (x - y[x])^{0.5} - 0.5 y[x]^{0.5} (x - y[x])^{-0.5}) == -y[x] (x - y[x])/y'[x]$$ As it does not have a closed form solution, I want to plot the directional field and pick a particular line corresponding to an initial value.

Here is my code,

f[x_] = {
  {NDSolveValue[{(1 + x) (0.5 y[x]^-0.5 (x - y[x])^0.5 - 
         0.5 y[x]^0.5 (x - y[x])^-0.5) == -y[x] (x - y[x])/y'[x], 
     y[1] == 0.5}, y[x], {x, 0, 2}]},
  {\[Placeholder]}
 }

But I get errors:

Power::infy: Infinite expression 1/0. encountered. >>
NDSolveValue::ndnum: Encountered non-numerical value for a derivative at x == 1.`. >>

I think I have seen others with similar functional form (potentially 1/0) plot their directional fields, is there anyway to get around here?

If I can do this first step(solving the ODE numerically), then I can plot it,

g1=Plot[f[x], {x, 0, 2},
   PlotStyle -> Directive[Thick, Red], 
   PlotRange -> {{-0.999, 2}, Automatic}, 
   PlotPoints -> 100
 ] 

then do the streamplot as it is called,

  g2=StreamPlot[{1, (-y[x] (x - y[x]))/((1 + x)[
      0.5 y[x]^-0.5 (x - y[x])^0.5 - 0.5 y[x]^0.5 (x - y[x])^-0.5])}, {x, 0, 2}, {y, 0, 1}]

and then finally showing them both

 Show[g1,g2]

The code above is what I modified from other's, his code does works. I changed the function into mine, and changed the range of x, that is all. Can someone help me figure it out?

The directional field should looks like this, but I want to highlight a red curve corresponding to a initial condition.

enter image description here

Also, can I add lines like y=x and y=x/2 on this plot?

Thanks in advance!

$\endgroup$
0
$\begingroup$

Only a partial solution.NDSolve can't solve with this initial conditions y(1) = 0.5

sol = NDSolve[{(1 + x)*(0.5*y[x]^(-0.5)*(x - y[x])^0.5 - 
0.5*y[x]^0.5*(x - y[x])^(-0.5)) == -y[x]*(x - y[x])/y'[x], 
y[0.99] == 0.5}, y, {x, 0, 2}, AccuracyGoal -> 30, 
PrecisionGoal -> 30, WorkingPrecision -> 55] // Quiet; 
ODEequations[x_] := y[x] /. sol;
g1 = Plot[{x, x^2, ODEequations[x]}, {x, 0, 2}, PlotRange -> {0, 2}, 
PlotLegends -> "Expressions"] // Quiet;
g2 = StreamPlot[{1, (-y*(x - y))/((1 + x)*(0.5*y^(-0.5)*(x - y)^0.5 - 
0.5*y^0.5 *(x - y)^(-0.5)))}, {x, 0, 2}, {y, 0, 1}] // Quiet;
Show[g1, g2]

enter image description here

The directional field looks another like yours. I can not explain this. Such a solution better than none :)

$\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.