1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| h=input('请输入步长'); [x,u]=pro1(h);
function d=f(x,u) d=exp(x)-100*u; end function [x,u] = pro1(h) x1=0; x2=1; u1=1;
N=abs(x2-x1)/h; u=zeros(N+1,1); up=zeros(N+1,1); x=zeros(N+1,1); u(1)=u1; up(1)=u1; x(1)=x1;
for i = 1:N up(i+1)=u(i)+f(x(i),u(i))*h; uc=u(i)+f(x(i+1),up(i+1))*h; u(i+1)=(uc+up(i+1))/2; x(i+1)=x(i)+h; disp(u(i+1)); end syms z(t);
ys=dsolve(diff(z,t)==exp(t)-100*z,z(0)==1); disp(ys) subplot(2,2,1); plot(x,u); ylabel('改进Euler'); title('改进Euler函数'); subplot(2,2,2); plot(x,up); ylabel('Euler'); title('Euler函数'); subplot(2,2,3); plot(u,up);
end
|