微分方程数值解Euler法
微分方程:dy/dt=1+y; 解是y=2exp(x)-1;
clc clear figure(2) dx=0.1; x=0:dx:8; y=zeros(size(x)); x(1)=0; y(1)=1; for i=1:length(x)-1 % z= y(i)+ (1+y(i)) * dx; % y(i+1)= y(i)+ ( (1+y(i) + 1+ z) /2) * dx; y(i+1)= y(i)+ (1+y(i)) * dx; end plot(x,y) grid on hold on ezplot('2*exp(x)-1',[0,8])
logistic增长模型
%微分方程入下 %dp/dt=0.001*(100-p)*p; clc clear figure(2) dx=0.1; x=0:dx:100; y=zeros(size(x)); x(1)=0; y(1)=10; for i=1:length(x)-1 % z= y(i)+ (1+y(i)) * dx; % y(i+1)= y(i)+ ( (1+y(i) + 1+ z) /2) * dx; % y(i+1)= y(i)+ (1+y(i)) * dx; z=y(i) +0.001 *(100-y(i))*y(i)*dx; y(i+1)=y(i) + ((0.001 *(100-y(i))*y(i) + 0.001*(100-z)*z )/2 ) *dx; end plot(x,y) grid on hold on