MATLAB Answers

Why it plots more than just one point and how do I get rid of them

1 view (last 30 days)
Stefania Maria
Stefania Maria on 10 Dec 2019
Edited: Star Strider on 10 Dec 2019
function xp=h2(t,x)
xp=x;
xp(1)=(x(2)) .^2 - 70.*x(2);
xp(2)=(x(1)) .^2 +134.*x(1) - 64 .*x(2);
hold on
[t,x]=ode23('h2', [0,100], [0,0]);
plot(x(:,1), x(:,2), 'kx')
[t,x]=ode23('h2', [0,100], [-64,70]);
plot(x(:,1),x(:,2), 'bx')
[t,x]=ode23('h2', [0,100], [-134,0]);
plot(x(:,1), x(:,2), 'kx')
[t,x]=ode23('h2', [0,100], [55.4/2,70]);
plot(x(:,1), x(:,2), 'kx')
[t,x]=ode23('h2', [0,100], [-323.4/2,70]);
plot(x(:,1), x(:,2), 'kx')
When I plot [-64,70] (stable node) and [55.4/2,70] (saddle), it doesn't appear just one 'x' on the given coord. but it plots a spiral of 'x's starting from those points to [0,0] (focus) point. How do I make the spiral disappear, and have only one 'x' point for each coord?

  0 Comments

Sign in to comment.

Answers (1)

Star Strider
Star Strider on 10 Dec 2019
The code plots more than one point because you told it to.
Revised code:
function xp=h2(t,x)
xp=zeros(2,1);
xp(1)=(x(2)) .^2 - 70.*x(2);
xp(2)=(x(1)) .^2 +134.*x(1) - 64 .*x(2);
end
hold on
[t,x]=ode23(@h2, [0,100], [0,0]);
plot(x(:,1), x(:,2), 'kx')
[t,x]=ode23(@h2, [0,100], [-64,70]);
plot(x(:,1),x(:,2), 'bx')
[t,x]=ode23(@h2, [0,100], [-134,0]);
plot(x(:,1), x(:,2), 'kx')
[t,x]=ode23(@h2, [0,100], [55.4/2,70]);
plot(x(:,1), x(:,2), 'kx')
[t,x]=ode23(@h2, [0,100], [-323.4/2,70]);
plot(x(:,1), x(:,2), 'kx')
hold off
What result do you want?

  2 Comments

Star Strider
Star Strider on 10 Dec 2019
Replace ‘[0,100]’ for the ‘tspan’ argument with:
tv = linspace(0, 100, 5);
Then use that in every ode45 call:
hold on
[t,x]=ode23(@h2, tv, [0,0]);
plot(x(:,1), x(:,2), 'kx')
[t,x]=ode23(@h2, tv, [-64,70]);
plot(x(:,1),x(:,2), 'bx')
[t,x]=ode23(@h2, tv, [-134,0]);
plot(x(:,1), x(:,2), 'kx')
[t,x]=ode23(@h2, tv, [55.4/2,70]);
plot(x(:,1), x(:,2), 'kx')
[t,x]=ode23(@h2, tv, [-323.4/2,70]);
plot(x(:,1), x(:,2), 'kx')
hold off
That will produce five points for every ode45 call.
If you only want five points total, you need to choose which points you want to plot.
That might require:
hold on
[t,x1]=ode23(@h2, tv, [0,0]);
plot(x1(end,1), x1(end,2), 'kx')
[t,x2]=ode23(@h2, tv, [-64,70]);
plot(x2(end,1),x2(end,2), 'bx')
[t,x3]=ode23(@h2, tv, [-134,0]);
plot(x3(end,1), x3(end,2), 'kx')
[t,x4]=ode23(@h2, tv, [55.4/2,70]);
plot(x4(end,1), x4(end,2), 'kx')
[t,x5]=ode23(@h2, tv, [-323.4/2,70]);
plot(x5(end,1), x5(end,2), 'kx')
hold off
Or something similar.
Experiment to get the result you want.

Sign in to comment.