# How to make a phase diagram plot?

193 visualizaciones (últimos 30 días)
Wing Nam el 1 de Abr. de 2023
Respondida: Sam Chak el 16 de Dic. de 2023
Hi all. I have an ODE and I have already found the general solution of.
How can I plot a phase diagram with some initial value like y(1)=1?
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Sai el 4 de Abr. de 2023
I am not sure if the solution is correct because when x is equal to 1, y is not equal 1.

Iniciar sesión para comentar.

### Respuestas (2)

Sai el 4 de Abr. de 2023
Hi Wing,
I understand that you want to make a phase diagram plot for the above differential equation.
Please refer to the below code snippet.
function dydx = diff_eq(x,y)
dydx = [y(2); (exp(-x)-2*(x-1)*y(2)-(x-2)*y(1))/x];
end
Place two code snippets in different .m files. But the above code snippet in diff_eq.m file
[x,y] = ode45(@diff_eq,[1 5],[1; 1]);
plot(y(:,1),y(:,2));
xlabel('y(1)');
ylabel('y(2)');
please refer to the below documentation ink for more information on using ‘ode45’
I hope this helps you to resolve the query
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

Sam Chak el 16 de Dic. de 2023
If the analytical solution exists, then you can solve the ODE symbolically using the dsolve() command. From the results, you can plot out the phase portrait diagram using the fplot() command.
%% Setup the ODE in symbolic form
syms y(x) z(x)
Dy = diff(y,x); % dy/dx
ODEqn = diff(y,x,2) == (exp(-x) - 2*(x - 1)*Dy - (x - 2)*y)/x
ODEqn(x) =
%% General solution
yGenSol(x) = dsolve(ODEqn)
yGenSol(x) =
%% Particular solution
init = [y(1)==1, Dy(1)==0]; % initial values
yParSol(x) = dsolve(ODEqn, init)
yParSol(x) =
DyParSol = simplify(diff(yParSol), 'steps', 100)
DyParSol(x) =
%% Plot time responses of states
figure(1)
T1 = tiledlayout(2, 1);
nexttile(T1)
fplot( yParSol, [1 20]), grid on, ylabel('y(x)')
nexttile(T1)
fplot(DyParSol, [1 20]), grid on, ylabel('dy/dx')
title( T1, '2nd-order System')
xlabel(T1, 'Time')
ylabel(T1, 'Staes')
%% Plot phase portrait diagram
figure(2)
fplot(yParSol, DyParSol, [1 20]), grid on
title('Phase Portrait')
xlabel('y(x)'), ylabel('dy/dx')
##### 0 comentariosMostrar -2 comentarios más antiguosOcultar -2 comentarios más antiguos

Iniciar sesión para comentar.

### Categorías

Más información sobre Symbolic Math Toolbox en Help Center y File Exchange.

R2022b

### Community Treasure Hunt

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

Start Hunting!

Translated by