Homework Numerical Method
Homework Numerical Method
Numerical Methods
Problem 1
Find the root of 𝑒𝑥+sin𝑥=4by at least three methods. Discuss your approaches.
1. Bisection method
a. Start by guessing the value of a and b, and find out the midpoint c.
b. Put the equation using value a, b, and c.
c. Check the error, and the error is set to less than 0.000005.
d. For the next iteration, using IF formula, to find out the next guess for a
and b.
e. Do the iteration until the error is less than 0.000005
f. The result for the root x = 1.1299801, with 18 iterations.
2. Secant method
a. Guess two of the x values, and find out the function.
b. For first iteration, use the formula of x before, and find out the function.
c. Check the error of each iteration using the x value of the initial iteration
and x value of the next iteration, the error should be 0.
d. The result for the root x = 1.12998, with 6 iterations.
1
Homework 1
Numerical Methods
3. Newton’s method
a. Guess one value of x, then calculate the function f(x) and f’(x).
b. For first iteration, the number calculate from 3 value of x, f(x) and f’(x).
c. Calculate the next value of x, and calculate its function.
d. Check the error of x value.
e. The result for the root x = 1.1299805, with 6 iterations.
iteration
xi f(xi) f'(xi) xi+1 f(xi+1) error
number
0 0 -3 2
1 1.5 1.479184 4.55243 1.1750779 0.16111 0.324922134
2 1.17507787 0.161115 3.62387 1.1306185 0.00225 0.044459409
3 1.13061846 0.002248 3.52367 1.1299806 4.5E-07 0.000637833
4 1.12998063 4.46E-07 3.52227 1.1299805 1.8E-14 1.26609E-07
5 1.1299805 1.78E-14 3.52227 1.1299805 0 5.10703E-15
6 1.1299805 0 3.52227 1.1299805 0 0
2
Homework 1
Numerical Methods
Problem 2
Solving x for 𝐴𝑥 = 𝐵
5 10 10 1 8 8
10 2 4 7 3 8
6 2 6 7 7 8
𝐴= and 𝐵 = [4 6 7 9 4 6]
3 8 6 2 3 1
6 6 7 6 10 10
[7 2 6 10 4 2]
1. Gaussian Method
Steps:
1) Form the upper triangular matrix from the equation
2) Back substitution to solve the variables
3) Iteratively improve the solution (if needed)
Using Matlab with code below
% Define the matrix A and vector B from the system Ax = B
A = [5 10 10 1 8 8;
10 2 4 7 3 8;
6 2 6 7 7 8;
3 8 6 2 3 1;
6 6 7 6 10 10;
7 2 6 10 4 2];
B = [4; 6; 7; 9; 4; 6];
% Number of equations
n = size(AB, 1);
3
Homework 1
Numerical Methods
Result:
Solution vector x is:
-7.1396
3.2064
1.7674
5.8479
-7.7733
5.7873
2. LU factorization
Steps:
1) Solve 𝐿𝑦 = 𝐵 for y
2) Solve 𝑈𝑥 = 𝑦 for x
Using Matlab with code below
% Define the matrix A and vector B from the system Ax = B
A = [5 10 10 1 8 8;
10 2 4 7 3 8;
6 2 6 7 7 8;
3 8 6 2 3 1;
6 6 7 6 10 10;
7 2 6 10 4 2];
B = [4; 6; 7; 9; 4; 6];
% Perform LU factorization
[L, U, P] = lu(A);
Result:
Solution vector x is:
-7.1396
3.2064
1.7674
5.8479
-7.7733
5.7873
4
Homework 1
Numerical Methods
3. PA=LU factorization
Steps:
Decomposing matrix A into three matrices: permutation matrix P, lower
triangular matrix L, and upper triangular matrix U.
Permutation matrix is used to rearrange the rows of A.
Using Matlab with the code below:
% Define the matrix A and vector B
A = [5 10 10 1 8 8;
10 2 4 7 3 8;
6 2 6 7 7 8;
3 8 6 2 3 1;
6 6 7 6 10 10;
7 2 6 10 4 2];
B = [4; 6; 7; 9; 4; 6];
Result:
Solution vector x is:
-7.1396
3.2064
1.7674
5.8479
-7.7733
5.7873
5
Homework 1
Numerical Methods
6
Homework 1
Numerical Methods
7 2 6 10 4 2];
B = [4; 6; 7; 9; 4; 6];
Result:
Solution from Gaussian Elimination:
-7.1396
3.2064
1.7674
5.8479
-7.7733
5.7873
7
Homework 1
Numerical Methods
Problem 3
Find the degree 1-5 interpolating polynomial of the data and use it to estimate the
CO2 emissions in (a) 1915, (b) 2010 and (c) 2050.
(The actual emissions in 1915 was 3.13 billion tons; the actual emissions in 2010
was 33.31 billion tons)
Evaluate the upper bound of error for your estimation.
Instead of polynomial, use Chebyshev and cubic spline interpolating.
Table 1 Average divided nodes (year) & corresponding CO2 value
Year 1850 1880 1910 1940 1970 2000
CO2 (tons) 2.0e8 8.5e8 3.0e9 4.9e9 1.5e10 2.6e10
Reference:
Hannah Ritchie and Max Roser (2020) - “CO₂ emissions” Published online at
OurWorldInData.org. Retrieved from: 'https://ourworldindata.org/co2-
emissions' [Online Resource]
Solving steps:
• Use polynomial interpolation to construct the degrees 1 through 5 using the
provided nodes and CO2 values. For each polynomial, estimation of CO2
emissions for the years 1915, 2010, and 2050, using MATLAB’s ‘polyfit’.
8
Homework 1
Numerical Methods
• Use the obtained polynomials to estimate CO2 emissions for the years 1915,
2010, and 2050.
• To evaluate the error, compare the estimated values with the actual emissions
provided for the years 1915 and 2010. The error bound for an interpolating
polynomial is given by:
(𝑓 (𝑛+1) (𝜉)) 𝑛
×∏ (𝑥 − 𝑥𝑖 )
(𝑛 + 1)! 𝑖=1
Where 𝑓 (𝑛+1) (𝜉) is the (𝑛 + 1)𝑡ℎ derivative of the function at some point ξ in
the interval of interpolation. We can estimate the error by comparing the
polynomial estimate to the known actual values.
• Use Chebyshev nodes and the cubic spline method to perform the interpolation
and then compare with the polynomial estimates.
• Create a comparison graph to visualize the differences between the polynomial,
Chebyshev, and cubic spline interpolations.
Matlab code for interpolating polynomials and estimate the emissions for the
years of interest:
% Define the years and CO2 data from average divided nodes
years_avg = [1850, 1880, 1910, 1940, 1970, 2000];
co2_avg = [2.08, 8.58, 3.09, 4.9, 1.5, 2.6] * 1e9; % convert to tons
9
Homework 1
Numerical Methods
end
% Calculate the error for 1915 and 2010 estimates using degree 5
polynomial (as an example)
error_avg = abs(estimations_avg(1:2, 5) - actual_emissions);
error_cheb = abs(estimations_cheb(1:2, 5) - actual_emissions);
10
Homework 1
Numerical Methods
Result:
Estimated emissions using average nodes:
1.0e+11 *
0.0395 0.0489 0.0557 0.0471 0.0312
0.0243 0.0073 0.0424 0.0124 0.1559
0.0179 -0.0302 0.1832 -0.1934 3.0521
Estimated emissions using Chebyshev nodes:
1.0e+11 *
0.0293 0.0439 0.0357 0.0380 0.0341
0.0363 0.0223 -0.0116 -0.0050 0.0571
0.0392 -0.0085 -0.2386 -0.1559 1.0330
Cubic spline estimations using average nodes:
1.0e+10 *
0.3017 0.7035 6.2658
Cubic spline estimations using Chebyshev nodes:
1.0e+10 *
0.3300 0.4654 4.4678
Error using average nodes:
1.0e+10 *
0.0011 3.0191
1.2459 1.7721
Error using Chebyshev nodes:
1.0e+10 *
0.0276 2.9904
0.2576 2.7604
11
Homework 1
Numerical Methods
Summary:
Table. Estimated emission by average nodes
Year Degree 1 Degree 2 Degree 3 Degree 4 Degree 5 Cubic Spline Actual Error
Emissions
1915 3.95E+10 4.89E+10 5.57E+10 4.71E+10 3.12E+10 30170000000 31300000000 1100000000
2010 2.43E+10 7.3E+09 4.24E+10 1.24E+10 1.56E+11 70350000000 3.331E+11 3.0191E+11
2050 1.79E+10 -3E+10 1.83E+11 -1.9E+11 3.05E+12 6.266E+11 N/A N/A
Discussion:
• Polynomial interpolation using average nodes shows a large range of estimates
for the year 2050 across different degrees, with the degree 5 polynomial
predicting an extremely high CO2 emission which seems unrealistic, indicating
a potential problem of polynomial oscillation at higher degrees known as
Runge's phenomenon.
• For the year 1915, the estimates are quite consistent and close to the actual
emissions, except for the cubic spline estimate, which is the closest.
• In 2010, the degree 5 polynomial estimate has a very high error, suggesting
that higher-degree polynomials may not always provide the best estimates for
data outside the interpolation range (extrapolation).
• Using Chebyshev nodes tends to yield better results as these nodes are chosen
to minimize the potential for Runge's phenomenon, and the estimates for 1915
and 2010 are generally closer to the actual emissions compared to average
nodes.
• However, the estimates for 2050 still show large variability and a high degree
polynomial also predicts an unrealistic number for emissions.
• Cubic spline interpolation provides a much smoother and more plausible
estimate, especially for 1915, where the estimate is very close to the actual
emissions. This indicates that spline interpolation may be more reliable for
data points that are within the range of known data.
12
Homework 1
Numerical Methods
• The estimates for 2050 using cubic splines are higher than the actual figures
for 2010, but not as drastically as the high degree polynomials, making it a
potentially more realistic estimate.
• The errors for the year 2010 are significantly higher than for the year 1915,
which indicates that interpolation methods tend to be more accurate for
intermediate values (1915) rather than extrapolation (2010).
• Degree 5 polynomials, especially using average nodes, show a considerable
overestimation for the year 2010, which can be attributed to polynomial
wiggle.
13