Need help plotting a directional field, along with some specific solutions(on the same graph)

I am trying to plot a directional field for the differential equation dY = y(4-y) with specific solutions at y(0) = -2, y(0) = 2 & y(0) = 6; I can plot the directional field but I do not know how to add in my specific solution curves to the plot. Been racking my brain on this all day so any help would be awesome!
Here is my current code:
>> clear
>> de = 'Dy = y*(4-y)';
>> y1 = dsolve(de, 'y(0)=-2','x')
y1 =
-4/(exp(log(3) - 4*x) - 1)
>> y2 = dsolve(de, 'y(0)=2','x')
y2 =
4/(exp(-4*x) + 1)
>> y3 = dsolve(de, 'y(0)=6','x')
y3 =
-4/(exp(- 4*x - log(3)) - 1)
>> [x y]=meshgrid(-2:0.2:2,-4:0.2:8);
>> dY=y.*(4-y);
>> dT=ones(size(dY));
>> L=sqrt(1+dY.^2);
>> quiver(x, y, dT./L, dY./L, 0.5), axis tight; hold on
>> contour(x,y,y1)
Error using contour (line 46) Input arguments must be numeric or objects which can be converted to double.
>> plot(x,y1)
Error using plot Data must be numeric, datetime, duration or an array convertible to double.
>> contour(x,y1)
Error using contour (line 46) Input arguments must be numeric or objects which can be converted to double.

Answers (1)

Hi,
if you type:
whos y1
you will see the reason for the error message. y1 is a sym object and therefore can not be plotted.
Use matlabFunction to create a function handle from symbolic equation. With this you can calculate values for your Problem and this can be uses to plot. For example:
y1_fun = matlabFunction(y1)
Best regards
Stephan

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 16 Sep 2018

Edited:

on 17 Sep 2018

Community Treasure Hunt

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

Start Hunting!