dsolve complex explicit answer

조회 수: 11 (최근 30일)
Richard  Nicolaas Meijerink
Richard Nicolaas Meijerink 2018년 2월 11일
댓글: Star Strider 2018년 2월 12일
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.

채택된 답변

Star Strider
Star Strider 2018년 2월 11일
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
Richard  Nicolaas Meijerink
Richard Nicolaas Meijerink 2018년 2월 12일
Ok, I can live with the numerical solution, I guess. Thanks!
Star Strider
Star Strider 2018년 2월 12일
As always, my pleasure.
If my Answer helped you solve your problem, please Accept it!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

Community Treasure Hunt

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

Start Hunting!

Translated by