Plotting trajectories of a system of equations.

15 ビュー (過去 30 日間)
luke hunt
luke hunt 2019 年 5 月 22 日
コメント済み: Star Strider 2019 年 5 月 25 日
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

採用された回答

Star Strider
Star Strider 2019 年 5 月 22 日
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 件のコメント
luke hunt
luke hunt 2019 年 5 月 25 日
That worked perfectly, thank you!
Star Strider
Star Strider 2019 年 5 月 25 日
My pleasure.
If my Answer helped you solve your problem, please Accept it!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeEquation Solving についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by