0

I am trying to plot a three dimensional phase portrait of a system of first order differential equations, but I am getting an error message saying I have an unrecognized variable eta. Apart from the three dimensional phase portrait, I also want to plot x(1) versus t, x(2) versus t, and x(3) versus t, all on the same graph. I have commented out this plot command because I'm sure I'm wrong. So, I am looking to plot (i) a 3D phase portrait, and (ii) a time plot of x(1), x(2) and x(3) all on the same graph. Also, the [-0.015,0.015], [-2,2], [-4,4] that you see in the argument of the ode45 are the axes limits of x(1), x(2) and x(3). I am not even sure that the axes limits should be placed there. Could someone please help? Thank you.

[t,x] = ode45(@eqx3, eta, omega, [-0.015,0.015], [-2,2], [-4,4], initcond);
global eta omega
eta = 0.05;
omega = 25;
%tspan = [0,50]
initcond = [1, 0.5, -0.4]


%subplot(211)
%plot(t, x(:,1), t,x(:,2),'--',x(:,3),'--');
%xlabel('t')

subplot(212)
plot(x(:,1), x(:,2), x(:,3))
xlabel('x1')
ylabel('x2')
zlabel('x3')

function xdot = eqx3(t,x,eta,omega)
  global eta omega
  xdot = zeros(3,1);
  xdot(1) = -(2*eta*omega + 1)*x(1) + x(2) - 1;
  xdot(2) = -(2*eta*omega + (omega^2))*x(1) + x(3) + 2;
  xdot(3) = -(omega^2)*x(1) + x(2) - 1;
  %xdot =[xdot(1);xdot(2);xdot(3)];
end
3
  • You are defining initcond after calling ode45. Move it to the first line in your program. Same thing with eta and omega. Commented Feb 12, 2020 at 21:42
  • Also, using global should not be necessary and it is usually frowned upon. You are already passing eta and omega as function arguments to eqx3, so no need to declare them as global variables. Commented Feb 12, 2020 at 21:43
  • Hi. I moved initcond to the first line of the code (that was a pretty silly mistake) and commented out global eta omega, but the error "Unrecognized function or variable eta" is still there. A good guide to solving this problem can be found at uk.mathworks.com/matlabcentral/answers/416516-3d-phase-portrait. Commented Feb 12, 2020 at 21:53

1 Answer 1

2

Your code needed some clean up to

  • Remove globals,
  • Define variables before using them ,
  • Pass the right parameters to the ODE equation.
  • Use an anonymous function @(t,x) eqx3(t,x,eta, omega) to package the ODE parameters with the function,
  • Use plot3 for 3D plotting of the phase portrait.

This is the result that I came up with:

eta = 0.05;
omega = 25;
tspan = [0,50];
initcond = [1, 0.5, -0.4]
[t,x] = ode45(@(t,x) eqx3(t,x,eta, omega), tspan, initcond);

plot3(x(:,1), x(:,2), x(:,3))
xlabel('x1')
ylabel('x2')
zlabel('x3')

function xdot = eqx3(t,x,eta,omega)
  xdot = zeros(3,1);
  xdot(1) = -(2*eta*omega + 1)*x(1) + x(2) - 1;
  xdot(2) = -(2*eta*omega + (omega^2))*x(1) + x(3) + 2;
  xdot(3) = -(omega^2)*x(1) + x(2) - 1;
end

which produces

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Kavka, I just ran your code, and it works beautifully. Muchos gracias.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.