Professional Documents
Culture Documents
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 .
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.
∫ 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.
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,
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 …
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
∝=9.8974
β=−2.5319
Page 4 of 29
4. Problem encountered
PROBLEM NO.3
1. Problem Statement
2. Method of solution
4 h3 f ' '' ( x )
'
4 f ( x−h )−f ( x−2 h ) =3 f ( x )−2 h f ( x ) + +…
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.
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
Page 6 of 29
First derivative of the function
@(x)exp(x)
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.
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
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:
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
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
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')
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
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
%where x0=0
x0=1;
val=df(x0);
end
A.2 RESULTS
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
Page 21 of 29
%initial value of h using logspace
hh=logspace(-1,-30,50);
%where x0=0
x0=1;
val=df(x0);
Page 23 of 29
error when h= 3.90694e-30 is 2.718282
error when h= 1e-30 is 2.718282
D. PROBLEM NUMBER 4
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.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)
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