0% found this document useful (0 votes)
17 views

Homework Numerical Method

Uploaded by

amaliaula
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Homework Numerical Method

Uploaded by

amaliaula
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Homework 1

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.

guess a guess b midpoint c f(a) f(b) f(c) error <0.000005


1 1.5 1.25 -0.4402 1.47918 0.43933 0.5 NOT OK
1 1.25 1.125 -0.4402 0.43933 -0.0175 0.25 NOT OK
1.125 1.25 1.1875 -0.0175 0.43933 0.20631 0.125 NOT OK
1.125 1.1875 1.15625 -0.0175 0.20631 0.09329 0.0625 NOT OK
1.125 1.15625 1.140625 -0.0175 0.09329 0.03762 0.03125 NOT OK
1.125 1.14063 1.1328125 -0.0175 0.03762 0.00998 0.01563 NOT OK
1.125 1.13281 1.1289063 -0.0175 0.00998 -0.0038 0.00781 NOT OK
1.12891 1.13281 1.1308594 -0.0038 0.00998 0.0031 0.00391 NOT OK
1.12891 1.13086 1.1298828 -0.0038 0.0031 -0.0003 0.00195 NOT OK
1.12988 1.13086 1.1303711 -0.0003 0.0031 0.00138 0.00098 NOT OK
1.12988 1.13037 1.130127 -0.0003 0.00138 0.00052 0.00049 NOT OK
1.12988 1.13013 1.1300049 -0.0003 0.00052 8.6E-05 0.00024 NOT OK
1.12988 1.13 1.1299438 -0.0003 8.6E-05 -0.0001 0.00012 NOT OK
1.12994 1.13 1.1299744 -0.0001 8.6E-05 -2E-05 6.1E-05 NOT OK
1.12997 1.13 1.1299896 -2E-05 8.6E-05 3.2E-05 3.1E-05 NOT OK
1.12997 1.12999 1.129982 -2E-05 3.2E-05 5.3E-06 1.5E-05 NOT OK
1.12997 1.12998 1.1299782 -2E-05 5.3E-06 -8E-06 7.6E-06 NOT OK
1.12998 1.12998 1.1299801 -8E-06 5.3E-06 -1E-06 3.8E-06 OK

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

iteration xi-1 xi f(xi-1) f(xi) xi+1 error


number
0 1 1.5 -0.4402 1.47918
1 1.5 1.11468 1.47918 -0.0536 1.12816 0.01348
2 1.11468 1.12816 -0.0536 -0.0064 1.12999 0.00183
3 1.12816 1.12999 -0.0064 3E-05 1.12998 8.6E-06
4 1.12999 1.12998 3E-05 -2E-08 1.12998 4.9E-09
5 1.12998 1.12998 -2E-08 -5E-14 1.12998 1.3E-14
6 1.12998 1.12998 -5E-14 0 1.12998 0

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];

% Augment matrix A with vector B to perform simultaneous operations


AB = [A B];

% Number of equations
n = size(AB, 1);

% Forward elimination to form upper triangular matrix


for i = 1:n-1
for j = i+1:n
factor = AB(j,i) / AB(i,i);
AB(j,:) = AB(j,:) - factor * AB(i,:);
end
end

% Back substitution to find solution vector x


x = zeros(n, 1);
for i = n:-1:1
x(i) = (AB(i,end) - AB(i,i+1:end-1) * x(i+1:end)) / AB(i,i);
end

% Display the result


disp('Solution vector x is:');
disp(x);

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);

% Adjust B with the permutation matrix P


PB = P * B;

% Solve Ly = PB for y using forward substitution


y = L \ PB;

% Solve Ux = y for x using back substitution


x = U \ y;

% Display the result


disp('Solution vector x is:');
disp(x);

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];

% Perform the PA=LU factorization of A


[P, L, U] = lu(A);

% Solve the systems Ly = PB and Ux = y to find the solution vector x.


% First, solve for y using forward substitution
y = L\(P*B);

% Next, solve for x using back substitution


x = U\y;

% Display the result


disp('Solution vector x is:');
disp(x);

Result:
Solution vector x is:
-7.1396
3.2064
1.7674
5.8479
-7.7733
5.7873

Comparison of three methods:


• Gaussian Elimination → this method can be unstable if the pivot elements
are very small, which lead to large rounding errors.
• LU decomposition without pivoting → similar to Gaussian but factors the
matrix A into a lower triangular matrix and an upper triangular matrix U.
It assumes that all pivot elements are nonzero and does not handle near-
zero pivots, which can be instability.

5
Homework 1
Numerical Methods

• PA=LU decomposition with partial pivoting → this method is more stable


since it rearranges the rows A that the largest element of each column of
A is placed on the diagonal of U, thereby minimizing the potential for large
numerical errors.
After running the script, all methods yield similar results.
Comparison in Matlab:
function [x_gaussian, x_lu, x_palu] = solve_systems(A, B)
% Gaussian Elimination (No Pivoting)
function x = gaussian_elimination(A, B)
AB = [A, B]; % Augment A with B
n = size(A, 1);
% Forward elimination
for i = 1:n
for j = i+1:n
factor = AB(j,i) / AB(i,i);
AB(j,i:end) = AB(j,i:end) - factor * AB(i,i:end);
end
end
% Back substitution
x = zeros(n, 1);
for i = n:-1:1
x(i) = (AB(i,end) - AB(i,i+1:n) * x(i+1:n)) / AB(i,i);
end
end

% LU Decomposition (No Pivoting)


function x = lu_decomposition(A, B)
[L, U] = lu(A, 'vector');
% Forward substitution to solve L*y = B
y = L \ B;
% Back substitution to solve U*x = y
x = U \ y;
end

% LU Decomposition with Partial Pivoting (PA=LU)


function x = palu_decomposition(A, B)
[L, U, P] = lu(A);
% Solve L*y = P*B
y = L \ (P*B);
% Back substitution to solve U*x = y
x = U \ y;
end

x_gaussian = gaussian_elimination(A, B);


x_lu = lu_decomposition(A, B);
x_palu = palu_decomposition(A, B);
end

% 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;

6
Homework 1
Numerical Methods

7 2 6 10 4 2];
B = [4; 6; 7; 9; 4; 6];

% Solve the systems using different methods


[x_gaussian, x_lu, x_palu] = solve_systems(A, B);

% Display the results for comparison


disp('Solution from Gaussian Elimination:');
disp(x_gaussian);
disp('Solution from LU Decomposition (No Pivoting):');
disp(x_lu);
disp('Solution from LU Decomposition with Partial Pivoting (PA=LU):');
disp(x_palu);

Result:
Solution from Gaussian Elimination:
-7.1396
3.2064
1.7674
5.8479
-7.7733
5.7873

Solution from LU Decomposition (No Pivoting):


-7.1396
3.2064
1.7674
5.8479
-7.7733
5.7873

Solution from LU Decomposition with Partial Pivoting (PA=LU):


-7.1396
3.2064
1.7674
5.8479
-7.7733
5.7873

Validation of the three methods:


Inversion Three Validation
A B
Method Methods (B value)
5 10 10 1 8 8 4 -7.13959 -7.1396 3.9999
10 2 4 7 3 8 6 3.20643 3.2064 6.0002
6 2 6 7 7 8 7 1.76736 1.7674 7.0002
3 8 6 2 3 1 9 5.84786 5.8479 9
6 6 7 6 10 10 4 -7.77326 -7.7733 4
7 2 6 10 4 2 6 5.78730 5.7873 6.0004

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

Table 2 Chebyshev nodes (year) & corresponding CO2 value


Year 1850 1856 1896 1954 1994 2000
CO2 (tons) 2.0e8 2.6e8 1.5e9 6.7e9 2.3e10 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

% Define the years and CO2 data from Chebyshev nodes


years_cheb = [1850, 1856, 1896, 1954, 1994, 2000];
co2_cheb = [2.08, 2.68, 1.59, 6.79, 2.3, 2.6] * 1e9; % convert to tons

% Years to estimate CO2 emissions


years_to_estimate = [1915, 2010, 2050];

% Pre-allocate arrays to hold estimated emissions for each polynomial


degree
estimations_avg = zeros(length(years_to_estimate), 5);
estimations_cheb = zeros(length(years_to_estimate), 5);

% Calculate polynomial coefficients and estimate CO2 emissions using


average nodes
for degree = 1:5
p_coeffs_avg = polyfit(years_avg, co2_avg, degree);
estimations_avg(:, degree) = polyval(p_coeffs_avg,
years_to_estimate);
end

% Calculate polynomial coefficients and estimate CO2 emissions using


Chebyshev nodes
for degree = 1:5
p_coeffs_cheb = polyfit(years_cheb, co2_cheb, degree);
estimations_cheb(:, degree) = polyval(p_coeffs_cheb,
years_to_estimate);

9
Homework 1
Numerical Methods

end

% Calculate cubic spline interpolation and estimate CO2 emissions


spline_estimations_avg = spline(years_avg, co2_avg, years_to_estimate);
spline_estimations_cheb = spline(years_cheb, co2_cheb,
years_to_estimate);

% Actual emissions data


actual_emissions = [3.13, 33.31] * 1e9; % for 1915 and 2010 in tons

% 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);

% Display the results


disp('Estimated emissions using average nodes:');
disp(estimations_avg);
disp('Estimated emissions using Chebyshev nodes:');
disp(estimations_cheb);
disp('Cubic spline estimations using average nodes:');
disp(spline_estimations_avg);
disp('Cubic spline estimations using Chebyshev nodes:');
disp(spline_estimations_cheb);
disp('Error using average nodes:');
disp(error_avg);
disp('Error using Chebyshev nodes:');
disp(error_cheb);

% Plot the comparison graph


figure;
hold on;
plot(years_avg, co2_avg, 'bo', 'DisplayName', 'Average Node Data');
plot(years_cheb, co2_cheb, 'ro', 'DisplayName', 'Chebyshev Node Data');
for degree = 1:5
fplot(@(x) polyval(polyfit(years_avg, co2_avg, degree), x),
[min(years_avg) max(years_cheb)], 'DisplayName', ['Degree '
num2str(degree) ' Avg Nodes']);
fplot(@(x) polyval(polyfit(years_cheb, co2_cheb, degree), x),
[min(years_avg) max(years_cheb)], 'DisplayName', ['Degree '
num2str(degree) ' Cheb Nodes']);
end
fplot(@(x) spline(years_avg, co2_avg, x), [min(years_avg)
max(years_cheb)], 'k-', 'LineWidth', 2, 'DisplayName', 'Cubic Spline Avg
Nodes');
fplot(@(x) spline(years_cheb, co2_cheb, x), [min(years_avg)
max(years_cheb)], 'k--', 'LineWidth', 2, 'DisplayName', 'Cubic Spline
Cheb Nodes');
legend('Location', 'NorthWest');
xlabel('Year');
ylabel('CO2 Emissions (tons)');
title('CO2 Emissions Estimation Comparison');
hold off;

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

Table. Estimated emission by Chebyshev nodes


Year Degree 1 Degree 2 Degree 3 Degree 4 Degree 5 Cubic Spline Actual Error
Emissions
1915 2.93E+10 43900000000 35700000000 38000000000 34100000000 33000000000 31300000000 2760000000
2010 3.63E+10 22300000000 -1160000000 -500000000 57100000000 46540000000 3.331E+11 2.7604E+11
2050 3.92E+10 -8500000000 -2.39E+11 -1.56E+11 1.03E+12 4.468E+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.

Conclusion and recommendations:


• For interpolation (predicting within the range of known values), cubic spline
interpolation seems to provide the most reliable estimates.
• For extrapolation (predicting outside the range of known values), lower
degree polynomials might be preferable to avoid extreme predictions, but
even then, caution should be used as polynomial extrapolation can be highly
unreliable.
• One should consider using spline interpolation or perhaps more sophisticated
methods like regression models for time-series forecasting when making
predictions about future CO2 emissions.
• When selecting an interpolation method, it's crucial to consider not only the fit
to the existing data but also the behavior of the method outside the range of
known data points. In this context, Chebyshev nodes offer a better option for
polynomial interpolation compared to equally spaced nodes.

13

You might also like