dsolve complex explicit answer
12 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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 个评论
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!




