You are on page 1of 38

www.mcdtu.wordpress.

com

SCIENTIFIC COMPUTING
LAB FILE

www.mcdtu.wordpress.com |

www.mcdtu.wordpress.com

ASSIGNMENT 1

BISECTION METHOD
SOURCE CODE:
% Bisection method to find the root of the function f(x)=0
% INPUT
%
%

- f is the function
- a and b are the left and right endpoints
- tol is the tolerance

% OUTPUT
%
%

- c is the root
- yc= f(c)
- err is the error estimate for c

% PROGRAM STARTS HERE


clear all; close all; clc;
disp('________________________________________________________________
__')
disp('--Name: A T I N
M I N O C H A, Roll No. 2K11/MC/017
--')
disp('--Assignment 1, Bisection Method
--')
disp('--Date - 27/02/2013
--')
disp('________________________________________________________________
__')
% INPUTS: Enter the following
f = inline('sin(x) - tan(x^2) + exp(x)'); % Function in f(x)=0
a = -1.0; % Upper initial guess
b = 0.0; % Lower initial guess
tol=.000001; count=0;
fprintf('The initial guess a = %g \n',a);
fprintf('The initial guess b = %g \n',b);
% SOLUTION STARTS HERE
ya=f(a);
yb=f(b);
if (ya*yb > 0)
disp('No root lie in this interval');
return;
end
while( b-a > tol)
count=count+1;
c=(a+b)/2;
yc=f(c);
www.mcdtu.wordpress.com |

www.mcdtu.wordpress.com
if yc==0
fprintf('The equation is sin(x) - tan(x^2) + exp(x)and the root
is x = %g \n',c);
elseif yb*yc>0
b=c;
yb=yc;
else
a=c;
ya=yc;
end
end
c=(a+b)/2;
err=abs(b-a);
yc=f(c);
fprintf('\n The equation is sin(x) - tan(x^2) + exp(x) and the root is
x = %g \n',c);
fprintf('\n Number of iterations are = %g \n',count);
% END OF THE PROGRAM

www.mcdtu.wordpress.com |

www.mcdtu.wordpress.com

OUTPUT:

www.mcdtu.wordpress.com |

www.mcdtu.wordpress.com

ASSIGNMENT 2

NEWTON RAPHSON METHOD


SOURCE CODE:
% Newton-Raphson method

to find the root of the function f(x)=0

% INPUT
%
%
%
%

f is the function
f1 is the derivative of f
xn is previous value
tol is the tolerance
fxn=f(xn)

% OUTPUT
%

- xn1 is the root


- fxn1= f(xn1)

% PROGRAM STARTS HERE


clear all; close all; clc;
disp('________________________________________________________________
__')
disp('--Name: A T I N
M I N O C H A, Roll No. 2K11/MC/017
--')
disp('--Assignment 2, Newton Raphson Method
--')
disp('--Date - 27/02/2013
--')
disp('________________________________________________________________
__')
% INPUTS: Enter the following
% GRAPH
syms x;
x= -2:0.2:2;
f = inline('cos(x)-x^2-x'); % Function in f(x)=0
f1 = inline('-sin(x)-2*x-1'); % Derivative of the function
plot(x,cos(x)-x.^2 -x,'--rs') % Plotting the graph of the function
grid on;
disp('------------SOLUTION STARTS HERE
-------')
disp('Since the graph crosses the x-axis at two points,')
disp('The function has two roots. Therefore, we need to take')
disp('two different approximations and solve them seperately.')
% FIRST ROOT
disp('--------')

-------

FIRST ROOT OF f(x)

------

www.mcdtu.wordpress.com |

-----

----5

www.mcdtu.wordpress.com
xn=-1; % Initial guess
tol=.000001; count=0;
disp(sprintf('The initial approximation of the first root is %g',xn));
fxn=f(xn);
f1xn=f1(xn);
xn1=xn-fxn/f1xn;
while( abs(xn1-xn) > tol)
count=count+1;
if fxn==0
xn1=xn
disp(sprintf('The equation is cos(x)-x^2-x and the root is x =
%g',xn1));
else
xn=xn1;
fxn=f(xn);
f1xn=f1(xn);
xn1=xn-(fxn/f1xn);
end
end
disp(sprintf('\n The equation is cos(x)-x^2-x and the root is x =
%g',xn1));
disp(sprintf('\n Number of iterations are = %g',count));
% SECOND ROOT
disp('------------SECOND ROOT OF f(x)
-----------')
xn=0;% Initial guess
tol=.000001; count=0;
disp(sprintf('\nThe initial approximation of the second root is
%g',xn));
fxn=f(xn);
f1xn=f1(xn);
xn1=xn-fxn/f1xn;
while( abs(xn1-xn) > tol)
count=count+1;
if fxn==0
xn1=xn
disp(sprintf('The equation is cos(x)-x^2-x and the root is x =
%g',xn1));
else
xn=xn1;
fxn=f(xn);
f1xn=f1(xn);
xn1=xn-(fxn/f1xn);
end
end
disp(sprintf('\n The equation is cos(x)-x^2-x and the root is x =
%g',xn1));
disp(sprintf('\n Number of iterations are = %g',count));
% END OF THE PROGRAM
www.mcdtu.wordpress.com |

www.mcdtu.wordpress.com

OUTPUT:

www.mcdtu.wordpress.com |

www.mcdtu.wordpress.com

ASSIGNMENT 3

GAUSS SEIDEL METHOD


SOURCE CODE:
% Gauss-Seidel method is used to find the solution of a system of
% equations!
% INPUT

- x, y, z and w are the initial approximations.

% OUTPUT
- x, y, z and w are the solutions to the given system of
% equations.
% PROGRAM STARTS HERE
clear all; close all; clc;
disp('________________________________________________________________
__')
disp('--Name: A T I N
M I N O C H A, Roll No. 2K11/MC/017
--')
disp('--Assignment 3, Gauss - Seidel Method
--')
disp('--Date - 20/03/2013
--')
disp('________________________________________________________________
__')
% INPUTS: Enter the following
disp(sprintf('\nSystem of equations is:\n1. 4x+y+w=2\n2. x+4y+z=-2\n3.
y+4z+w=2\n4. x+z+4w=-2'));
x=0; % Initial approximations
y=0;
z=0;
w=0;
tol=.000001; % tol is the limit of while loop.
count=1; % count is used to calculate no. of iterations.
disp(sprintf('\nInitial guess =[%g %g %g %g]',x,y,z,w));
disp('--------------')
% SOLUTION starts here!

SOLUTION STARTS HERE

------

-----

X0=[x;y;z;w];
x= 1/4*(2-y-w);
y= 1/4*(-2-x-z);
www.mcdtu.wordpress.com |

www.mcdtu.wordpress.com
z= 1/4*(2-y-w);
w= 1/4*(-2-x-z);
X1=[x;y;z;w];
while(max(X1-X0) > tol)
count=count+1;
X0=[x;y;z;w];
x= 1/4*(2-y-w);
y= 1/4*(-2-x-z);
z= 1/4*(2-y-w);
w= 1/4*(-2-x-z);
X1=[x;y;z;w];
end
disp(sprintf('\nNo. of iterations are: %g',count));
disp(sprintf('\nThe solution of system of system of equations =[%g %g
%g %g]',x,y,z,w));
% END OF THE PROGRAM

www.mcdtu.wordpress.com |

www.mcdtu.wordpress.com

OUTPUT:

www.mcdtu.wordpress.com |

10

www.mcdtu.wordpress.com

ASSIGNMENT 4

SECANT METHOD
SOURCE CODE:
% Secant Method

to find the root of the function f(x)=0

% INPUT
%
%
%
%

f is the function
xn and xn0 are the previous values
tol is the tolerance
fxn=f(xn)
fxn0=f(xn0)

% OUTPUT
%

- xn1 is the root


- fxn1=f(xn1)

% PROGRAM STARTS HERE


clear all; close all; clc;
disp('________________________________________________________________
__')
disp('--Name: A T I N
M I N O C H A, Roll No. 2K11/MC/017
--')
disp('--Assignment 4, Secant Method
--')
disp('--Date - 20/03/2013
--')
disp('________________________________________________________________
__')
% INPUTS: Enter the following
syms x;
x= 0:0.2:1;
f = inline('cos(x)-x.*exp(x)'); % Function in f(x)=0
plot(x,cos(x)-x.*exp(x),'--rs') % Plotting the graph of the function
grid on;
disp('------------SOLUTION STARTS HERE
-------')
disp('The graph crosses the x-axis between 0 and 1.')

-----

xn=1; xn0=0; % Initial guess


tol=.000001;
count=0;
disp(sprintf('The initial guesses are %g and %g.',xn0,xn));
fxn=f(xn);
www.mcdtu.wordpress.com |

11

www.mcdtu.wordpress.com
fxn0=f(xn0);
xn1=xn-fxn/(fxn-fxn0)*(xn-xn0);
while( abs(xn1-xn) > tol)
count=count+1;
if fxn==0
xn1=xn;
xn=xn0;
disp(sprintf('The equation is cos(x)-x*(e^x) and the root is x
= %g',xn1));
else
xn=xn1;
fxn=f(xn);
fxn0=f(xn0);
xn1=xn-fxn/(fxn-fxn0)*(xn-xn0);
end
end
disp(sprintf('\n The equation is cos(x)-x*(e^x) and the root is x =
%g',xn1));
disp(sprintf('\n Number of iterations are %g',count));
% END OF THE PROGRAM

www.mcdtu.wordpress.com |

12

www.mcdtu.wordpress.com

OUTPUT:

www.mcdtu.wordpress.com |

13

www.mcdtu.wordpress.com

ASSIGNMENT 5

CROUTS METHOD
SOURCE CODE:
% Crout's method is used to find the solution of a system of
% equations!
% INPUT
%
%
%
%
%
% OUTPUT
% equations.

A and B are the matrices of the system of equations.


A = LU
X is the solution matrix of the system.
L is the lower triangular matrix.
U is the upper triangular matrix.
Z = UX
x, y, z and w are the solutions to the given system of

% PROGRAM STARTS HERE


clear all; close all; clc;
disp('________________________________________________________________
__')
disp('--Name: A T I N
M I N O C H A, Roll No. 2K11/MC/017
--')
disp('--Assignment 5, Crouts Method
--')
disp('--Date - 03/04/2013
--')
disp('________________________________________________________________
__')
% INPUTS: Enter the following
disp(sprintf('\nSystem of equations is:\n1. 2x+1y+-4z+1w= 4\n2. 4x+3y+5z-2w=-10\n3. 1x-1y+1z-1w= 2\n4. 1x+3y-3z+2w=-1'));
% SOLUTION starts here!
A=[2, 1, -4, 1; -4, 3, 5, -2; 1, -1, 1, -1; 1, 3, -3, 2]
B=[4 ;-10 ;2 ;-1]
disp('------------SOLUTION STARTS HERE
-------')

-----

l11=A(1,1);
l21=A(2,1);
l31=A(3,1);
l41=A(4,1);
u12=A(1,2)./l11;
www.mcdtu.wordpress.com |

14

www.mcdtu.wordpress.com
u13=A(1,3)./l11;
u14=A(1,4)./l11;
l22=A(2,2)-l21.*u12;
l32=A(3,2)-l31.*u12;
l42=A(4,2)-l41.*u12;
u23=(A(2,3)-l21.*u13)./l22;
u24=(A(2,4)-l21.*u14)./l22;
l33=A(3,3)-l31*u13-l32*u23;
l43=A(4,3)-l41*u13-l42*u23;
u34=(A(3,4)-l31*u14-l32*u24)/l33;
l44=A(4,4)-l41*u14-l42*u24-l43*u34;
disp(sprintf('\nLower triangular matrix is\n'));
L=[l11 0 0 0;l21 l22 0 0;l31 l32 l33 0; l41 l42 l43 l44]
disp(sprintf('\nUpper triangular matrix is\n'));
U=[1 u12 u13 u14; 0 1 u23 u24; 0 0 1 u34; 0 0 0 1]
Z=(inv(L))*B;
disp(sprintf('\nThe solution of system of system of equations is'));
X=(inv(U))*Z
Ainv=inv(U).*inv(L)
% END OF THE PROGRAM

www.mcdtu.wordpress.com |

15

www.mcdtu.wordpress.com

OUTPUT:

www.mcdtu.wordpress.com |

16

www.mcdtu.wordpress.com

www.mcdtu.wordpress.com |

17

www.mcdtu.wordpress.com

www.mcdtu.wordpress.com |

18

www.mcdtu.wordpress.com

ASSIGNMENT 6

NEWTONS FORWARD AND BACKWARD DIFFERENCE


INTERPOLATION METHOD
SOURCE CODE:
% Newton's Forward and Backward Interpolation Difference Formula!
% INPUT
%
%

- x is the matrix for which value is given.


- y is the matrix giving values corresponding to x.
- x1 is the matrix whose values are to be found.

% PROGRAM STARTS HERE


clear all; close all; clc;
disp('________________________________________________________________
__')
disp('--Name: A T I N
M I N O C H A, Roll No. 2K11/MC/017
--')
disp('--Assignment 6, Interpolation
--')
disp('--Date - 06/04/2013
--')
disp('________________________________________________________________
__')
% INPUTS:Enter the following
x= [0 0.1 0.2 0.3 0.4 0.5]
y= [-1.5 -1.27 -0.98 -0.63 -0.22 0.25]
x1= [0.15 0.25 0.45]
% SOLUTION starts here!
for l=1:3
p=x1(l);
disp(sprintf('\n\nApplying Newton Forward Interpolation difference
formula for x = %g',x1(l)));
% Newton's Forward Interpolation Formula
n = length(x); % Calculating length of x
a(1) = y(1);
for k = 1 : n - 1
d(k, 1) = (y(k+1) - y(k))/(x(k+1) - x(k)); % Finite forward
difference matrix
end
www.mcdtu.wordpress.com |

19

www.mcdtu.wordpress.com
for j = 2 : n - 1
for k = 1 : n - j
d(k, j) = (d(k+1, j - 1) - d(k, j - 1))/(x(k+j) - x(k));
end
end
for j = 2 : n
a(j) = d(1, j-1);
end
Df(1) = 1;
c(1) = a(1);
for j = 2 : n
Df(j)=(p - x(j-1)) .* Df(j-1);
c(j) = a(j) .* Df(j);
end
fp=sum(c);
disp(sprintf('\nWe get value as %g',fp)); % Finding sum
disp(sprintf('\n\nApplying Newton Backward Interpolation difference
formula for x = %g',x1(l)));
% Newton's Backward Interpolation Formula
for i=1:n
diff(i,1)=y(i); % Finite backward difference operator matrix
end
for j=2:n
for i=n:-1:j
diff(i,j)=diff(i,j-1)-diff(i-1,j-1);
end
end
answer=y(n);
h=x(n)-x(n-1);
s=(p-x(n))/h;
for i=1:n-1
term=1;
for j=1:i
term=term*(s+j-1)/j;
end
answer=answer+term*diff(n,i+1); % Finding sum
end
disp(sprintf('\nWe get value as %g',answer));
end
% END OF THE PROGRAM

www.mcdtu.wordpress.com |

20

www.mcdtu.wordpress.com

OUTPUT:

www.mcdtu.wordpress.com |

21

www.mcdtu.wordpress.com

www.mcdtu.wordpress.com |

22

www.mcdtu.wordpress.com

ASSIGNMENT 7

TRAPEZOIDAL METHOD
SOURCE CODE:
% TRAPEZOIDAL METHOD to perform Numerical Integration!
% INPUTS
- f is the fucntion e^-(x^2)=0 we need to integrate.
%
- x0 is the lower limit of integration.
%
- x1 is the upper limit of integration.
%
- n is the number of mesh points
%
i.e no of intervals between x0 and x1.
%
- h is the length of the interval.
% Area gives the result of integration of curve f between f0 and f1.
% PROGRAM STARTS HERE
clear all; close all; clc;
disp('________________________________________________________________
__')
disp('--Name: A T I N
M I N O C H A, Roll No. 2K11/MC/017
--')
disp('--Assignment 7, Trapezoidal Method
--')
disp('--Date - 10/04/2013
--')
disp('________________________________________________________________
__')
% INPUTS:Enter the following:
f=inline('exp(-x^2)');%inline function definition
x0=0;
x1=1;
n=20; % No. of intervals to be present between 0 and 1.
f0=f(x0);
f1=f(x1); % Computation of function values at end points.
h=(x1-x0)/n; % Value of length of the intervals.
f2=0;%initialize f2 to zero
a=x0;
for i=1:n-1
x2= a+i*h;
f2= f2+ f(x2);
end
disp('The answer for integration is: ');
area= h*(f0+f1)/2+ h*f2 % Prints the result of integration.
www.mcdtu.wordpress.com |

23

www.mcdtu.wordpress.com

% END OF THE PROGRAM

www.mcdtu.wordpress.com |

24

www.mcdtu.wordpress.com

OUTPUT:

www.mcdtu.wordpress.com |

25

www.mcdtu.wordpress.com

ASSIGNMENT 8

SIMPSONS METHOD
SOURCE CODE:
% Simpson's Method is used for Numerical Integration!
% INPUT
%
%

- f is the function
- f0 and fn are 1st and last values
- h is the difference between 2 values of x

% OUTPUT
- fx is the required function
% PROGRAM STARTS HERE
clear all; close all; clc;
disp('________________________________________________________________
__')
disp('--Name: A T I N
M I N O C H A, Roll No. 2K11/MC/017
--')
disp('--Assignment 8, Simpsons Method
--')
disp('--Date - 11/04/2013
--')
disp('________________________________________________________________
__')
% INPUTS: Enter the following
f=inline('exp(-x^2)'); % Function
f0=f(0); % Value of the function at 0
fn=f(1); % Value of the function at 1
h=0.05;
n=20; % No. of divisions
fx=h/3*(f0+fn);
if mod(n,2)==0 % mod is used to find the remainder
for i=1:19
k=i*h;
if mod(i,2)==0
fx=fx+2*h/3*f(k); % Formula when i is a multiple of 2
else
fx=fx+4*h/3*f(k); % Formula when i is not a multiple of 2
end
end
end
disp(sprintf('\nThe equation is exp(-x^2) and its integration from x
= 0 to 1 by Simpsons method is\n %g',fx));
% END OF THE PROGRAM
www.mcdtu.wordpress.com |

26

www.mcdtu.wordpress.com

OUTPUT:

www.mcdtu.wordpress.com |

27

www.mcdtu.wordpress.com

ASSIGNMENT 9

ROMBERGS METHOD
SOURCE CODE:
% Romberg's Method is a method used for Numerical Integration
% INPUT
%
%

- f is the function.
- f0 and fn are 1st and last values.
- h1,h2,h3 are the difference between 2 values of x.

% OUTPUT
%
%

- Q1,Q2,Q3 are the required functions at h1,h2,h3 resp.


- R1 and R2 are the Richardson solutions.
- S is the required function value.

% PROGRAM STARTS HERE


clear all; close all; clc;
disp('________________________________________________________________
__')
disp('--Name: A T I N
M I N O C H A, Roll No. 2K11/MC/017
--')
disp('--Assignment 9, Rombergs Method
--')
disp('--Date - 17/04/2013
--')
disp('________________________________________________________________
__')
% INPUTS: Enter the following
f=inline('(cos(x)*log(sin(x)))/(1+(sin(x)^2))'); % Given function
f0=f(pi/4); % Value of the function at 0.
fn=f(pi/2); % Value of the function at 1.
h1=pi/8;
n1=(pi/2-pi/4)/h1; % No. of divisions
Q1=h1/2*(f0+fn); % Applying Trapezoidal Rule.
for i=1:n1
k=i*h1;
Q1=Q1+h1*f(pi/4 + k);
end
h2=h1/2;
n2=(pi/2-pi/4)/h2;
Q2=h2/2*(f0+fn);
for i=1:n2
k=i*h2;
Q2=Q2+h2*f(pi/4 + k);
end
R1=(4*Q2-Q1)/3;
www.mcdtu.wordpress.com |

28

www.mcdtu.wordpress.com
h3=h2/2;
n3=(pi/2-pi/4)/h3;
Q3=h3/3*(f0+fn); % Applying Simpson's rule
if mod(n3,2)==0 % mod is used to find remainder
for i=1:n3
k=i*h3;
if mod(i,2)==0
Q3=Q3+2*h3/3*f(pi/4 + k); % Formula for i multiple of 2
else
Q3=Q3+4*h3/3*f(pi/4 + k); % Formula for not i multiple of 2
end
end
end
R2=(16*Q3-Q2)/15;
S=(16*R2-R1)/15;
disp(sprintf('\nThe equation is (cos(x)*log(sin(x)))/(1+(sin(x)^2))
and its integration \nfrom x = pi/4 to pi/2 by Rombergs Method is\n
%g',S));
% END OF THE PROGRAM

www.mcdtu.wordpress.com |

29

www.mcdtu.wordpress.com

OUTPUT:

www.mcdtu.wordpress.com |

30

www.mcdtu.wordpress.com

ASSIGNMENT 10

PICARDS METHOD
SOURCE CODE:
% Picard's Method is used to find solution of a differential equations
% INPUT
%

- f is the function
- y0 is the solution at initial value of x=0.4

% OUTPUT
%

- Y is the solution of differential equation.


- Y1 is the solution of differential equation at x=0.8.

% PROGRAM STARTS HERE


clear all; close all; clc;
disp('________________________________________________________________
__')
disp('--Name: A T I N
M I N O C H A, Roll No. 2K11/MC/017
--')
disp('--Assignment 10, Picards Method
--')
disp('--Date - 29/04/2013
--')
disp('________________________________________________________________
__')
% INPUTS: Enter the following
f=inline('sqrt(x+y)');
fint=inline('2/3*sqrt((x+y)^3)');
x0=0.4;% Initial value of x
y0=0.41; % Initial value of y at x=0.4
Y=y0;
x1=0.6;
for i=1:3
y1=Y;
Y=y0+fint(x1,y1)-fint(x0,y1);
end
disp(sprintf('\nThe solution of the differential equation dy/dx =
sqrt(x+y) \nby Picards method at x = 0.6 is\n%g',Y));
y0=Y;
x2=0.8;
Y1=Y;
for i=1:3
y1=Y1;
Y1=y0+fint(x2,y1)-fint(x1,y1);
end
www.mcdtu.wordpress.com |

31

www.mcdtu.wordpress.com
disp(sprintf('\nThe solution of the differential equation dy/dx =
sqrt(x+y) \nby Picards method at x = 0.8 is \n%g\n',Y1));
% END OF THE PROGRAM

www.mcdtu.wordpress.com |

32

www.mcdtu.wordpress.com

OUTPUT:

www.mcdtu.wordpress.com |

33

www.mcdtu.wordpress.com

ASSIGNMENT 11

EULERS METHOD
SOURCE CODE:
% Euler's method is used to find solution of a differential equation.
% INPUT
%

- f is the function.
- y0 is the solution at initial value of x=0.4.

% OUTPUT
%

- Y is the solution of differential equation.


- Y1 is the solution of differential equation at x=0.8.

% PROGRAM STARTS HERE


clear all; close all; clc;
disp('________________________________________________________________
__')
disp('--Name: A T I N
M I N O C H A, Roll No. 2K11/MC/017
--')
disp('--Assignment 11, Eulers Method
--')
disp('--Date - 29/04/2013
--')
disp('________________________________________________________________
__')
% INPUTS: Enter the following
f=inline('sqrt(x+y)');
x0=0.4;% Initial value of x
y0=0.41;% Initial value of y at x=0.4
x1=0.6;
h=0.2;
% SOLUTION STARTS HERE
Y=y0+h*f(x0+h/2,y0+h/2*f(x0,y0));
Y1=Y+h*f(x1+h/2,Y+h/2*f(x1,Y));
disp(sprintf('\nThe solution of the differential equation dy/dx =
sqrt(x+y)\nby Eulers method at x = 0.6 is \n%g\n',Y));
disp(sprintf('\nThe solution of the differential equation dy/dx =
sqrt(x+y)\nby Eulers method at x = 0.8 is \n%g\n',Y1));
% END OF THE PROGRAM

www.mcdtu.wordpress.com |

34

www.mcdtu.wordpress.com

OUTPUT:

www.mcdtu.wordpress.com |

35

www.mcdtu.wordpress.com

ASSIGNMENT 12

RUNGE KUTTA METHOD


SOURCE CODE:
% Runge Kuttas 4th Order Method is used to find the solution of a
% differential equation
% INPUT
%

- f is the function.
- y0 is the solution at initial value of x=0.4.

% OUTPUT
%

- Y is the solution of differential equation.


- Y1 is the solution of differential equation at x=0.8.

% PROGRAM STARTS HERE


clear all; close all; clc;
disp('________________________________________________________________
__')
disp('--Name: A T I N
M I N O C H A, Roll No. 2K11/MC/017
--')
disp('--Assignment 12, Runge Kutta Method
--')
disp('--Date - 29/04/2013
--')
disp('________________________________________________________________
__')
% INPUTS: Enter the following
syms x;
f=inline('(x+y)^(1/2)');
x0=0.4;% Initial value of x
y0=0.41;% Initial value of y at x=0.4
Y=y0;
x1=0.6;
h=0.2;
k1=h*f(x0,y0);
k2=h*f(x0+h/2,y0+k1/2);
k3=h*f(x0+h/2,y0+k2/2);
k4=h*f(x0+h,y0+k3);
Y=y0+1/6*(k1+2*k2+2*k3+k4);
disp(sprintf('\nThe solution of the differential equation dy/dx =
sqrt(x+y)\nby Runge Kutta 4th Order Method at x = 0.6 is \n%g\n',Y));
k1=h*f(x1,Y);
k2=h*f(x1+h/2,Y+k1/2);
k3=h*f(x1+h/2,Y+k2/2);
k4=h*f(x1+h,Y+k3);
Y1=Y+1/6*(k1+2*k2+2*k3+k4);
www.mcdtu.wordpress.com |

36

www.mcdtu.wordpress.com
disp(sprintf('\nThe solution of the differential equation dy/dx =
sqrt(x+y)\nby Runge Kutta 4th Order Method at x = 0.8 is \n%g\n',Y1));
% END OF THE PROGRAM

www.mcdtu.wordpress.com |

37

www.mcdtu.wordpress.com

OUTPUT:

www.mcdtu.wordpress.com |

38

You might also like