'프로그래밍/Matlab'에 해당되는 글 1건

  1. 2015.05.10 Lorenz attractor evolving animation

This post contains a Matlab example to see how  trajectory of the Lorenz attractor is evolving as time flow and the Lorenz attractor is shown below.






Below is the M file used to generate the above picture.


%% Imlements the Lorenz System ODE

%   dx/dt = sigma*(y-x)

%   dy/dt = x*(rho-z)-y

%   dz/dt = xy-beta*z



clear all;


%% Settings for Lorenz system

global sigma rho beta


% Standard constants for the Lorenz Attractor

sigma = 10;

rho = 28;

beta = 8/3;


tmax = 1000;


start_point = [1.0 1.0 1.0];


%% Function declaration

%[t,y] = ode45(@mylorenz,[0 1000], [1.0 1.0 1.0]);


f = @(t,y) [sigma*(y(2) - y(1)); -y(1)*y(3) + rho*y(1) - y(2); y(1)*y(2) - beta*y(3)];


% To plot a trajectory in the phase plane starting at a point (a1, a2) at 

%   time t=0 for increasing values of t going from 0 to 4 type

[ts,ys] = ode45(f,[0,tmax],start_point);


syms t y1 y2 y3

F = f(t,[y1;y2;y3])


[y1s,y2s,y3s] = solve(F(1),F(2),F(3),y1,y2,y3)


plot3(y1s(1),y2s(1),y3s(1),'*', y1s(2),y2s(2),y3s(2),'>', y1s(3),y2s(3),y3s(3),'o');

strCrit1 = ['(',num2str(double(y1s(1))),',',num2str(double(y2s(1))),',',num2str(double(y3s(1))),')'];

strCrit2 = ['(',num2str(double(y1s(2))),',',num2str(double(y2s(2))),',',num2str(double(y3s(2))),')'];

strCrit3 = ['(',num2str(double(y1s(3))),',',num2str(double(y2s(3))),',',num2str(double(y3s(3))),')'];

%legend(strCrit1,strCrit2,strCrit3,'Location','northoutside','Orientation','horizontal')

legend(strCrit1,strCrit2,strCrit3);


[t,y] = ode45(@mylorenz,[0 tmax], start_point);


h = animatedline;

h.Color = [0 0 1];


%# initialize point

spot = line('XData',y(1,1), 'YData',y(1,2), 'ZData',y(1,3), ...

        'Color','r', 'marker','.', 'MarkerSize',50);

    

for k = 1:length(t)

    addpoints(h,y(k,1),y(k,2),y(k,3));

    set(spot,'XData',y(k,1), 'YData',y(k,2), 'ZData',y(k,3))    %# update X/Y data

    str = ['Time: ',num2str(k)];

    %set(hTxt,'String',str);         %# update time tick

    title(['Time ',str,' Ticks'])

    drawnow

    if ~ishandle(h), return; end             %# end running in case you close the figure

end


grid on;



And another m file: mylorenz.m


% mylorenz.m

%

% Imlements the Lorenz System ODE

%   dx/dt = sigma*(y-x)

%   dy/dt = x*(rho-z)-y

%   dz/dt = xy-beta*z

%

% Inputs:

%   t - Time variable: not used here because our equation

%       is independent of time, or 'autonomous'.

%   x - Independent variable: has 3 dimensions

% Output:

%   dx - First derivative: the rate of change of the three dimension

%        values over time, given the values of each dimension


function dy = mylorenz(t,y)


global sigma rho beta


% Standard constants for the Lorenz Attractor

%sigma = 10;

%rho = 28;

%beta = 8/3;


% I like to initialize my arrays

dy = [0; 0; 0];

%dy = zeros(3,1);


dy(1) = sigma*(y(2) - y(1));

dy(2) = -y(1)*y(3) + rho*y(1) - y(2);

dy(3) = y(1)*y(2) - beta*y(3);



Posted by kevino
,