Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

TECHNOLOGICAL INSTITUTE OF THE PHILIPINES

938 Aurora Blvd., Cubao, Quezon City

CE 007 (Numerical Solutions to CE Problems)


CE31S2

Machine Problem 3.1

Interpolations and Approximations

Submitted by:
Santos, Michelle Anne F.
1820027

ACADEMIC INTEGRITY PLEDGE

I swear on my honor that I did not use any inappropriate aid, nor give such to others, in accomplishing
this coursework. I understand that cheating and/or plagiarism is a major offense, as stated in TIP
Memorandum No. P-04, s. 2017-2018, and that I will be sanctioned appropriately once I have committed
such acts.

Santos, Michelle Anne F.


1820027
Machine Exercise:
A. Make an Algorithm for interpolating, approximating, and root-finding using the methods below:
a. Lagrange's Method
1. Start the program.
2. Read number of data (n)
3. Read data Xi and Yi for i=1 to n
4. Read value of independent variables say xp whose corresponding value of dependent say yp is
to be determined.
5. Initialize: yp = 0
6. For i = 1 to n
• Set p = 1
• For j =1 to n
• If i ≠ j then
• Calculate p = p * (xp - Xj)/(Xi - Xj)
• End If
• Next j
• Calculate yp = yp + p * Yi
• Next i
7. Display value of yp as interpolated value.
8. Stop the program.

b. Newton's Method
1. Start the program.
2. Initialize x and xo
3. For k = 0,1,2, . . . do
4. if f(xk) is sufficiently small then
5. Calculate x* = xk
6. Display value of x*
7. Calculate xi = xi-1 - f(xi-1)/f '(xi-1)
8. if |xk+1 − xk| is sufficiently small then
9. x* = xk+1
10. Display value of x*
11. Stop the program.
c. Neville's Method
1. Start the program.
2. Order interpolation points in x.
3. Set Pi = yi
4. Determine C1,i, D1,i (linear interpolation)
5. Check convergence.
6. If not adequate construct P01
7. Construct Cm,i, Dm,i
8. Check for convergence, if too large.
9. Construct P01...m; loop to 6
10. If OK, Stop and get error estimate.

d. Interpolation with Cubic Spline


e. Linear Fit
1. Start the program.
2. Read Number of Data (n)
3. For i=1 to n:
• Read Xi and Y
• Next i
4. Initialize:
5. sumX = 0
• sumX2 = 0
• sumY = 0
• sumXY = 0
6. Calculate Required Sum
7. For i=1 to n:
• sumX = sumX + Xi
• sumX2 = sumX2 + Xi * Xi
• sumY = sumY + Yi
• sumXY = sumXY + Xi * Yi
• Next i
8. Calculate Required Constant a and b of y = a + bx:
• b = (n * sumXY - sumX * sumY)/(n*sumX2 - sumX * sumX)
• a = (sumY - b*sumX)/n
9. Display value of a and b
10. Stop the program.

f. Polynomial Fit
1. Start the program.
2. Input order of polynomial to be fit, m.
3. Input number of data points, n.
4. If n < m+1, print out an error message that regression is impossible and terminate the process.
If n is greater than or equal to m+1, continue.
5. Compute the elements of the normal equation in the form of augmented matrix.
6. Solve the augmented matrix for the coefficients ao, a1, a2, . . an, using an elimination method.
7. Print out the coefficients.
8. Stop the program.

g. Exponential Fit
1. Obtain an approximation of b
2. Using b, obtain an approximation of a
3. Using a,b, obtain an approximation of c
4. Discard a,b and replace y = ae^(bx) + c ....by.... y - c = ae^(bx)
5. ln(y - c) = ln(a) + bx ...........a linear function
6. Call the least square method to find a,b of the best fitting line
h. Bisection Method
1. Start the program.
2. Define function f(x)
3. Choose initial guesses x0 and x1 such that f(x0)f(x1) < 0
4. Choose pre-specified tolerable error e.
5. Calculate new approximated root as x2 = (x0 + x1)/2
6. Calculate f(x0)f(x2)
• if f(x0)f(x2) < 0 then x0 = x0 and x1 = x2
• if f(x0)f(x2) > 0 then x0 = x2 and x1 = x1
• if f(x0)f(x2) = 0 then goto (8)
7. if |f(x2)| > e then goto (5) otherwise goto (8)
8. Display x2 as root.
9. Stop the program.

i. False Position Methods


1. Start the program.
2. Find points x0 and x1 such that x0<x1 and f(x0)⋅f(x1)<0.
3. Take the interval [x0,x1] and find next value using
• Formula-1 : x2=x0-f(x0)⋅(x1-x0 / f(x1)-f(x0))
• Formula-2 : x2= (x0⋅f(x1)-x1⋅f(x0)) / (f(x1)-f(x0))
• Formula-3 : x2=x1-f(x1)⋅(x1-x0) / (f(x1)-f(x0))
• (Using any of the formula, you will get same x2 value)
4. If f(x2)=0 then x2 is an exact root,
• else if f(x0)⋅f(x2)<0 then x1=x2,
• else if f(x2)⋅f(x1)<0 then x0=x2.
5. Repeat steps 2 & 3 until f(xi)=0 or |f(xi)|≤Accuracy
6. Stop the program.

j. Secant Method
1. Find points x0 and x1 such that x0<x1 and f(x0)⋅f(x1)<0.
2. Find next value using
• Formula-1 : x2=x0-f(x0)⋅(x1-x0 / f(x1)-f(x0))
• Formula-2 : x2= (x0⋅f(x1)-x1⋅f(x0)) / (f(x1)-f(x0))
• Formula-3 : x2=x1-f(x1)⋅(x1-x0) / (f(x1)-f(x0))
• (Using any of the formula, you will get same x2 value)
3. If f(x2)=0 then x2 is an exact root,
• Else x0=x1 and x1=x2
4. Repeat steps 2 & 3 until f(xi)=0 or |f(xi)|≤Accuracy

k. Newton-Raphson Method
1. Start the program.
2. Read x, e, n, d
• *x is the initial guess
• e is the absolute error i.e the desired degree of accuracy
• n is for operating loop
• d is for checking slope*
3. Do for i =1 to n in step of 2
4. f = f(x)
5. f1 = f'(x)
6. If ( [f1] < d), then display too small slope and goto 11.
• *[ ] is used as modulus sign*
7. x1 = x – f/f1
8. If ( [(x1 – x)/x1] < e ), the display the root as x1 and goto 11.
• *[ ] is used as modulus sign*
9. x = x1 and end loop
10. Display method does not converge due to oscillation.
11. Stop the program.
B. Solve the following problems (you can use hand calculation).
1. Use Lagrange interpolation and Newton interpolation methods to find a cubic polynomial that
interpolates the following data points: {(−2, −3), (−1, 1), (0, −1), (1, −3)}. After applying each
of these methods, convert the polynomial to the standard form P3(x) = a0 + a1x + a2x2 + a3x3
(if it is not already in this form).
2. Given the set of data points {(−3, −1), (−2, 1), (0, 2), (1, 3), (3, 2)}, fit a straight line y = mx + b
that approximates the points in the least-squares sense.
(a) Write a least-squares system Ax ≈ b which can be used to obtain values for m and b.
(b) Solve the obtained system above using the method of normal equations.
Machine Problem:
1. Use Newton’s method to
(a) Create a program that computes for the power am/n given a real number a. You are not allowed to use
roots or raise numbers to fractional powers in the formula. [Hint: This value is the solution of xn − am = 0.]

#include <iostream>
using namespace std;

int main()
{
int exponent;
float base, result = 1;

cout << "Enter base and exponent respectively: ";


cin >> base >> exponent;

cout << base << "^" << exponent << " = ";

while (exponent != 0) {
result *= base;
--exponent;
}

cout << result;

return 0;
}
(b) Create a program that computes for arcsin(x) of a given number x ∈ [0, 1]. You are not allowed to use
inverse trigonometric functions in the formula but trigonometric functions such as sin, cos, tan are
allowed.
// CPP code to illustrate
// the use of asin function
#include <bits/stdc++.h>
using namespace std;

#define PI 3.14159265

int main()
{
double x, ret, val;

// Take any value between [0, 1]


cout << "Enter value for x from any real number between 0 to 1: ";
cin >> x;

// asin() returns value in the range of [-?/2,?/2]


ret = asin(x);
val = (ret * 180) / PI;
cout << "The arcsine of " << x << " is " << ret
<< " radians or " << val << " degrees";

return 0;
}
References:
Anonymous, (April 20, 2014). Newton Raphson Method Algorithm and Flowchart. Retrieved from:
https://www.codewithc.com/newton-raphson-method-algorithm-flowchart/
Anonymous, (2021). Retrieved from:
https://atozmath.com/example/CONM/Bisection.aspx?he=e&q=se
Anonymous, (2021). Lagrange Interpolation Method Algorithm (Step Wise). Retrieved from:
https://www.codesansar.com/numerical-methods/lagrange-interpolation-method-algorithm.htm
Anonymous. Exponential Curve Fitting. Retrieved from:
http://www.davdata.nl/math/expfitting.html
Gupta, Akash. (October 30, 2020). asin() and atan() functions in C/C++ with Example. Retrieved
from: https://www.geeksforgeeks.org/asin-atan-functions-cc-example/
Weisstein, Eric W. (April 2021). Normal Equation. From MathWorld--A Wolfram Web
Resource. https://mathworld.wolfram.com/NormalEquation.html

You might also like