# Problems with Plotting a Solution Curve to a Differential Equation

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

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

• 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] Nov 5, 2020 at 0:50
• @cvgmt I'm not quite sure what you're saying? Nov 5, 2020 at 1:29
• 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]] Nov 5, 2020 at 2:10
• @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. Nov 5, 2020 at 2:25
• @SharkyKesa Why do you assume the shape of the curve to be wrong? Nov 5, 2020 at 8:02

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 @@ FunctionPropertiesSingularities[
y[x] /. gensol[-1], {x}, {"BRANCHCUTS", "DEFCUTS", "POLES",
"ESSENTIAL", "IGNORE", "PWMINMAX"}]]
`