14032401-4 - Numerical Analysis
14032401-4 - Numerical Analysis
14032401-4
TABLE OF CONTENTS
3
14032401-4 Numerical Methods for Computing
4
14032401-4 Numerical Methods for Computing
Experiment No. 1
Introduction to MATLAB
Objective:
The purpose of this tutorial is to acquaint the students with basics of MATLAB. The topics
discussed in this lab includes Command Window, numbers and arithmetic operations,
Relations and Logical Operations, some built-in functions, some Commands, The m – files,
Vectors and Matrices loops and Conditional Statements.
Introduction:
The name MATLAB stands for MATrix LABoratory. MATLAB was written originally to
provide easy access to matrix software developed by the LINPACK (linear system package)
and EISPACK (Eigen system package) projects.
5
14032401-4 Numerical Methods for Computing
To close MATLAB type exit in the Command Window and next press Enter.
Comments %
✓ The percentage sign % is used to indicate that line is a comment.
✓ It is best to include comments not only at the top of a program, but also with each
section.
✓ For a script program it is often helpful to include the name of the program at the
beginning.
✓ All comments are ignored by MATLAB.
✓ They are added to improve readability of the code.
6
14032401-4 Numerical Methods for Computing
Help Browser:
✓ MATLAB has thousands of built in functions and various capabilities. Therefore you
should always search around for what has been done before writing a program.
✓ Searching in the Help window is usually the best way
✓ If you know the name of a MATLAB function but are not sure what it does, type in
the command windown command
Examples:
Integer numbers:
xi = 10
xi =
10
7
14032401-4 Numerical Methods for Computing
Real numbers:
xr = 10.01
xr =
10.0100
List of basic arithmetic operations in MATLAB includes 6 operations
Operation Symbol
addition +
subtraction -
Multiplication *
Division / or \
exponentiation ^
MATLAB has two division operators: the right division(/) and the left division(\). you have
to note that they do not produce the same results.
Example:
x = 6/2
x=
3
Whereas,
x = 6\2
x=
0.3333
< less than, > greater than, >= greater than or equal to, <= less than or equal to, == equal, ~=
not equal, & and, | or, ~ not
Operator Description
< Less than
Less than or equal
<=
to
> Greater
>= Greater or equal to
== Equal to
~= Not equal to
8
14032401-4 Numerical Methods for Computing
▪ Variables
MATLAB has built-in variables and here below a list of some variables:
Variable Meaning
pi Π
exp(x) ex
eps Epsilon
Keeps track of the last output which was not
ans assigned to another variable.
i, j Imaginary unit
Inf Stands for (+∞)
-Inf Stands for (-∞)
NaN Stands for not a number
9
14032401-4 Numerical Methods for Computing
Entering Data:
Data can be also be entered by typing directly into the Command Window, through the editor
in an m-file, or through the Workspace Window by entering values in a spreadsheet format.
To store a given data set in the Workspace, the data must be preceded by a variable name.
The variable name can be any combination of letters and numbers, but it must begin with a
letter and they are case sensitive. Variable names are case sensitive. Also, there is a library of
reserved words that cannot be used as variable names. These will appear as blue when typing
them in .m files.
Variable types:
For example,
After a variable is entered, it will be displayed in the workspace. If the variable is double
clicked on in the workspace, the value of the variable will be displayed in a spreadsheet in a
new window. The value of the variable can also be printed in the command window by
typing the name of the variable and hitting enter.
Note: A semicolon placed at the end of a statement will suppress output to the window. It’s
sometimes useful to take a look at your data, but if you have a lot of data printing out to the
screen while your program is running, you may slow things down.
10
14032401-4 Numerical Methods for Computing
Experiment No. 2
Introduction to Functions and Plots
Objective:
The purpose of this tutorial is to acquaint the students with basics of MATLAB functions and
two-dimensional plotting. The topics discussed in this lab includes The m – files, inline
functions, Vectors and Matrices.
Introduction:
From previous lab, MATLAB can be run interactively from the command line, you can write
a MATLAB program by composing a text file that contains the commands you want
MATLAB to perform in the order in which they appear in the file. The standard file suffix for
a text file containing a MATLAB program is .m. In the MATLAB command window,
selecting the pull-down menu File - > New -> M-file opens the integrated MATLAB text
editor for writing a m-file. This utility is very similar to word processors, so the use of
writing and saving m-files is not explained in detail here. The file is saved in the work
directory of Matlab by default but can be saved in an alternate directory. You can move to
another directory by clicking the [...] button and browsing to it.
The m – files:
Files that contain a computer code are called the m-files. There are two kinds of m-
files:
✓ Script files: Files that do not take the input arguments or return the output
arguments.
✓ Function files: Files that may take input arguments or return output arguments.
statement(s)
end
11
14032401-4 Numerical Methods for Computing
Example 1:
o Create an m – file and write the following code:
function x = square(a)
x = a ^ 2;
end
o Save the m – file with the name square.m
o In the command window type the following line:
X = square(4)
o In the command window type the following line:
X = feval(‘square’,4)
Note:
Using the semicolon at the end of any line of your code will cause the result not to be
displayed on the screen.
Example 2:
% A comment line starts with a percent-sign.
Save the file as “tutorial.m”, make note what directory the file was saved in.
12
14032401-4 Numerical Methods for Computing
Running m-files You can run Matlab scripts in several ways. We will run the tutorial.m script
from the command window and from the script itself.
The name of the function must match the name of the .m file containing the function. (If not,
then Matlab ignores the internal name, which just confuses everything.) As a good programming
practice, the function name and the .m filename should be the same. Why? Suppose you want to
call that function from another .m file or the Matlab command line, it is most logical to call it
with the function name. But if you use another filename as the function name, Matlab will not
be able to find the function.
The basic function used to create 2-D graphs is the plot function which has a variable number
of input arguments.The following is the general syntax for plotting:
In general, it is possible to plot multiple sets of data on a given plot by either entering an
additional set of independent and dependent variables following the first set in the
parentheses as follows:
Multiple data sets can also be plotted in the same figure by typing the command hold on.
>> plot(x,y1);
>> hold on
>> plot(x,y2);
If you have several plots active at a given point in time, the figure may need to be stipulated
before using the plot function with the following syntax:
figure(figure_number)
13
14032401-4 Numerical Methods for Computing
The third potential entry in the plot function is the option to format the way in which the data
is plotted. This is normally just a combination of a letter and a symbol. The letter will
designate the color of the line or symbol used in plotting the data. The symbol will determine
if a line is plotted or if each individual point is plotted with a symbol. The syntax for color
and symbol is shown on the attached cheat sheet or can be found in the plot help listing.
Example:
To plot f(x) = cos(x), and g(x) = sin(x), xЄ[0,2π]:
>>X = 0:0.1:2*pi;
>>Y = cos(X);
>>Z = sin(X);
>>plot(X,Y,X,Z,’O’)
14
14032401-4 Numerical Methods for Computing
Experiment No. 3
Introduction to Vectors, Matrices and Conditions
Objective:
The purpose of this tutorial is to acquaint the students with basics of MATLAB based matrix
operations. The topics discussed in this lab includes Vectors, Matrices and Conditions.
Introduction:
Matlab was designed to deal mainly with numerical data in matrix or vector form. Due to this
design criteria, the some of the standard mathematical operations function as matrix
operations and not as you might expect.
Vectors and Matrices
In this section will see how to create vectors and matrices and the operations we can perform
on them. Before we can examine how to operate on vectors or matrices, we must know how
to define them in Matlab. A vector is either a column or a row of numbers, it is defined as
follows:
x = [1 2 3]
x=
1 2 3
There is another way to define a vector with condition that there is a common step
between the successive elements. The general syntax of general operation is:
X = start-element:step:final-element
Example:
X = 0:0.5:2
X=
0 0.5000 1.0000 1.5000 2.0000
A matrix:
M = [1 2 3; 4 5 6; 7 8 9;];
The above example creates a three by three matrix. The only key to defining a matrix is
to ensure that each column or row has the same number of elements.
A row vector:
row = [1 2 3 4];
A column vector:
column = [1;2;3;4];
15
14032401-4 Numerical Methods for Computing
To define a 2D matrices you may use one of the methods illustrated below:
To define a 2 x 3 matrix, say the name of the matrix is (A).
A = [1 2 3;4 5 6]
A=
1 2 3
4 5 6
A = [1 2 3
4 5 6]
A=
1 2 3
4 5 6
Note: You have to enclose the matrix by the square bracket [].
There are Elementary Matrices that can be generated using built – in functions, here
below some examples:
The components of matrices can be manipulated in several ways, here are below some
examples:
>>A = [1 2 3;4 5 6]
A =
1 2 3
4 5 6
>>A(2,3)
ans =
6
Note: The colon operator: stands for all columns or all rows.
16
14032401-4 Numerical Methods for Computing
Operations on matrices:
Operation Symbol
Addition +
subtraction -
multiplication *
Transpose ’
Power ^
Elementwise multiplication .*
Elementwise Division ./
Elementwise power .^
Notes:
o For the addition and subtraction, the matrices must have the same size. Those two
operation operates elementwise.
o If you want to multiply 2 matrices A and B (A * B), and A has size of n x m then B
must be with the size m x p.
o The transpose takes each row of a matrix and make it as a column.
o The power operation can be applied on square matrices only.
o The elementwise multiplication, division, and power operates on each element not
on the matrix as one unit.
Matrix Manipulation:
Being able to easily manipulate the values, columns, and rows of a matrix will make your life
much easier. The following are examples of the syntax used with matrices:
A=[1 2 3];
B=[4 5 6];
17
14032401-4 Numerical Methods for Computing
Examples:
o Create an m – file, name it matrices.
o Write the following code in the matrices.m file.
% This program perform some operations on matrices
x = input(‘Enter 2x2 matrix: ’);
y = input(‘Enter 2x2 matrix: ’);
disp(‘x + y = ’);
x+y
disp(‘x - y = ’);
x-y
disp(‘x * y = ’);
x*y
disp(‘x .* y = ’);
x .* y
disp(‘x ^ 2 = ’);
x^2
disp(‘x .^ 2 = ’);
x .^ 2
disp(‘transpose of x = ’);
x’
o Run the above program.
So, [1 2 3].^3 gives [1 8 27] but [1 2 3]^3 gives an error. Forgetting the dot is one of the most
common bugs!
Practice Problem: Enter the following data into the Matlab command window.
A=[0 2; 1 4];
B=[1 3; 2 6];
What is A*B? What is A.*B?
What does A.^-1 produce verses A^-1?
18
14032401-4 Numerical Methods for Computing
for i=1:n
<program>
end
if (condition is true)
statement(s)
end
OR
if (condition1 is true)
statement(s)
statement(s)
else
statement(s)
end
Example: The FOR loop performs a series of operations a known number of times. The
example code below illustrate the syntax:
X = [2 3 1 4];
even = 0;
For k=1:length(X)
if (x(k)%2 == 0)
even = even + 1;
end
end
19
14032401-4 Numerical Methods for Computing
Assignment vs. Equals is important in loops. An assignment is a = b and means assign value
b to variable a. The equals operator is a == b results in 1 or yes if they are equal, 0 or no if
they are not.
The final type of loop is a WHILE loop. It requires that a series of operations occur while the
condition applied to a specified variable is correct.
The example syntax is:
i=10;
while i>5
i=i-1
end
Practice Problem: Given the matrix: M=[1 1; 1 3;]; Use loops to determine how many
of the entries are greater than 1 and the location of all qualifying entries (the
corresponding row and column).
20
14032401-4 Numerical Methods for Computing
Experiment No. 4
Taylor Series
Objective:
Truncation errors are those that result from using an approximation in place of an
exact mathematical procedure. In order to gain insight into the properties of such
errors, we now turn to a mathematical formulation that is used widely in numerical
methods to express functions in an approximate fashion—the Taylor series.
Introduction:
Theorem 1. (Taylor polynomial with integral remainder) Suppose a function f (x)
and its first n + 1 derivatives are continuous in a closed interval [c, d] containing
the point x = a. Then for any value x on this interval
It’s clear from the fact that n! grows rapidly as n increases that for sufficiently
differentiable functions f (x). Taylor polynomials become more accurate as n
increases.
Methodology:
We can use Matlab to create a function or script to calculate the series for a given
number of terms n.
%%Matlab Code
clear
prompt = 'Enter angle value between 0 to 2pi? ';
x = input(prompt)
21
14032401-4 Numerical Methods for Computing
N = input(prompt)
ty = 0;
for i = 1:N
ty = ty +((-1)^(i))*((x^(2*i+1))/(factorial(2*i+1)));%your calculations
end
fprintf(['actual value ' num2str(ty)]) %display the results
Example 2. Find Taylor series expansion of 𝑒^𝑥 about 𝑥=1 using Matlab.
>> syms x
>> A = taylor(exp(x), x,1)
Matlab Output
A = exp(1) + exp(1)*(x - 1) + (exp(1)*(x - 1)^2)/2 + (exp(1)*(x - 1)^3)/6 +
(exp(1)*(x - 1)^4)/24 + (exp(1)*(x - 1)^5)/120
Matlab Output
B = 1281/1280
Or, to get the answer in decimal, use double
>> B = double(subs(A,1.5))
Matlab Output
B = 4.4820
>>syms x
>>taylor(exp(x))
Matlab Output
x^5/120 + x^4/24 + x^3/6 + x^2/2 + x + 1
Matlab Output
x^7/5040 + x^6/720 + x^5/120 + x^4/24 + x^3/6 + x^2/2 + x + 1
22
14032401-4 Numerical Methods for Computing
Practice Problem: Find Taylor series using MATLAB for the following functions:
>>syms x
>>A1 = taylor(sin(x))
>>A2 = taylor(sin(x),'Order',8)
>>A3 = taylor(sin(x),'Order',10)
>>fplot([A1 A2 A3])
6th Order
1 8th Order
10th Order
0.5
-0.5
-1
-3 -2 -1 0 1 2 3
23
14032401-4 Numerical Methods for Computing
Experiment No. 5
Bisection Method and Locating Roots
Objective:
The first class of numerical methods is known as bracketing methods. They work by
choosing two values of x, in the above case, that bracket the root. Then different methods are
used to zero in on the actual root. Usually a tolerance is specified, so that the exact root is not
found. The value that is closer to the root than the tolerance is kept. To gain insight into the
working of these methods, we will learn how to implement bisection method using
MATLAB.
Introduction:
Root finding is a skill that is particularly well suited for computer programming. Unless the
roots of an equation are easy to find, iterative methods that can evaluate a function hundred,
thousands, or millions of times will be required. Another was to say, “root finding” is to say
“what value of x for a function will give an answer of zero”. Also, root finding can be
thought of as finding where two functions intersect.
Roots Command: It is used to find roots for a polynomial function
f(x) = x3 + 2x2 + 10x - 20
This is the problem Leonardo Fibonacci solved in 1225, x~1.36880810785
>> a=[1 2 10 ‐20];
a=
1 2 10 ‐20
>> format long
>> roots(a)
ans = ‐1.684404053910685 + 3.431331350197691i
‐1.684404053910685 ‐ 3.431331350197691i
-1.368808107821373
Bisection Method:
Bracketing methods are based on two initial guesses that “bracket” the root—that is, are on
either side of the root. The bisection method is a variation of the incremental search method
in which the interval is always divided in half. If a function changes sign over an interval, the
function value at the midpoint is evaluated. The location of the root is then determined as
lying within the subinterval where the sign change occurs. The subinterval then becomes the
interval for the next iteration. The process is repeated until the root is known to the required
precision.
24
14032401-4 Numerical Methods for Computing
Algorithm:
Loop
1. Compute the mid point c=(a+b)/2
2. Evaluate f(c)
i. If f(a)f(c) < 0 then new interval [a, c]
ii. If f(a)f(c) > 0 then new interval [c, b]
iii. If f(a)f(c) = 0 then Stop and the root is c
End loop
Matlab Code:
25
14032401-4 Numerical Methods for Computing
Experiment No. 6
Open Methods – Newton Raphson and Secant
Objective:
These methods are related and often confused. We shall derive them in tandem, since they
only differ in the last stage. The central premise for both methods is that the function is
locally linear and the next iteration for the required value can be attained via linear
extrapolation (or interpolation).
Introduction:
We will start using a Taylor series to derive the Newton–Raphson technique. We assume the
current guess is x and this is incorrect by an amount h, so that x + h is the required value. It
now remains for us to determine h or at least find an approximation to it. The Taylor
expansion for the function f(x) at the point x + h is given by
f(x + h) = f(x) + hf′(x) + O(h2).
This can be interpreted as: the value of the function at x + h is equal to the value of the
function at x plus the gradient times the distance between the points. This can be considered
to include further terms; now we are fitting a straight line. In this expression we have used the
term O(h2): loosely this means something the same size as h2 and the prime means
differentiated with respect to the argument of the function. We now note that x + h is
supposedly the actual root so f(x+h) = 0, and discarding the higher-order terms we find that
h ≈ − f(x)/f′(x).
This presumes we are close to the actual root, and consequently we can discard the terms
proportional to h2 since these should be smaller than those proportional to h. This allows us to
construct the iterative scheme
xn+1 = xn − f(xn)/f′(xn) , n = 0, 1, 2, ··· .
This method can also be derived using geometric arguments. In these derivations the function
is taken to be approximated by a straight line to determine the next point.
Figure 5. Newton-Raphson
26
14032401-4 Numerical Methods for Computing
The value of h is determined by using the fact that the ratio of the two sides h and f(xn) must
be equal to f ′(xn).
Now we shall presume that we have two routines func.m and func prime.m which give us the
function and its derivative. For ease let us consider the function
% func.m
function [f] = func(x)
f = x-2*sin(x.ˆ2);
% func_prime.m
function [value] = func_prime(x)
value=1- 4*x.*cos(x.ˆ2);
Notice that we have used the dot operators, even though this routine is only ever likely to be
called in this context using a scalar. This permits the routine to be used from other codes in a
portable fashion. This can be coded simply using:
x = 1;
for j = 1:10
x=x- func(x)/func_prime(x);
end
where we have set the initial guess to be x = 1 and supposed that the method will converge in
ten iterations. We now give a more robust code to perform the iterations
%Newton_Raphson.m
x = input(’Starting guess :’);
tolerance = 1e-8;
iterations = 0;
while (iterationstolerance)
x = x-func(x)/func_prime(x);
iterations = iterations + 1;
end
if iterations==30
disp(’No root found’)
else
disp([’ Root = ’ num2str(x,10) ’ found in ’ ... int2str(iterations) ’ iterations.’])
end
Hopefully you can see the difference between the two codes and see that ultimately the
second version is more useful. By entering the values 0.1, 0.6 and 1.5 we can obtain the three
roots we are concerned with. Notice we have increased the number of digits printed in the
answer to 10 using the form of the MATLAB command num2str which accepts two
arguments.
27
14032401-4 Numerical Methods for Computing
%Secant.m
x0=input(’Starting guess point 1 :’);
x1=input(’Starting guess point 2 :’);
tolerance=1e-8;
iterations=0;
while(iterations<30)& (abs(func(x1))>tolerance)
iterations = iterations + 1 ;
f0 = func(x0);
f1 = func(x1);
x2 = x0-f0*(x1-x0)/(f1-f0);
x0 = x1;
x1 = x2;
end
if iterations==30
disp(’No root found’)
else
disp([’ Root = ’ num2str(x1,10) ’ found in ’ ... num2str(iterations) ’
iterations.’])
This method works far better if the two initial points are on opposite sides of the root. In the
above method we have merely chosen to proceed to use x1 and the newly attained point x2:
however, we could equally have chosen x0 and x2. In order to determine which, we should use
we require that the function changes sign between the two ends of the interval. This is done
by changing the lines where the next interval is chosen.
Practice Problem: Using the Newton–Raphson routine determine the zero of the
function
• f(x)=ex − e−2x + 1.
Instructor Solution:
28
14032401-4 Numerical Methods for Computing
Experiment No. 7a
Elimination Methods
Objective:
In this lab, students will get acquainted with Gauss elimination, Reduced row echelon form,
Gauss-Jordon elimination and LU decomposition methods.
Introduction:
Gaussian Elimination:
The Gauss Elimination method is a method for solving a matrix equation Ax=b for x. The
process is:
1. Start by augmenting the matrix A with the column vector b.
2. Perform elementary row operations to transform this augmented matrix to upper
triangular form.
3. Use back-substitution to solve for the unknowns in x.
For example, for a 2 x 2 system, the first step is to augment the matrix [A b]:
Then, EROs are applied to get the augmented matrix into an upper triangular form (which,
for a 2 x 2 matrix means finding an ERO that would change a21 to 0):
Here, the primes indicate that the values may have been changed. Putting this back into the
equation form, we have
29
14032401-4 Numerical Methods for Computing
%Backward Substitution
x(n)=b(n)/a(n,n); %solve for the last x value
for i=n-1:-1:1
sum = 0;
for j=i+1:n
sum = sum + a(i,j)*x(j);
end
x(i)=(b(i)-sum)/a(i,i);
end
Gaussian Elimination:
a = [3 4 -2 2 2
4 9 -3 5 8
-2 -3 7 6 10
1 4 6 7 2];
30
14032401-4 Numerical Methods for Computing
c=c+a(s,k)*x(k);
end
x(s)=(a(s,n)-c)/a(s,s);
end
for j=(i+1):m
A(j,:) = A(j,:)+(-1)*A(j,i)*A(i,:);
end
end
%Upwards
for i=m:-1:1
for j=(i-1):-1:1
A(j,:) = A(j,:) + (-1)*A(j,i)*A(i,:);
end
end
%Finding the values
x = []; % Solution vector
for i=1:m
x(i) = A(i,n);
end
Practice Problem: Solve the linear system using Gauss Elimination method.
x1 − x2 + 2x3 − x4 = −8
2x1 − 2x2 + 3x3 − 3x4 = −20
x1 + x2 + x3 = −2
x1 − x2 + 4x3 + 3x4 = 4
31
14032401-4 Numerical Methods for Computing
Experiment No. 7b
Elimination Methods
we see that MATLAB saves us time compared to all the work we needed to do on the
previous page. Here is another, this one has free variables at the end. We will see that after
rref because there are non-pivot columns corresponding to variables.
>> A=[1 3 4 2; 1 -3 0 1]
and then reduce it
>> rref(A)
we get the reduced form. It’s in decimals but clearly it corresponds to the augmented matrix
Note that the third column is not a pivot column so x3 is free. Hence, we have the solution.
Lastly, note that we can deal with matrices with unknown constants in them but we have to
explicitly tell MATLAB that they are unknown constants. For example, suppose we wish to
enter the matrix
First we must tell MATLAB that h and k are to be dealt with symbolically. We do this with
the syms command
>> syms h Practice Problem: Consider the system of
>> syms k equations
And now we can do
>> A=[-2 3 h ; 5 -1 k] x1 + 2x2 − 3x3 = −3
>> rref(A) −4x1 − 5x2 + 2x3 = −2
2x1 + 3x2 − x3 = 2
32
14032401-4 Numerical Methods for Computing
LU Decomposition:
Suppose we have the system of equations
AX = B.
The motivation for an LU decomposition is based on the observation that systems of
equations involving triangular coefficient matrices are easier to deal with. Indeed, the whole
point of Gaussian Elimination is to replace the coefficient matrix with one that is triangular.
The LU decomposition is another approach designed to exploit triangular systems. We
suppose that we can write
A = LU
where L is a lower triangular matrix and U is an upper triangular matrix. Our aim is to find L
and U and once we have done so we have found an LU decomposition of A.
The name of the built-in function for a Lower-Upper decomposition is 'lu'. To get the
LU factorization of a square matrix A, type the command
>>[L, U] = lu(A)
Matlab returns a lower triangular matrix L and an upper triangular matrix U such that L*U = A.
A= [ 1 2 -3
-3 -4 13
2 1 -5]
L=[1 0 0
-3 1 0
2 -1.5 1]
and matrix
U = [1 2 -3
0 2 4
0 0 7]
The decomposition of the matrix A is an illustration of an important and well known theorem.
If A is a nonsingular matrix that can be transformed into an upper diagonal form U by the
application or row addition operations, then there exists a lower triangular matrix L such
that A = LU.
U = En En-1 ... E2 E1 A
33
14032401-4 Numerical Methods for Computing
L will have ones on the diagonal. The off-diagonal elements are zeros above the diagonal,
while the elements below the diagonal are the multipliers required to perform Gaussian
elimination on the matrix A. The element lij is equal to the multiplier used to eliminate the
(i, j) position.
[L,U]=lu(sparse(A),0) no pivoting
Example:
In Matlab, let's find the LU decomposition of the matrix
>>[L, U] = lu(A)
L=
-0.2500 -0.5385 1.0000
0.7500 1.0000 0
1.0000 0 0
U=
8.0000 3.0000 -7.0000
0 -3.2500 13.2500
0 0 2.3846
>>L*U
ans =
-2.0000 1.0000 -3.0000
6.0000 -1.0000 8.0000
8.0000 3.0000 -7.0000
34
14032401-4 Numerical Methods for Computing
Experiment No. 8
Least Squares Regression and Newton Interpolation
Objective:
In this lab, students will get acquainted with least squares linear regression and Newton
interpolation techniques.
Introduction:
First, we will come up with a Matlab function to compute slope and intercept of the best fit
straight line along with correlation coefficient and standard error of the estimate. For given
inputs vectors x and y, the output slope and intercept along with the best fit plot are provided.
35
14032401-4 Numerical Methods for Computing
36
14032401-4 Numerical Methods for Computing
>> p = polyfit(x, y, n)
where x and y are the vectors of the independent and the dependent variables, respectively,
and n = the order of the polynomial. The function returns a vector p containing the
polynomial’s coefficients. We should note that it represents the polynomial using decreasing
powers of x as in the following representation:
Because a straight line is a first-order polynomial, polyfit(x,y,1) will return the slope and the
intercept of the best-fit straight line.
Another function, polyval, can then be used to compute a value using the coefficients. It has
the general format:
>> y = polyval(p, x)
where p = the polynomial coefficients, and y = the best-fit value at x. For example,
>> y = polyval(a,45)
y=
641.8750
Polynomial Interpolation:
For the case where the number of data points equals the number of coefficients, polyfit
performs interpolation. That is, it returns the coefficients of the polynomial that pass
directly through the data points. For example, it can be used to determine the coefficients
of the parabola that passes through the last three density values:
37
14032401-4 Numerical Methods for Computing
>> d = polyval(p,350)
d=
0.56762500000000
Newton Interpolation:
It is straightforward to develop an M-file to implement Newton interpolation. As the first step
is to compute the finite divided differences and store them in an array.
The differences are then used in conjunction with Newton interpolation equation to perform
the interpolation. An example of a session using the function would be:
>> y = log(x);
>> Newtint(x,y,2)
ans =
0.62876857890841
The algorithm for Newtint() function used in the above example is given as:
38
14032401-4 Numerical Methods for Computing
39
14032401-4 Numerical Methods for Computing
Experiment No. 9
Integration and Differentiation
Objective:
The primary objective of this laboratory session is to introduce the students with numerical
integration and differentiation and their implementation.
Introduction:
Mathematically, integration is represented by
b
I = f ( x)dx (9.1)
a
which stands for the integral of the function f(x) with respect to be independent variable x,
evaluated between the limits x = a to x = b.
For function lying above the x axis, the integral expressed by Eq(9.1) corresponds to the area
under the curve of f(x) between x = a and b.
In this lab, we will only use two common approaches to solve the numerical integration
problem trapezoidal rule and simpson’s 1/3.
integral = trapz(x,y)
where the two vectors x and y, hold the independent and dependent variables, respectively.
Example 9.1
Integrate the function
f (x ) = 0.2 + 25x − 200x 2 + 675x3 − 900x 4 + 400x5
from a = 0 to b = 0.8.
40
14032401-4 Numerical Methods for Computing
Solution
First, use the MATLAB function quad to integrate the function. To use quad, we must first
develop an M-file to hold the function. Using a text editor, we can create the following file
function y=fx(x)
y=0.2+25*x-200*x.^2+675*x.^3-900*x.^4+400*x.^5;
It can then be stored on the MATLAB directory as fx.m. To see how it work, type
where the second two entries are the integration limits. The result is
integral=
1.6405
Example 9.2
Repeat the Example 9.1.
Solution
First, generate the values of independent variable,
>> x=0:0.1:0.8
integral=
1.6008
Trapezoidal Rule:
The trapezoidal rule is a method for finding an approximate value for a definite integral.
Suppose we have the definite integral
b
a
f ( x)dx
b−a
First the area under the curve y = f (x) is divided into n strips, each of equal width h = .
n
41
14032401-4 Numerical Methods for Computing
Denoting the areas of the trapezoidal as I1, I2, ….,In, we have (Fig.6.1)
f ( x0 ) + f ( x1 ) f ( x1 ) + f ( x 2 )
I1 = h, I 2 = h,...
2 2
f ( xi −1 ) + f ( xi ) f ( x n −1 ) + f ( x n )
Ii = h,..., and I n = h.
2 2
n
h
I = f ( x)dx I i = ( f ( x0 ) + 2 f ( x1 ) + 2 f ( x 2 ) + ... + 2 f ( x n −1 ) + f ( x n )).
b
a
i =1 2
42
14032401-4 Numerical Methods for Computing
Start
Set
a, b, M, and s
h = (b-a)/M
Yes k=1
f(a), f(b) k > M-1
k=k+1
No
trap=h*((f,a)+(f,b))/2+h*s
x=a+(h*k)
f(x)
STOP
s = s+f(x)
Example 9.3
4 x
Determine the value of the integral I = dx using trapezoidal rule with different step
1
x +1
lengths.
h=(b-a)/M;
s=0;
for k=1:(M-1)
x=a+(h*k)
43
14032401-4 Numerical Methods for Computing
s=s+feval(f,x)
end
s=h*(feval(f,a)+feval(f,b))/2+h*s
Simpson’s Rule:
Simpson’s 1/3 Rule is another technique used for numerical integration. When we used the
single interval trapezoidal rule to estimate the integral of f(x) over the range of a to b, we
drew a straight line from the point (a,f(a)) to (b,f(b)). A more accurate approach might to
increase the level of the polynomial approximating the curve from linear (a polynomial of
order one, as used in the trapezoidal rule) to quadratic (a polynomial of order two, as used in
Simpson’s 1/3 rule).
h n −1 n−2
I 0
3
f + 4 f i + 2 fi + fn (9.3)
i =1, 3, 5,... i = 2, 4, 6
The term “1/3” in Simpson’s one-third rule refers to the presence of the factor “1/3” in
Eq.(9.3). For a multistage application of Simpson’s one-third rule, we need to divide the
b−a
range a x b into n segments of equal width h = . The number of segments must be
n
an even number so that Eq.(9.3) can be applied of two segments.
44
14032401-4 Numerical Methods for Computing
Start
Set
a, b, M, s1, and s2
h = (b-a)/M
Yes k=1
k > M-(M/2)
k = k+ M-(M/2)
No
x=a+h(2*k-1)
f(x)
s1 = s1+f(x)
Yes
k=1
f(a), f(b) k > M-(M/2+1)
k = k+ M-(M/2+1)
simp=h*((f,a)+(f,b)+(4*s1)+(2*s2))/3
x=a+h(2*k)
f(x)
STOP
s2 = s2+f(x)
45
14032401-4 Numerical Methods for Computing
Example 9.4
4 x
Determine the value of the integral I = dx using Simpson’s 1/3 rule with step sizes
1
x +1
= 1.
h=(b-a)/M
s1=0
s2=0
for k=1:(M-(M/2))
x=a+h*(2*k-1)
s1=s1+feval(f,x)
end
for k=1:(M-((M/2)+1))
x=a+h*2*k
s2=s2+feval(f,x)
end
s=h*(feval(f,a)+feval(f,b)+(4*s1)+(2*s2))/3
Numerical Differentiation:
46
14032401-4 Numerical Methods for Computing
47
14032401-4 Numerical Methods for Computing
Anonymous Functions
h = @(arglist)anonymous_function
48