Impact-Site-Verification: dbe48ff9-4514-40fe-8cc0-70131430799e

Search This Blog

Euler's method for solving ODE using MATLAB

MATLAB Program:

% Euler's method
 % Approximate the solution to the initial-value problem
 % dy/dt=y-t^2+1 ; 0<=t<=2 ; y(0)=0.5;

 f = @(t,y) (y-t^2+1);
 a = input('Enter left end ponit, a:  ');
 b = input('Enter right end point, b:  ');
 n = input('Enter no. of subintervals, n: ');               
 alpha = input('Enter the initial condition, alpha:  ');

 h = (b-a)/n;                                               
 t = a;
 w = alpha;
 fprintf('  t         w\n');
 fprintf('%5.4f  %11.8f\n', t, w);

 for i = 1:n
 w = w+h*f(t, w);
 t = a+i*h;
 fprintf('%5.4f  %11.8f\n', t, w);
 plot(t,w,'r*'); grid on;
 xlabel('t values'); ylabel('w values');
 hold on;
 end

OutPut:
>> euler_final
Enter left end ponit, a:  0
Enter right end point, b:  2
Enter no. of subintervals, n: 10
Enter the initial condition, alpha:  0.5
  t         w
0.0000   0.50000000
0.2000   0.80000000
0.4000   1.15200000
0.6000   1.55040000
0.8000   1.98848000
1.0000   2.45817600
1.2000   2.94981120
1.4000   3.45177344
1.6000   3.95012813
1.8000   4.42815375
2.0000   4.86578450

>> 




No comments

Popular Posts