Although it might not be obvious from the differential equat
Although it might not be obvious from the differential equation, its solution could
Solution
y\'= x^2+y^3, y(1) =1 h=0.1 function yx= f(x,y) % save as f.m yx=x^2+y^3; Euler’s method : %% % euler rule % SAVE AS eul.m clear euler h=0.1 ; n=5; x(1)= 1; y(1)=1; tout=x(1); yout=y(1).\'; disp(\' x numerical \') disp(\'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\') for i=1:(n+1 ) x(i+1) = x(1)+ i*h; k1 = h*f(x(i), y(i)); y(i+1) = y(i) + k1 ; tout=[tout;x(i)]; yout=[yout,y(i).\'] ; if rem(i-1 ,1) ==0 % x(i)<= 0.1 % rem(i-1,10) == 0 fprintf(\'%5.2f%17.7f\ \' ,x(i), y(i) ); end end plot(tout,yout,\'r\'),grid xlabel(\'t\') ylabel(\'y(t)\') >> eul x numerical ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1.00 1.0000000 1.10 1.2000000 1.20 1.4938000 1.30 1.9711323 1.40 2.9059886 ==> y(1.4) = 2.9059886 with Euler method improved Euler method: %% % improved euler method clear euler h=0.1 ; n=5; x(1)= 1; y(1)=1; tout=x(1); yout=y(1).\'; disp(\' x numerical \') disp(\'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\') for i=1:(n+1 ) x(i+1) = x(1)+ i*h; k1 = h*f(x(i), y(i)); k2= h*f(x(i)+h, y(i)+k1); y(i+1) = y(i) + ( k1+k2)/2 ; tout=[tout;x(i)]; yout=[yout,y(i).\'] ; if rem(i-1 ,1) ==0 % x(i)<= 0.1 % rem(i-1,10) == 0 fprintf(\'%5.2f%17.7f\ \' ,x(i), y(i) ); end end plot(tout,yout,\'r\'),grid xlabel(\'t\') ylabel(\'y(t)\') >> impeuler x numerical ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 1.00 1.0000000 1.10 1.2469000 1.20 1.6667966 1.30 2.6426810 1.40 8.7988106 ==> y(1.4) = 8.7988106 with improved Euler method