dsolve complex explicit answer
12 views (last 30 days)
Show older comments
Richard Nicolaas Meijerink
on 11 Feb 2018
Commented: Star Strider
on 12 Feb 2018
I'm trying to plot some level curves from the differential equation dy/dx=-(x^2-x)/(y^2-2y) using dsolve. By hand I get the implicit solution (1/3)*y^3 -y^2 = -(1/3)*x^3+(1/2)*x^2+c, wich not even my HP50g finds an explicit solution.
With MATLAB I find an explicit, complex answer, but that way fplot nor ezplot are able to plot the curves
here's the code I wrote
%%%%
syms y(x) x
eqn = diff(y) == (-x^2+x)/(y^2-2*y)
y0 = [-3 -2 -1 1 2 3]
for k=1:length(y0)
cond = y(0) == y0(k)
sol = dsolve(eqn,cond)
ezplot(sol)
hold on
end
I've been able to plot it with ode15s, but it doesn't give a smooth curve, since it only plots the solution interval containing the initial condition. Also tried plotting fplot(real(sol)), but at the vertical asymptotes, the function looks kind of mirrored.
0 Comments
Accepted Answer
Star Strider
on 11 Feb 2018
Try this:
syms y(x) x
eqn = diff(y) == (-x^2+x)/(y^2-2*y);
y0 = [-3 -2 -1 1 2 3];
for k = 1:length(y0)
cond = y(0) == y0(k);
sol{k} = dsolve(eqn,cond);
af{k} = matlabFunction(sol{k});
end
cm = colormap(jet(numel(y0)));
axh = axes('NextPlot','Add');
x = linspace(-2*pi, pi, 150);
for k = 1:numel(af)
fcn = af{k};
plot(x, real(fcn(x)),'-', 'Color',cm(k,:))
plot(x, imag(fcn(x)),'--', 'Color',cm(k,:))
grid
end
It creates anonymous functions from your ‘sol’ results, then uses them to plot the real and imaginary parts in a separate loop.
8 Comments
Star Strider
on 12 Feb 2018
As always, my pleasure.
If my Answer helped you solve your problem, please Accept it!
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



