-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod_eular.m
More file actions
executable file
·35 lines (30 loc) · 1.16 KB
/
mod_eular.m
File metadata and controls
executable file
·35 lines (30 loc) · 1.16 KB
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
function a= eular( df )
% for calculating value of dy/dx at some perticular pt using modifes
% asking intial conditions
x0 = input(' initial value of x : ');
y0 = input ('initial value of y : ');
x1 = input( ' value of x at which y is to be calculated : ');
tol = input( 'desired lavel of accuracy in the final result : ');
% calaulating the value of h
n =ceil( (x1-x0)/sqrt(tol));
h = ( x1 - x0)/n
%loop for calculating values
for k = 1 : n
X(1,1) = x0; Y (1,1) = y0;
X( 1, k+1) = X(1,k) + h;
y_t = Y(1,k) + h* feval( df , X(1,k) , Y(1,k));% Eular's formula
Y(1 ,k+1) = Y(1,k) + h/2* (feval( df , X(1,k) , Y(1,k)) + feval( df , X(1,k) + h , y_t ) ) ;
%improving results obtained by modified Eular's formula
while abs( Y(1,k+1) - y_t ) > h
y_t = Y(1,k) + h*feval( df , X(1,k) , Y(1,k+1));
Y( 1 ,k+1) = Y(1,k) + h/2* (feval( df , X(1,k) , Y(1,k)) + feval( df , X(1,k) + h , y_t ) );
end
end
%displaying results
fprintf( 'for \t x = %g \n \ty = %g \n' , x1,Y(1,n+1))
%displaying graph
x = 1:n+1;
y = Y(1,n+1)*ones(1,n+1) - Y(1,:);
plot(x,y,'b')
xlabel = (' no of intarval ');
ylabel = ( ' Error ');