Denny
Denny

Reputation: 225

Discretization for plotting trajectory of dynamic system in Matlab

Suppose I have the following dynamic system

\dot{x} = x(x-0.5)(x+0.5)

So I know the region of attraction is in [-0.5,0.5]

So given x0 in this region, this state should go to 0 as t goes to infinity

In matlab, I want to see such trajectory, so I use the following common way:

x(t+1) = x(t) + dt*f(x)

x0 = [-.3;-.1;.3] % 3D case
dt = .01;
num = 1;
for t = dt : dt : tf
    num = num + 1;
        x(:,num) = x(:,num - 1) + dt*([x(1)^3-0.25*x(1);x(2)^3-0.25*x(2);x(3)^3-0.25*x(3)] );
end 

It seems that all three trajectories do not converge to 0. Is there any problem with my codes?

enter image description here

Upvotes: 0

Views: 129

Answers (1)

ViG
ViG

Reputation: 1868

x is a 3xN matrix. By saying x(1) you implicitly write x(1,1), so this means that your second term (dt*([x(1)^3-0.25*x(1);x(2)^3-0.25*x(2);x(3)^3-0.25*x(3)]) will always stay the same. To go to the latest column use x(1,num-1).

The full code (I've changed variable tf to ma because tf is an existing MATLAB function.):

x0 = [-.3;-.1;.3]; % 3D case
dt = .01;
num = 1;
ma = 30;
x = zeros(3,length(dt:dt:ma)+1);
x(:,1) = x0;
for t = dt : dt : ma
    num = num + 1;
        x(:,num) = x(:,num - 1) + dt*([x(1,num-1)^3-0.25*x(1,num-1);x(2,num-1)^3-0.25*x(2,num-1);x(3,num-1)^3-0.25*x(3,num-1)] );
end 

plot(dt:dt:ma+dt,x)
xlim([0 30])

with result:

enter image description here

Upvotes: 1

Related Questions