来源: http://blog.csdn.net/superdont
建立函数1:
function xdot=lorrenz(t,x)
for t=0:0.1:100;
if(cos(5.3*t)>=0)
p=1;
else
p=-1;
end
end
xdot=[-8/3*x(1)+p*x(2)*x(3);(-x(2)+x(3))*(25-10*cos(5.3*t));-p*x(1)*x(2)-(17.5*cos(5.3*t)+10.5)*x(2)+(13.3-14*cos(5.3*t))*x(3)];
建立文件2:
tic;
x0=[0;0;1e-10];tspan=[0,100];
[t,x]=ode45(@lorrenz,tspan,x0);
plot(t,x(:,1),'k');
r=length(t)-5000:length(t);
figure; plot(x(r,2),x(r,3),'k');
toc;
显示相图如下
输出结果:
Elapsed time is 5.547000 seconds.
式中函数:
Tic
Toc
用于计算时间,例如:
for n = 1:100
A = rand(n,n);
b = rand(n,1);
tic
x = A\b;
t(n) = toc;
end
plot(t)
可输出: