Matlab forward Euler

来源:互联网 发布:枪神纪刷枪软件 编辑:程序博客网 时间:2024/06/18 02:21
% forward Euler demo% take two steps in the solution of % dy/dt = y, y(0) = 1% exact solution is y(t) = exp(t)clear allclose all% the exact solutiont = 0:0.01:1;yexact = exp(t);% initial conditionsy0 = 1h = 0.5tt = [0];yy = [y0];% one step of forward Euleryhalf = y0 +h*y0tt = [tt; h];yy = [yy; yhalf];plot(t,yexact)axis([0,1,0,3])hold onplot(tt,yy,'rx-')pause% what family member are we on now?c1 = yhalf/exp(0.5);ymember1 = c1*exp(t);plot(t,ymember1,'g--')pause% step 2 of forward Eulery1 = yhalf + h*yhalftt = [tt; 2*h];yy = [yy; y1];plot(tt,yy,'rx-')pause% what family member are we on now?c2 = y1/exp(1);ymember2 = c2*exp(t);plot(t,ymember2,'c--')

0 0
原创粉丝点击