You are on page 1of 29

Engineering Science 204

Numerical Methods in Engineering


First Semester AY 2020-2021

MACHINE PROBLEM No. 1

NAME: REGINA M. LAGATIC STUDENT NO.: 201922598

As a student of the University of the Philippines, I pledge to act ethically and uphold the
value of honor and excellence.
I understand that suspected misconduct on this MP will be reported to the
appropriate office and if established, will result in disciplinary action in
accordance with University rules, policies and procedures. I may work with
others only to the extent allowed by the Instructor.

Signature:

GENERAL INSTRUCTIONS: Solve all the problems using appropriate numerical and programming
techniques, independently and completely. Cite all references or any assistance that you received during
the development of your solution. Submit a brief but comprehensive EXECUTIVE SUMMARY of
your solution to the problems.
SUBMISSION REQUIREMENTS: (1) Submit a copy of the EXECUTIVE SUM-MARY that
adheres to the prescribed format. The first page of which should be this questionnaire with your name,
student number, and signature. (2) A consolidated copy (Use the filename format: lastnameMP1.xxx
e.g. pdf, doc, zip) of the executive summary, source codes and references used in all the solutions
should be uploaded to the course UVLE. DUE DATE: 28 OCTOBER 2020 11:59 PM (ONLINE).
PROBLEM 1 [30 points]: Given the hypothetical data below:

x1234
y 12 0 3

Determine and plot the interpolating polynomial using the Lagrange and cubic spline methods. Comment
on the estimate at x=2.5 .

PROBLEM 2 [40 points]: Given the following experimental data,

x 0.10. 2 0.4 0.6 0.9 1.31.5 1.7 1.8


Page 1 of 29
y 0.75 1.251.45 1.25 0.85 0.55 0.35 0.280.18

Fit the data into the equation using non-linear least squares. DO NOT LINEARIZE the equation when
solving this problem.

y=∝ x e βx.
PROBLEM 3 [30 points]: Derive a three-point forward-difference approximation and a three-point
backward-difference approximation of the first derivative of f (x) that are second order accurate using
Taylor series. Use your formula to get the derivative of f ( x )=e x at x=1. By varying the step size h , plot
the error with respect to the step size. What value of h gives the least error? Show by plotting the error
with respect to h .

PROBLEM 4 [20 point]: The error function erf ⁡( x) appears in many solutions of classical physical
problems (for example, statistics, temperature, temperature distribution, diffusion and viscous flow
velocity distribution). The error function is given by

x
2
erf ( x )= ∫ exp (−t 2 ) dt
√π 0
Use three-point quadratures to estimate erf(0.5) using the trapezoidal rule, Simpson’s rule and Gaussian
quadrature. Compare your results using the three methods.

PROBLEM 5 [20 points]: Evaluate numerically

∫ 1+4x 2 dx
0

Use a non-adaptive method. Pick a method that requires the smallest number. Pick a method that requires
the smallest number of functional evaluation yet produces accurate results. Exact solution is
π=3.14159265357932385 . Match the results to the limit of your computer system.

PROBLEM 6 [40 points]: Evaluate

3
sin x
∫ dx
0 √ 1+ x2
Use an adaptive version of Newton-Cotes. Set tolerance = 1 x 10−6 .

Page 2 of 29
Engineering Science 204
Numerical Methods in Engineering
EXECUTIVE SUMMARY OF MACHINE PROBLEM 1

PART 1

PROBLEM NO.1

1. Problem Statement
Given the hypothetical data below:

x1234
y 12 0 3

Determine and plot the interpolating polynomial using the Lagrange and cubic spline methods.
Comment on the estimate at x=2.5 .

2. Method of solution

3. Partial Results
4. Problem encountered

PROBLEM NO.2

1. Problem Statement
Given the following experimental data,

x 0.10. 2 0.4 0.6 0.9 1.31.5 1.7 1.8


y 0.75 1.251.45 1.25 0.85 0.55 0.35 0.280.18

Fit the data into the equation using non-linear least squares. DO NOT LINEARIZE the equation when
solving this problem.

y=∝ x e βx.
2. Method of solution

To solve this problem numerically, use Gauss Newton Method for Non Linear Least squares wherein
it solves for a root of the gradient of the squared error or to clear it can be used to minimize the sum of
squares of errors between the data and the predictions.

In this problem, to compute the best least squares by Gauss-Newton Method, we need to define first
βx
the residual equation, which is r n =∝ x n e − y n. The derivatives of r evaluated at nine data points with
n

respect to the parameters ∝and β is 9 x 2 ¿. The partial derivative is called the Jacobian matrix.

Page 3 of 29
To minimize r 1 (x )2 + …+ r m (x )2 , set x 0=initial vector then the algorithm for Gauss-Newton is:

For k =0,1,2 …

A=Dr(x k ) where Dr is the Jacobian matrix


AT A v k =− AT r ( x k )
x k+1=x k + v k where v k is the value of parameters in k iteration
end

To solve using matlab program, the user will input the data and the function, its derivative,
maximum number of iteration and initial guess of ∝and β value.

3. Partial Results

After running the program:

∝=9.8974
β=−2.5319

Thus the curve equation is y=9.8974 x e−2.5319 x

Page 4 of 29
4. Problem encountered

PROBLEM NO.3

1. Problem Statement

Derive a three-point forward-difference approximation and a three-point backward-difference


approximation of the first derivative of f (x) that are second order accurate using Taylor series. Use
your formula to get the derivative of f ( x )=e x at x=1. By varying the step size h , plot the error with
respect to the step size. What value of h gives the least error? Show by plotting the error with respect
to h .

2. Method of solution

 Three Point Backward Difference (Derivation using Taylor Series of Expansion


h2 f ' ' ( x ) h3 f ' '' ( x )
f ( x−h )=f ( x )−h f ' ( x ) + − +… in [−h ,]  eq.1
2! 3!
' (2h)2 f ' ' ( x ) (2 h)3 f ' ' ' ( x )
f ( x−2 h )=f ( x )−2 h f ( x )+ − + … in [−2 h]  eq.2
2! 3!

Multiply 4 to Equation 1 and subtract from Equation 2.

4 h2 f ' ' ( x ) 4 h3 f ' ' ' ( x )


4 f ( x−h )=4 f ( x )−4 h f ' ( x ) + − +…
2! 3!
' (2h)2 f ' ' ( x ) (2 h)3 f ' ' ' ( x )
−¿ f ( x−2 h )=f ( x )−2 h f ( x )+ − +…
2! 3!

4 h3 f ' '' ( x )
'
4 f ( x−h )−f ( x−2 h ) =3 f ( x )−2 h f ( x ) + +…
3!

By simplifying, we will get:

3 f ( x )+ f ( x−2 h )−4 f ( x−h) 2 h2 f ' ' ' ( x )


f ' ( x )= +
2h 3!

3 f ( x )+ f ( x−2 h )−4 f (x−h)


where f ' ( x )= is the three-point backward difference formula and
2h
2h 2 f '' ' (x)
is the O(h) or the error form.
3!

To solve the problem numerically, the user will input the function on the matlab program and
initialize a wide range of increment size h using logspace function, to determine the error between the

Page 5 of 29
correct value of the derivative and the value based on the three point backward difference. Normally, the
error decreases as h decreases. To plot the, use the loglog function.

 Three Point Forward Difference (Derivation using Taylor Series of Expansion)


h2 f ' ' ( x ) h 3 f '' ' ( x )
f ( x +h ) =f ( x ) +h f ' ( x ) + + + … in [h ,]  eq.1
2! 3!
' (2 h)2 f ' ' ( x ) (2 h)3 f ' ' ' ( x )
f ( x +2 h )=f ( x )+2 h f ( x )+ + +… in [ 2h ]  eq.2
2! 3!

Multiply 4 to Equation 1 and subtract from Equation 2.

4 h2 f ' ' ( x ) 4 h3 f ' '' ( x )


4 f ( x+ h )=4 f ( x ) + 4 h f ' ( x ) + + +…
2! 3!
(2 h)2 f ' ' ( x ) (2 h)3 f ' ' ' ( x )
−¿ f ( x +2 h )=f ( x )+2 h f ' ( x )+ + +…
2! 3!

' 4 h3 f ' '' ( x )


4 f ( x+ h )−f ( x+2 h )=3 f ( x )+ 2h f ( x )− +…
3!

By simplifying, we will get:

4 f ( x+ h )−f ( x +2 h )−43(x ) 2 h2 f '' ' ( x)


f ' ( x )= +
2h 3!

4 f ( x+ h )−f ( x +2 h )−43(x )
where f ' ( x )= is the three-point forward difference formula and
2h
2h 2 f '' ' (x)
is the O(h) or the error form.
3!

To solve the problem numerically, the user will input the function on the matlab program and
initialize a wide range of increment size h using logspace function. The for loop is to determine the error
between the correct value of the derivative and the approximate derivative using the three point forward
difference within the specified range of h . Normally, the error decreases as h decreases.

3. Results

 Three point backward difference

By running the script, we will get:

Page 6 of 29
First derivative of the function
@(x)exp(x)

Exact value for first derivative at x=0 is 2.718282.


error when h= 0.1 is 0.008412
error when h= 0.0255955 is 0.000582
error when h= 0.00655129 is 0.000039
error when h= 0.00167683 is 0.000003
error when h= 0.000429193 is 0.000000
error when h= 0.000109854 is 0.000000
error when h= 2.81177e-05 is 0.000000
error when h= 7.19686e-06 is 0.000000
error when h= 1.84207e-06 is 0.000000
error when h= 4.71487e-07 is 0.000000
error when h= 1.20679e-07 is 0.000000
error when h= 3.08884e-08 is 0.000000
error when h= 7.90604e-09 is 0.000000
error when h= 2.02359e-09 is 0.000000
error when h= 5.17947e-10 is 0.000002
error when h= 1.32571e-10 is 0.000009
error when h= 3.39322e-11 is 0.000000
error when h= 8.68511e-12 is 0.000214
error when h= 2.223e-12 is 0.000595
error when h= 5.68987e-13 is 0.002512
error when h= 1.45635e-13 is 0.001724
error when h= 3.72759e-14 is 0.021838
error when h= 9.54095e-15 is 0.074453
error when h= 2.44205e-15 is 0.555032
error when h= 6.25055e-16 is 1.544598
error when h= 1.59986e-16 is 2.833324
error when h= 4.09492e-17 is 2.718282
error when h= 1.04811e-17 is 2.718282
error when h= 2.6827e-18 is 2.718282
error when h= 6.86649e-19 is 2.718282
error when h= 1.75751e-19 is 2.718282
error when h= 4.49843e-20 is 2.718282
error when h= 1.1514e-20 is 2.718282
error when h= 2.94705e-21 is 2.718282
error when h= 7.54312e-22 is 2.718282
error when h= 1.9307e-22 is 2.718282

Page 7 of 29
error when h= 4.94171e-23 is 2.718282
error when h= 1.26486e-23 is 2.718282
error when h= 3.23746e-24 is 2.718282
error when h= 8.28643e-25 is 2.718282
error when h= 2.12095e-25 is 2.718282
error when h= 5.42868e-26 is 2.718282
error when h= 1.3895e-26 is 2.718282
error when h= 3.55648e-27 is 2.718282
error when h= 9.10298e-28 is 2.718282
error when h= 2.32995e-28 is 2.718282
error when h= 5.96362e-29 is 2.718282
error when h= 1.52642e-29 is 2.718282
error when h= 3.90694e-30 is 2.718282
error when h= 1e-30 is 2.718282
The result shows that when h=7.19686e-06, it has the lowest error with 6.61604e-12.

 Three point forward difference

First derivative of the function


@(x)exp(x)

Exact value for first derivative at x=0 is 2.718282.


Page 8 of 29
error when h= 0.1 is 0.009773
error when h= 0.0255955 is 0.000605
error when h= 0.00655129 is 0.000039
error when h= 0.00167683 is 0.000003
error when h= 0.000429193 is 0.000000
error when h= 0.000109854 is 0.000000
error when h= 2.81177e-05 is 0.000000
error when h= 7.19686e-06 is 0.000000
error when h= 1.84207e-06 is 0.000000
error when h= 4.71487e-07 is 0.000000
error when h= 1.20679e-07 is 0.000000
error when h= 3.08884e-08 is 0.000000
error when h= 7.90604e-09 is 0.000000
error when h= 2.02359e-09 is 0.000000
error when h= 5.17947e-10 is 0.000002
error when h= 1.32571e-10 is 0.000012
error when h= 3.39322e-11 is 0.000026
error when h= 8.68511e-12 is 0.000093
error when h= 2.223e-12 is 0.000603
error when h= 5.68987e-13 is 0.002171
error when h= 1.45635e-13 is 0.004375
error when h= 3.72759e-14 is 0.001990
error when h= 9.54095e-15 is 0.111730
error when h= 2.44205e-15 is 0.536073
error when h= 6.25055e-16 is 0.123638
error when h= 1.59986e-16 is 2.718282
error when h= 4.09492e-17 is 2.718282
error when h= 1.04811e-17 is 2.718282
error when h= 2.6827e-18 is 2.718282
error when h= 6.86649e-19 is 2.718282
error when h= 1.75751e-19 is 2.718282
error when h= 4.49843e-20 is 2.718282
error when h= 1.1514e-20 is 2.718282
error when h= 2.94705e-21 is 2.718282
error when h= 7.54312e-22 is 2.718282
error when h= 1.9307e-22 is 2.718282
error when h= 4.94171e-23 is 2.718282
error when h= 1.26486e-23 is 2.718282
error when h= 3.23746e-24 is 2.718282
error when h= 8.28643e-25 is 2.718282

Page 9 of 29
error when h= 2.12095e-25 is 2.718282
error when h= 5.42868e-26 is 2.718282
error when h= 1.3895e-26 is 2.718282
error when h= 3.55648e-27 is 2.718282
error when h= 9.10298e-28 is 2.718282
error when h= 2.32995e-28 is 2.718282
error when h= 5.96362e-29 is 2.718282
error when h= 1.52642e-29 is 2.718282
error when h= 3.90694e-30 is 2.718282
error when h= 1e-30 is 2.718282
 
The result shows that when h=7.19686e-06, it has the lowest error with 6.61604e-12.

4. Problem encountered

PROBLEM NO.4

1. Problem Statement

The error function erf ⁡( x) appears in many solutions of classical physical problems (for example,
statistics, temperature, temperature distribution, diffusion and viscous flow velocity distribution). The
error function is given by
Page 10 of 29
x
2
erf ( x )= ∫ exp (−t 2 ) dt
√π 0
Use three-point quadratures to estimate erf(0.5) using the trapezoidal rule, Simpson’s rule and Gaussian
quadrature. Compare your results using the three methods.

2. Method of solution

f ' ' (co )


b
3
 Three point Trapezoidal Quadrature integration Rule is defined as ∫ f ( x ) dx=S[ a ,b ]−h
a 12
''
f ( co ) b−a
for a< c o <b where S[a , b]=(b−a)(f ( a )+ f ( b ) )/2 and h3 as the error term and h= .
12 2
Next, we subdivide the subinterval into two parts and then apply trapezoidal rule.
b '' ''
h3 f ( c 1 ) h3 f ( c 2 )
∫ f ( x ) dx=S[ a ,c]− 8 12 + S[ c, b]− 8 12 for a< c 1< c , c <c 2 <b.
a

b
If the |( S [ a , c ] + S [ c ,b ] ∓S [ a , b ] )|<3 tol then ∫ f ( x ) dx=S [a ,c ]+ S[ c ,b ]. If not, consider again half
a
1
intervals [a1 , b1 ] and [a2 , b 2] with tolerance tol= tol.
2
b
f (5) ( c o ) 5
 Three point Simpson’s rule is defined as ∫ f ( x ) dx=S[ a ,b ]−(b−a) for a< c o <b where
a 2880
b−a a+b f (5) ( c o )
S[a , b]=
6
[f ( a ) + 4 f
2 ( )
+ f ( b ) ] and (b−a)5
2880
as the error term. Same with

Trapezoidal Rules, Next, we subdivide the subinterval into two parts and then apply simpson’s
b (5) (5 )
h 5 f ( c 1) h5 f ( c2 )
rule. ∫ f ( x ) dx=S[ a ,c]− + S[c , b]− for a< c 1< c , c <c 2 <b.
a 32 90 32 90

b
If the |( S [ a , c ] + S [ c ,b ] ∓S [ a , b ] )|<15 tol then ∫ f ( x ) dx=S [a ,c ]+ S[ c ,b ]. If not consider again half
a
1
intervals [a1 , b1 ] and [a2 , b 2] with tolerance tol= tol.
2

 Three-point Gaussian Quadrature The objective is by changing variable, rewrite the integral over
b 1
[-1,1] using substitution. Reference formula is ∫ f ( x ) dx=∫ f
a −1
( ( b−a )2t+ b+a )( b−a2 ) dt . Let

2 x−a−b 2 dx b−a
t= ; therefore dt = and dx= dt . After getting the integrand as a function of
b−a b−a 2
, we can now use the three-point Gaussian Quadrature to evaluate the integral by using the table

Page 11 of 29
for coefficients and roots for Three point-Gaussian such as the integral will be now
1

∫ f ( t ) dt= 59 f (√ 35 )+ 89 f ( 0) + f (− √ 35 ) .
−1

3. Partial Results

After Running the code, here are the answer for each method:

THREE-POINT TRAPEZOIDAL QUADRATURE

THREE POINT SIMPSONS RULE

GAUSSIAN QUADRATURE

4. Problem encountered

Page 12 of 29
PROBLEM NO.5

1. Problem Statemsent

Evaluate numerically

∫ 1+4x 2 dx
0

Use a non-adaptive method. Pick a method that requires the smallest number. Pick a method that requires
the smallest number of functional evaluation yet produces accurate results. Exact solution is
π=3.14159265357932385 . Match the results to the limit of your computer system.

2. Method of solution

Composite Trapezoidal Rule is the non-adaptive method used in solving this problem numerically,
simply summing up the trapezoidal rule approximations on adjacent subintervals between the limit a
and b (integration limit). Increase the number of panels to get close to the correct integral value and
have a least error.

To start with the trapezoidal rule, set h=b−a , and Let as S=1/2 ¿; and do the iteration process. We
(b−a)
divide the interval [a, b] into sub-intervals or n panels and therefore h= . If |Sn−S n−1|< tol,
2
stop the iteration and state that Sn is the approximation to the integral.

To solve, the user will input the integral limit and number of panels in the matlab program, and use
vpa command to get the desired precision of value.

3. Partial Results

After running the matlab script:

4. Problem encountered

PROBLEM NO.6

1. Problem Statement
Page 13 of 29
Evaluate

3
sin x
∫ dx
0 √ 1+ x2
Use an adaptive version of Newton-Cotes. Set tolerance = 1 x 10−6 .

2. Method of solution

Apply Simpson’s rule on interval [a , b] with tolerance ( tol ) . Evaluate the integral using Simpsons
rule over sub intervals [a1 ,b 1 ] and [a2 , b2 ]. If accuracy test
1
S ( a , b ) + S ( a 2 , b2 ) −S (a 0 , b 0)|<tol is satisfied then stop. Accept S ( a1 , b1 ) + S ( a 2 , b2 )as
15 | 1 1
approximation of [a , b]. If not, consider subintervals [a1 ,b 1 ] and [a2 , b2 ] with tolerance
1
tol= (tol) . Then repeat procedure by applying simpsons rule on each subintervals and
2
subsubintervals.

3. Partial Results

4. Problem encountered

PART 2: APPENDIX

A. PROBLEM NUMBER 1
A.1 SOURCE CODE

function y= lagrange(x,xdata,ydata)

n=size(xdata,2);
L=ones(n,size(x,2));
for i=1:n

Page 14 of 29
for j=1:n
if(i~=j)
L(i,:)=L(i,:).*(x-xdata(j))/(xdata(i)-xdata(j));
end
end
y=0;
for i=1:n
y=y+ydata(i)*L(i,:);
end
end

Plot and value at x=2.5

clc;
clear;
xdata = [1 2 3 4];
ydata = [1 2 0 3];
x=2.5;
y=lagrange(x,xdata,ydata)

xdata = [1 2 3 4];
ydata = [1 2 0 3];
x1 = 1:4;
n=length(x1);
for i=1:n
y0(i)=lagrange(x1(i),xdata,ydata);
end
plot(x1,y0)
hold on
plot(xdata, ydata, 'bo', x, y, 'ro')
axis([0 6 -2 6])
title('Lagrange Interpolating Polynomial')
xlabel('x')
ylabel('y')
text(x,y,'\leftarrow x=2.5,y=0.875')

%Natural cubic spline interpolation

clc;
clear;
x = [1 2 3 4];
y = [1 2 0 3];
pp=csapi(x,y,2.5)
disp(pp)
xx = 0:0.25:5;
yy = csapi(x,y,xx);
plot(x,y,'o',xx,yy);
Page 15 of 29
title('Lagrange Interpolating Polynomial')
xlabel('x')
ylabel('y')
hold on
plot(2.5,pp,'ro')
text(2.5,pp,'\leftarrow x=2.5,y=0.875')

A.2 RESULT

Page 16 of 29
A.3 REFERENCE

B. PROBLEM NUMBER 2

B.1 SOURCE CODE

function x=GN(f,x,NumIters)
[y,dy]=f(x);
for j=1:NumIters
s=(dy'*dy)\(dy'*y);
x=x-s;
[y,dy]=f(x);
end

function [f,df]=fun2(z)
a=z(1); b=z(2);
r=[0.75;1.25;1.45;1.25;0.85;0.55;0.35;0.28;0.18];
x=[0.1;0.2;0.4;0.6;0.9;1.3;1.5;1.7;1.8];
f=(r-a*x.*exp(b*x));
df(:,1)=-x.*exp(b*x);
df(:,2)=-a*(x.^2).*exp(b*x);
end

clear; clc
z=GN(@fun2,[10;1],200);
a=z(1); b=z(2);
r=[0.75;1.25;1.45;1.25;0.85;0.55;0.35;0.28;0.18];
x=[0.1;0.2;0.4;0.6;0.9;1.3;1.5;1.7;1.8];
z
e=r-z(1)*x.*exp(x*z(2));
rmse=sqrt(sum((e-mean(e)).^2)/9);
figure(1)
plot(x,a*x.*exp(b*x),x,r,'*');
legend('Approximate','Points');

B.2 RESULT

Page 17 of 29
B.3 REFERENCE

C. PROBLEM NUMBER 3

A.1 Source Code


%%Matlab code for log log plot of first derivative
clc
clear

%function for which derivative have to find


f=@(x) exp(x);

%function for exact derivative


df= @(x) exp(x);
%first derivative of the function
fprintf('First derivative of the function \n')
disp(df)
Page 18 of 29
%initial value of h using logspace
hh=logspace(-1,-30,50);

%formula for three-point backward difference


threeptbackward=@(x,h) (3*f(x)+f(x-(2*h))-4*f(x-h))/(2*h);

%where x0=0
x0=1;
val=df(x0);

fprintf('Exact value for first derivative at x=0 is %f.\n',val);

%loop for error for different h


for i=1:length(hh)
dff(i)=threeptbackward(x0,hh(i));
error(i)=abs(val-dff(i));
fprintf('error when h= %g is %8.6f\n',hh(i),error(i));

end

%plotting error vs. h


loglog(hh,error,'o','MarkerFaceColor',[0 0.447 0.741])
xlabel('delta x (h)')
ylabel('Error')
grid("on")
title('Comparison of errors vs. h for central difference')

A.2 RESULTS

Three point backward difference

First derivative of the function


@(x)exp(x)

Exact value for first derivative at x=0 is 2.718282.


error when h= 0.1 is 0.008412
error when h= 0.0255955 is 0.000582
error when h= 0.00655129 is 0.000039
error when h= 0.00167683 is 0.000003
error when h= 0.000429193 is 0.000000
error when h= 0.000109854 is 0.000000
Page 19 of 29
error when h= 2.81177e-05 is 0.000000
error when h= 7.19686e-06 is 0.000000
error when h= 1.84207e-06 is 0.000000
error when h= 4.71487e-07 is 0.000000
error when h= 1.20679e-07 is 0.000000
error when h= 3.08884e-08 is 0.000000
error when h= 7.90604e-09 is 0.000000
error when h= 2.02359e-09 is 0.000000
error when h= 5.17947e-10 is 0.000002
error when h= 1.32571e-10 is 0.000009
error when h= 3.39322e-11 is 0.000000
error when h= 8.68511e-12 is 0.000214
error when h= 2.223e-12 is 0.000595
error when h= 5.68987e-13 is 0.002512
error when h= 1.45635e-13 is 0.001724
error when h= 3.72759e-14 is 0.021838
error when h= 9.54095e-15 is 0.074453
error when h= 2.44205e-15 is 0.555032
error when h= 6.25055e-16 is 1.544598
error when h= 1.59986e-16 is 2.833324
error when h= 4.09492e-17 is 2.718282
error when h= 1.04811e-17 is 2.718282
error when h= 2.6827e-18 is 2.718282
error when h= 6.86649e-19 is 2.718282
error when h= 1.75751e-19 is 2.718282
error when h= 4.49843e-20 is 2.718282
error when h= 1.1514e-20 is 2.718282
error when h= 2.94705e-21 is 2.718282
error when h= 7.54312e-22 is 2.718282
error when h= 1.9307e-22 is 2.718282
error when h= 4.94171e-23 is 2.718282
error when h= 1.26486e-23 is 2.718282
error when h= 3.23746e-24 is 2.718282
error when h= 8.28643e-25 is 2.718282
error when h= 2.12095e-25 is 2.718282
error when h= 5.42868e-26 is 2.718282
error when h= 1.3895e-26 is 2.718282
error when h= 3.55648e-27 is 2.718282
error when h= 9.10298e-28 is 2.718282
error when h= 2.32995e-28 is 2.718282

Page 20 of 29
error when h= 5.96362e-29 is 2.718282
error when h= 1.52642e-29 is 2.718282
error when h= 3.90694e-30 is 2.718282
error when h= 1e-30 is 2.718282
 

Three point forward difference

%%Matlab code for log log plot of first derivative


clc
clear

%function for which derivative have to find


f=@(x) exp(x);

%function for exact derivative


df= @(x) exp(x);
%first derivative of the function
fprintf('First derivative of the function \n')
disp(df)

Page 21 of 29
%initial value of h using logspace
hh=logspace(-1,-30,50);

%formula for three-point forward difference


threeptforward=@(x,h) (4*f(x+h)-f(x+(2*h))-3*f(x))/(2*h);

%where x0=0
x0=1;
val=df(x0);

fprintf('Exact value for first derivative at x=0 is %f.\n',val);

%loop for error for different h


for i=1:length(hh)
dff(i)=threeptforward(x0,hh(i));
error(i)=abs(val-dff(i));
fprintf('error when h= %g is %8.6f\n',hh(i),error(i));
end

%plotting error vs. h


loglog(hh,error,'o','MarkerFaceColor',[0 0.447 0.741])
xlabel('delta x (h)')
ylabel('Error')
grid("on")
title('Comparison of errors vs. h for central difference')

Three point forward difference

First derivative of the function


@(x)exp(x)

Exact value for first derivative at x=0 is 2.718282.


error when h= 0.1 is 0.009773
error when h= 0.0255955 is 0.000605
error when h= 0.00655129 is 0.000039
error when h= 0.00167683 is 0.000003
error when h= 0.000429193 is 0.000000
error when h= 0.000109854 is 0.000000
error when h= 2.81177e-05 is 0.000000
error when h= 7.19686e-06 is 0.000000
error when h= 1.84207e-06 is 0.000000
Page 22 of 29
error when h= 4.71487e-07 is 0.000000
error when h= 1.20679e-07 is 0.000000
error when h= 3.08884e-08 is 0.000000
error when h= 7.90604e-09 is 0.000000
error when h= 2.02359e-09 is 0.000000
error when h= 5.17947e-10 is 0.000002
error when h= 1.32571e-10 is 0.000012
error when h= 3.39322e-11 is 0.000026
error when h= 8.68511e-12 is 0.000093
error when h= 2.223e-12 is 0.000603
error when h= 5.68987e-13 is 0.002171
error when h= 1.45635e-13 is 0.004375
error when h= 3.72759e-14 is 0.001990
error when h= 9.54095e-15 is 0.111730
error when h= 2.44205e-15 is 0.536073
error when h= 6.25055e-16 is 0.123638
error when h= 1.59986e-16 is 2.718282
error when h= 4.09492e-17 is 2.718282
error when h= 1.04811e-17 is 2.718282
error when h= 2.6827e-18 is 2.718282
error when h= 6.86649e-19 is 2.718282
error when h= 1.75751e-19 is 2.718282
error when h= 4.49843e-20 is 2.718282
error when h= 1.1514e-20 is 2.718282
error when h= 2.94705e-21 is 2.718282
error when h= 7.54312e-22 is 2.718282
error when h= 1.9307e-22 is 2.718282
error when h= 4.94171e-23 is 2.718282
error when h= 1.26486e-23 is 2.718282
error when h= 3.23746e-24 is 2.718282
error when h= 8.28643e-25 is 2.718282
error when h= 2.12095e-25 is 2.718282
error when h= 5.42868e-26 is 2.718282
error when h= 1.3895e-26 is 2.718282
error when h= 3.55648e-27 is 2.718282
error when h= 9.10298e-28 is 2.718282
error when h= 2.32995e-28 is 2.718282
error when h= 5.96362e-29 is 2.718282
error when h= 1.52642e-29 is 2.718282

Page 23 of 29
error when h= 3.90694e-30 is 2.718282
error when h= 1e-30 is 2.718282
 

A.3 REFERENCE MATERIAL

D. PROBLEM NUMBER 4

D.1.1 THREE POINT TRAPEZOIDAL QUADRATURE

%Compute approximation to definite integral


%f specifying integrand,
%a, b integration interval,
% err or tol tolerance limit
%count - number of times the integrand is evaluated
Page 24 of 29
%it is only used in recursive calls
%Output: approximate definite integral
% - number of times the integrand was evaluated
function [q,p]=threepointtrap(f,a,b,tol,count)
if (nargin<5)
count=1;
end
p=count+1;
sab=S(f,a,b);
c=0.5*(a+b);
sac=S(f,a,c);
scb=S(f,c,b);
factor=3;
if (abs(sac+scb-sab)<factor*tol)
q=sac+scb;
else
[q1,p]=threepointtrap(f,a,c,tol/2,p);
[q2,p]=threepointtrap(f,c,b,tol/2,p);
q=q1+q2;
end
function q=S(f,a,b)
q=(b-a)/2*(feval(f,a)+feval(f,b));
format long

D.1.2 THREE POINT SIMPSON RULE


%compute approximation to definite integral
%f specifying integrand,
%a,b integration interval,
%err r tol tolerance limit
%count - number of times the integrand is evaluated
%it is only used in recursive calls
%output: approximate definite integral
%count1-number of times the integrand was evaluated
function [q,count1]=threepointsimpson(f,a,b,tol,count)
if (nargin<5)
count=0;
end
count1=count+9;
sab = S(f,a,b);
c = 0.5*(a+b);
sac = S(f,a,c);
scb = S(f,c,b);
factor=15;
if(abs(sac+scb-sab)<factor*tol)
q=sac+scb;
else
[q1,count1]=threepointsimpson(f,a,c,tol/2,count1);
[q2,count1]=threepointsimpson(f,c,b,tol/2,count1);

Page 25 of 29
q=q1+q2;
end
function q=S(f,a,b)
q=(b-a)/6*(feval(f,a) + 4*feval(f,(a+b)/2) + feval(f,b));
format long

D.1.3 GUASSIAN QUADRATURE


x=[sqrt(3/5), 0, -sqrt(3/5)];
c=[5/9, 8/9, 5/9];
a=0;
b=0.5;
tildec=(b-a)/2*c;
tildex=(b-a)/2*x + (b+a)/2;
f= (2/sqrt(pi)).*(exp(-tildex.^2));
value=sum(tildec.*f);

D.2 RESULTS
D.2.1 TRAPEZOIDAL RULE
f=@(x) (2/sqrt(pi))*(exp(-x^2));
a=0;
b=0.5;
tol=.5e-9
threepointtrap(f,a,b,tol)

D.2.2 SIMPSON RULE


f=@(x) (2/sqrt(pi))*(exp(-x^2));
a=0;
b=0.5;
tol=.5e-9
threepointsimpson(f,a,b,tol)

Page 26 of 29
D.2.3 GUASSIAN QUDRATURE

E. PROBLEM 5
E.1 SOURCE CODE
function s = trap(f,a,b,m)
%input-f is the integral
% a and b are upper and lower limits of integraton
%m is the panel or number of integrals
%s is the trapezoidal rule sum
h= (b-a)/m;
s=0;
for k=1:(m-1)
x=a+h*k;
s=s+feval(f,x);
end

s=h*(feval(f,a)+feval(f,b))/2+h*s;
digitsOld=digits(500);
vpa(s)
digits(digitsOld)

E.2 RESULT
clear
clc
f=@(x) 4/(1+x^(2));
Page 27 of 29
a=0;
b=1;
m=1000000;
trap(f,a,b,m)

F. PROBLEM 6
F.1 SOURCE CODE
%Compute approximation to definite integral using Simpson's rule
%f specifying integrand
%a,b integration interVAL,
%err of tol tolerance limit
%count-number of timees the integrand is evaluated
%it is only used in recursive calls
%output: approximate definite integral
%p- total number of times the integrand was evaluated
function [q,p]=adapt(f,a,b,tol,count)
if (nargin<5)
count=1;
end
p=count+1;
sab = S(f,a,b);
c = 0.5*(a+b);
sac = S(f,a,c);
scb = S(f,c,b);
factor=15;
if (abs(sac+scb-sab) < factor*tol)
q = sac + scb;
else
[q1,p]=adapt(f,a,c,tol/2,p);
[q2,p]=adapt(f,c,b,tol/2,p);
q=q1+q2;
end
end

function qr=S(f,a,b)
qr=(b-a)/6*(feval(f,a) + 4*feval(f,(a+b)/2) + feval(f,b));
end
F.2 RESULT

Page 28 of 29
f=@(x) (sin(x)/sqrt(1+x^(2)));
a=0;
b=3;
tol=1e-6
[q,p]=adapt(f,a,b,tol)
format long

Page 29 of 29

You might also like