1
$\begingroup$

I was using Mathematica to plot various solution curves to differential equations, and graphing them simultaneously. However, I came across a problem when graphing $\frac{\mathrm{d}y}{\mathrm{d}x} = x^2 + y^2$ with $y(0) = -1$. Using the following code,

gensol[y0_] = DSolve[{y'[x] == x^2 + y[x]^2, y[0] == y0}, y[x], x];
Plot[y[x] /. gensol[-1], {x, -2, 2}, PlotRange -> {{-5, 5}, {-5, 5}}]

I get the following image enter image description here

Clearly, this is not the actual shape of the curve, so I was wondering how I would fix this.

$\endgroup$
8
  • $\begingroup$ seems the same. sol = DSolve[{y'[x] == x^2 + y[x]^2}, y[x], x]; Plot[y[x] /. sol /. C[1] -> 5, {x, -2, 2}, PlotRange -> {{-5, 5}, {-5, 5}}, ExclusionsStyle -> Dashed, Exclusions -> Automatic] $\endgroup$
    – cvgmt
    Nov 5, 2020 at 0:50
  • $\begingroup$ @cvgmt I'm not quite sure what you're saying? $\endgroup$ Nov 5, 2020 at 1:29
  • $\begingroup$ Consider FunctionDomain[y[x] /. gensol[-1], x] and see if this is better for you: Plot[y[x] /. gensol[-1], {x, -2, 2}, PlotRange -> {{-5, 5}, {-5, 5}}, Exclusions -> List @@ Simplify@Not@FunctionDomain[y[x] /. gensol[-1], x]] $\endgroup$
    – Michael E2
    Nov 5, 2020 at 2:10
  • $\begingroup$ @MichaelE2 The graph now changes to not have the line between (0,1) and (0,-1), but the rest of the graph stays the same as above. The required graph should just be a smooth curve. $\endgroup$ Nov 5, 2020 at 2:25
  • 1
    $\begingroup$ @SharkyKesa Why do you assume the shape of the curve to be wrong? $\endgroup$ Nov 5, 2020 at 8:02

1 Answer 1

1
$\begingroup$

This gives a solution to the IVP continuous in a neighborhood of the initial condition:

gensol[y0_] = {y -> Function[x, Piecewise[{
       {DSolveValue[{y'[x] == x^2 + y[x]^2, y[0] == y0}, y[x], x, 
         Assumptions -> x > 0], x > 0},
       {DSolveValue[{y'[x] == x^2 + y[x]^2, y[0] == y0}, y[x], x, 
         Assumptions -> x < 0], x < 0}
       }, y0] // Evaluate]};

Plot[y[x] /. gensol[-1], {x, -5, 5},
 Exclusions -> 
  Join @@ FunctionProperties`Singularities[
    y[x] /. gensol[-1], {x}, {"BRANCHCUTS", "DEFCUTS", "POLES", 
     "ESSENTIAL", "IGNORE", "PWMINMAX"}]]

enter image description here

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