Plotting trajectories of a system of equations.

Hi all,
Im writing a project and have been told that MatLab is the best way to visualise what is happening, however i am very new to matlab.
the two equations I wish to plot are simple, dx/dt = ax and dy\dt = -y. I want to vary a and then see how the phase portrait changes by plotting some trajectories and showing how the fixed point at the origin changes according to the value of a. I have solved the system having no problems but them when it comes to plotting anything i am having difficulties.
The code i have so far is:
syms x(t) y(t);
a=1;
A = [a 0; 0 1];
Z = [x;y];
odes = diff(Z) == A*Z
[xSol(t), ySol(t)] = dsolve(odes);
xSol(t) = simplify(xSol(t))
ySol(t) = simplify(ySol(t))
xdom = linspace(-10,10,100);
ydom = linspace(-10,10,100);
U = a.*x;
V = -1*y;
[X,Y] = meshgrid(xdom,ydom);
quiver(X,Y,U,V)
% this returns an error saying unable to conver expression into double array

 Accepted Answer

First, use the matlabFunction (link) function to create anonymous functions (or function files) from your symbolic expressions. Second, use the ‘X’ and ‘Y’ matrices to calculate ‘U’ and ‘V’.
Other than that, you did everything correctly.
Try this:
syms x(t) y(t);
a=1;
A = [a 0; 0 1];
Z = [x;y];
odes = diff(Z) == A*Z
[xSol(t), ySol(t)] = dsolve(odes);
xSol(t) = simplify(xSol(t))
ySol(t) = simplify(ySol(t))
xfcn = matlabFunction(xSol) % Create Anonymous Function From Symbolic Function
yfcn = matlabFunction(ySol) % Create Anonymous Function From Symbolic Function
xdom = linspace(-10,10,100);
ydom = linspace(-10,10,100);
[X,Y] = meshgrid(xdom,ydom);
U = a.*X; % Use ‘X’ To Calculate Derivatives
V = -1*Y; % Use ‘Y’ To Calculate Derivatives
quiver(X,Y,U,V,5) % Scale Arrows At 5
axis([-1 1 -1 1]) % Zoom In To View Details

2 Comments

That worked perfectly, thank you!
My pleasure.
If my Answer helped you solve your problem, please Accept it!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!