Plotting with dsolve for ODE

6 views (last 30 days)
Christian Zimmerman
Christian Zimmerman on 20 Jan 2022
Edited: Torsten on 20 Jan 2022
I have created a slope field, now I wish to overlay a single curve for the function that satisfies the equation. below is code so far.
clear
clc
[x,y] = meshgrid(-5:0.4:5,-5:0.4:5);
dydx = x.^2+y.^2-1;
L = sqrt(1+dydx.^2);
quiver(x,y,1./L,dydx./L,'k')
hold on
axis([-3 3 -3 3])
I have attempted to continue like this Unsucccessfuly:
syms y(x)
func = dsolve(diff(y,1) == x^2+y^2-1,y(0) == 0)
fplot(func,[-3 3])
Thank you for your assistance.

Answers (1)

VBBV
VBBV on 20 Jan 2022
clear
clc
[x,y] = meshgrid(-5:0.4:5,-5:0.4:5);
dydx = x.^2+y.^2-1;
L = sqrt(1+dydx.^2);
quiver(x,y,1./L,dydx./L,'k')
hold on
axis([-3 3 -3 3])
syms y(x)
eqn = diff(y,x) == x^2+y-1;
cond = y(0) == 0;
func = dsolve(eqn,cond);
fplot(func,[-3 3],'linewidth',2);
This is close form of solution it can get.
  2 Comments
Torsten
Torsten on 20 Jan 2022
The y-term does not disappear in the ODE because of the initial condition y(0)=0.
If you insert your y-function into the differential equation, you'll see that it is not a solution.
Here is a code to get the correct solution numerically:
fun=@(t,y) t^2+y(1)^2-1;
tspan = [0 -2];
y0 = 0;
[t1 y1] = ode45(fun,tspan,y0);
tspan = [0 2];
y0 = 0;
[t2 y2] = ode45(fun,tspan,y0);
T = vertcat(flipud(t1),t2);
Y = vertcat(flipud(y1),y2);
plot(T,[Y,T.^3/3-T])

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!