Upload
singh-ak
View
224
Download
0
Embed Size (px)
Citation preview
8/10/2019 mathematics and computing- SC File
1/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 1
SCIENTIFIC COMPUTING
LAB FILE
8/10/2019 mathematics and computing- SC File
2/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 2
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 followingf = inline('sin(x) - tan(x^2) + exp(x)'); % Function in f(x)=0a = -1.0; % Upper initial guessb = 0.0; % Lower initial guesstol=.000001; count=0;fprintf('The initial guess a = %g \n',a);
fprintf('The initial guess b = %g \n',b);
% SOLUTION STARTS HEREya=f(a);yb=f(b);if (ya*yb > 0)
disp('No root lie in this interval');return;
endwhile( b-a > tol)
count=count+1;
c=(a+b)/2;yc=f(c);
8/10/2019 mathematics and computing- SC File
3/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 3
if yc==0fprintf('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;
endendc=(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 isx = %g \n',c);
fprintf('\n Number of iterations are = %g \n',count);% END OF THE PROGRAM
8/10/2019 mathematics and computing- SC File
4/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 4
OUTPUT:
8/10/2019 mathematics and computing- SC File
5/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 5
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% GRAPHsyms 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 functionplot(x,cos(x)-x.^2 -x,'--rs') % Plotting the graph of the functiongrid 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) ------ -------')
8/10/2019 mathematics and computing- SC File
6/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 6
xn=-1; % Initial guesstol=.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=xndisp(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
enddisp(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 ROOTdisp('------- ------- 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=xndisp(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);
endenddisp(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
8/10/2019 mathematics and computing- SC File
7/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 7
OUTPUT:
8/10/2019 mathematics and computing- SC File
8/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 8
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 approximationsy=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);
8/10/2019 mathematics and computing- SC File
9/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 9
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
8/10/2019 mathematics and computing- SC File
10/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 10
OUTPUT:
8/10/2019 mathematics and computing- SC File
11/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 11
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 functiongrid on;
disp('------- ------- SOLUTION STARTS HERE ------ -------')disp('The graph crosses the x-axis between 0 and 1.')
xn=1; xn0=0; % Initial guesstol=.000001;count=0;disp(sprintf('The initial guesses are %g and %g.',xn0,xn));
fxn=f(xn);
8/10/2019 mathematics and computing- SC File
12/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 12
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));
elsexn=xn1;fxn=f(xn);fxn0=f(xn0);xn1=xn-fxn/(fxn-fxn0)*(xn-xn0);
endend
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
8/10/2019 mathematics and computing- SC File
13/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 13
OUTPUT:
8/10/2019 mathematics and computing- SC File
14/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 14
ASSIGNMENT 5
CROUTS METHOD
SOURCE CODE:
% Crout's method is used to find the solution of a system of% equations!
% INPUT - 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
% 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 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;
8/10/2019 mathematics and computing- SC File
15/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 15
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))*ZAinv=inv(U).*inv(L)
% END OF THE PROGRAM
8/10/2019 mathematics and computing- SC File
16/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 16
OUTPUT:
8/10/2019 mathematics and computing- SC File
17/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 17
8/10/2019 mathematics and computing- SC File
18/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 18
8/10/2019 mathematics and computing- SC File
19/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 19
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:3p=x1(l);
disp(sprintf('\n\nApplying Newton Forward Interpolation differenceformula for x = %g',x1(l)));
% Newton's Forward Interpolation Formula
n = length(x); % Calculating length of xa(1) = y(1);for k = 1 : n - 1
d(k, 1) = (y(k+1) - y(k))/(x(k+1) - x(k)); % Finite forwarddifference matrixend
8/10/2019 mathematics and computing- SC File
20/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 20
for j = 2 : n - 1for 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 : na(j) = d(1, j-1);
endDf(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);
endfp=sum(c);disp(sprintf('\nWe get value as %g',fp)); % Finding sum
disp(sprintf('\n\nApplying Newton Backward Interpolation differenceformula for x = %g',x1(l)));
% Newton's Backward Interpolation Formulafor i=1:n
diff(i,1)=y(i); % Finite backward difference operator matrixendfor j=2:n
for i=n:-1:jdiff(i,j)=diff(i,j-1)-diff(i-1,j-1);
end
endanswer=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;endanswer=answer+term*diff(n,i+1); % Finding sum
end
disp(sprintf('\nWe get value as %g',answer));end% END OF THE PROGRAM
8/10/2019 mathematics and computing- SC File
21/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 21
OUTPUT:
8/10/2019 mathematics and computing- SC File
22/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 22
8/10/2019 mathematics and computing- SC File
23/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 23
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 definitionx0=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 zeroa=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.
8/10/2019 mathematics and computing- SC File
24/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 24
% END OF THE PROGRAM
8/10/2019 mathematics and computing- SC File
25/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 25
OUTPUT:
8/10/2019 mathematics and computing- SC File
26/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 26
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)'); % Functionf0=f(0); % Value of the function at 0fn=f(1); % Value of the function at 1h=0.05;n=20; % No. of divisionsfx=h/3*(f0+fn);if mod(n,2)==0 % mod is used to find the remainder
for i=1:19k=i*h;if mod(i,2)==0
fx=fx+2*h/3*f(k); % Formula when i is a multiple of 2else
fx=fx+4*h/3*f(k); % Formula when i is not a multiple of 2end
endenddisp(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
8/10/2019 mathematics and computing- SC File
27/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 27
OUTPUT:
8/10/2019 mathematics and computing- SC File
28/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 28
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 followingf=inline('(cos(x)*log(sin(x)))/(1+(sin(x)^2))'); % Given functionf0=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);
endh2=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);
endR1=(4*Q2-Q1)/3;
8/10/2019 mathematics and computing- SC File
29/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 29
h3=h2/2;n3=(pi/2-pi/4)/h3;Q3=h3/3*(f0+fn); % Applying Simpson's ruleif mod(n3,2)==0 % mod is used to find remainderfor 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 2else
Q3=Q3+4*h3/3*f(pi/4 + k); % Formula for not i multiple of 2end
endendR2=(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
8/10/2019 mathematics and computing- SC File
30/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 30
OUTPUT:
8/10/2019 mathematics and computing- SC File
31/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 31
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 followingf=inline('sqrt(x+y)');fint=inline('2/3*sqrt((x+y)^3)');x0=0.4;% Initial value of xy0=0.41; % Initial value of y at x=0.4Y=y0;x1=0.6;for i=1:3
y1=Y;Y=y0+fint(x1,y1)-fint(x0,y1);
enddisp(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
8/10/2019 mathematics and computing- SC File
32/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 32
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
8/10/2019 mathematics and computing- SC File
33/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 33
OUTPUT:
8/10/2019 mathematics and computing- SC File
34/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 34
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 xy0=0.41;% Initial value of y at x=0.4x1=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
8/10/2019 mathematics and computing- SC File
35/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 35
OUTPUT:
8/10/2019 mathematics and computing- SC File
36/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 36
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 followingsyms x;f=inline('(x+y)^(1/2)');x0=0.4;% Initial value of xy0=0.41;% Initial value of y at x=0.4Y=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);
8/10/2019 mathematics and computing- SC File
37/38
www.mcdtu.wordpress.com
www.mcdtu.wordpress.com| 37
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
8/10/2019 mathematics and computing- SC File
38/38
www.mcdtu.wordpress.com
OUTPUT: