Matlab File Uma011
Matlab File Uma011
UMA011
MATLAB Practicals (ODD Semester2021-2022)
B.E. Second Year
Thapar Institute of Engineering and Technology, Patiala
Name:………………………….
Roll No.:……………………….
Group:…………………………
Instructor:………………………
Contents
S.No. Experiment Date Signature
1. (a) Use intermediate value theorem to find the interval of the roots.
(b) Find the root of non-linear equation f(x) = 0 using bisection method.
2. Find the root of non-linear equation f(x) = 0 using Newton’s and
secant methods
3. Find the root of non-linear equation f(x) = 0 using fixed-point
iteration method
4. (a) Solve system of linear equations Ax = b using Gauss elimination
method.
(b) Further use it to apply LU factorization method for solving system of
linear equations
5. Solve system of linear equations Ax = b using Gauss-Seidel and SOR
iterative methods.
6. (a) Find a dominant eigen-value and associated eigen-vector by Power
method.
(b) Implement Lagrange interpolating polynomials of degree ≤ n on n+1
discrete data points.
7. Implement Newton’s divided difference interpolating polynomials
for n+1 discrete data points.
8. Fit a curve for given data points by using principle of least squares.
9. Integrate a function numerically using composite trapezoidal and
Simpson’s rules.
10. Find the solution of initial value problem using Euler and Runge-
Kutta (fourth-order) methods.
Experiment 1: Bisection Method
1. Algorithm of Intermediate Value Theorem (IVT): To determine all the subintervals [a, b]
of [-N, N] that containing the roots of f(x) = 0.
Input: function f(x), and the values of h, N
for i = -N : h : N
if f(i) * f(i + h) < 0 then a = i and b = i + h
end if
end i
3. Students are required to write both the programs (IVT and Bisection) and implement it on the
following examples.
(i) Use bisection method in computing of 29 with ϵ = 0.001, N = 10, h = 1.
(ii) Determine the number of iterations necessary to solve f(x) = x3 + 4x2 – 10 = 0 with
accuracy 10-3 using a = 1 and b = 2 and hence find the root with desired accuracy.
4. Thermistors are temperature-measuring devices based on the principle that the thermistor
material exhibits a change in electrical resistance with a change in temperature.
By measuring the resistance of the thermistor material, one can then determine the
temperature. For a 10K3A Betatherm thermistor, the relationship between the resistance R of
the thermistor and the temperature is given by
1
= 1.129241 × 10−3 + 2.341077 × 10−4 ln 𝑅 + 8.775468 × 10−8 ln(𝑅) 3
𝑇
where T is in Kelvin and R is in ohms. Use the bisection method to find the resistance R at
0
18.99 C.
Experiment 2: Newton’s and Secant Methods
1. Algorithm for Newton’s method: Find a solution to f(x) = 0, given an initial approximation x0.
Input: Initial approximation x0, tolerance value ϵ, maximum number of iterations N.
Output: Approximate solution or message of failure.
Step 1: Set i = 1.
Step 2: While i ≤ N do Steps 3 to 6.
𝑓(𝑥 )
Step 3: Set 𝑥1 = 𝑥0 − 𝑑𝑓 (𝑥0 ). (Compute xi).
0
𝑥 1 −𝑥 0
Step 4: If 𝑥1 − 𝑥0 ≤ 𝜖 or 𝑥1
≤ 𝜖 then OUTPUT x1; (The procedure is successful)
STOP.
Step 5: Set i = i + 1.
Step 6: Set x0 = x1. (Update x0)
Step 7: Print (‘The method failed after N iterations, N =’, N); (The procedure is unsuccessful)
STOP
2. Algorithm for Secant method: Find a solution to f(x) = 0, given an initial approximations x0
and x1.
Input: Initial approximation x0 and x1, tolerance value ϵ, maximum number of iterations N.
Output: Approximate solution or message of failure.
Step 1: Set i = 1.
Step 2: While i ≤ N do Steps 3 to 6.
𝑥 1 −𝑥 0
Step 3: Set 𝑥2 = 𝑥1 − 𝑓 𝑥 1 −𝑓(𝑥 0 )
𝑓(𝑥1 ). (Compute xi).
𝑥 −𝑥
Step 4: If 𝑥2 − 𝑥1 ≤ 𝜖 or 2𝑥 1 ≤ 𝜖 then OUTPUT x2; (The procedure is successful)
2
STOP.
Step 5: Set i = i + 1.
Step 6: Set x0 = x1 and x1 = x2. (Update x0 and x1)
Step 7: Print (‘The method failed after N iterations, N =’, N); (The procedure is unsuccessful)
STOP
3. Students are required to write both the program and implement it on the following examples.
Take tolerance value ϵ = 0.00001
(i) Compute 17.
(ii) The root of exp(– x)(x2 + 5x + 2) + 1 = 0. Take initial guess –1.0.
(iii) Find a non-zero solution of x = 2sin x. (Apply IVT to find an initial guess)
1. Algorithm for Fixed-point iteration method: To find a solution to x = g(x), given an initial
approximation x0.
Input: Initial approximation x0, tolerance value ϵ, maximum number of iterations N.
Output: Approximate solution or message of failure.
Step 1: Set i = 1.
Step 2: While i ≤ N do Steps 3 to 6.
Step 3: Set 𝑥1 = 𝑔(𝑥0 ). (Compute xi).
𝑥 1 −𝑥 0
Step 4: If 𝑥1 − 𝑥0 ≤ 𝜖 or 𝑥1
≤ 𝜖 then OUTPUT x1; (The procedure is successful)
STOP.
Step 5: Set i = i + 1.
Step 6: Set x0 = x1. (Update x0)
Step 7: Print the output and STOP.
2. The equation f(x) = x3 + 4x2 – 10 = 0 has a unique root in [1,2]. There are many ways to
change the equation to the fixed-point form x = g(x) using simple algebraic manipulation. Let
g1, g2, g3, g4 and g5 are iteration functions obtained by the given function, then check which of
the following iteration functions will converge to the fixed point? (Tolerance ϵ = 10-3)
(a) 𝑔1 𝑥 = 𝑥 − 𝑥 3 − 4𝑥 2 + 10
10
(b) 𝑔2 𝑥 = − 4𝑥
𝑥
(c) 𝑔3 𝑥 = 0.5 10 − 𝑥 3
10
(d) 𝑔4 𝑥 = 4+𝑥
𝑥 3 +4𝑥 2 −10
(e) 𝑔5 𝑥 = 𝑥 − 3𝑥 2 +8𝑥
3. Find the smallest and second smallest positive roots of the equation tan(x) = 4x, with an
accuracy of 10-3 using fixed-point iterations.
4. Use a fixed-point iteration method to determine a solution accurate to within 10-2 for
2sin𝜋𝑥 + 𝑥 = 0 on [1,2]. Use initial guess x0 = 1.
Experiment 4: Gauss Elimination and LU Factorization Methods
1. Algorithm for Gauss elimination method: Find a solution of system of linear equations.
Input: Number of unknowns and equations n,
Augmented matrix 𝐴 = 𝑎𝑖𝑗 , where 1 ≤ 𝑖 ≤ 𝑛, and 1 ≤ 𝑗 ≤ 𝑛 + 1.
Output: Solution (𝑥1 , 𝑥2 , ⋯ , 𝑥𝑛 ) or message that the linear system has no unique solution.
Step 1: For 𝑖 = 1,2, ⋯ , 𝑛 − 1 do Steps 2 – 4. (Elimination process)
Step 2: Let p be the smallest integer with 𝑖 ≤ 𝑝 ≤ 𝑛 and 𝑎𝑝𝑖 ≠ 0.
If no integer p can be found then
OUTPUT (‘no unique solution exists’);
STOP.
Step 3: If 𝑝 ≠ 𝑖 then perform 𝐸𝑝 ↔ 𝐸𝑖 .
Step 4: For 𝑗 = 𝑖 + 1, ⋯ , 𝑛 do Steps 5 and 6.
Step 5: Set 𝑚𝑗𝑖 = 𝑎𝑗𝑖 𝑎𝑖𝑖 .
Step 6: Perform 𝐸𝑗 − 𝑚𝑗𝑖 𝐸𝑖 ↔ 𝐸𝑗 ;
Step 7: If 𝑎𝑛𝑛 = 0 then
OUTPUT (‘no unique solution exists’);
STOP.
Step 8: Set 𝑥𝑛 = 𝑎𝑛,𝑛+1 𝑎𝑛𝑛 . (Start backward substitution)
Step 9: For 𝑖 = 𝑛 − 1, 𝑛 − 2, ⋯ ,1 set 𝑥𝑖 = 𝑎𝑖,𝑛+1 − 𝑛𝑗=𝑖+1 𝑎𝑖𝑗 𝑥𝑗 𝑎𝑖𝑖 .
Step 10: OUTPUT (𝑥1 , 𝑥2 , ⋯ , 𝑥𝑛 ). (Procedure completed successfully)
STOP.
3. Use Gauss elimination method to find the solution of the following linear system of
equations:
10x + 8y – 3z + u = 16
2x + 10y + z – 4u = 9
3x – 4y + 10z + u = 10
2x + 2y – 3z + 10u = 11
4. Solve the following linear system of equations:
𝜋𝑥1 + 2𝑥2 − 𝑥3 + 𝑥4 = 0
𝑒𝑥1 − 𝑥2 + 𝑥3 + 2𝑥4 = 1
𝑥1 + 𝑥2 − 3𝑥3 + 𝑥4 = 2
−𝑥1 − 𝑥2 + 𝑥3 − 5𝑥4 = 3
5. Kirchhoff’s laws of electrical circuits state that both the net flow of current through each
junction and the net voltage drop around each closed loop of a circuit are zero. Suppose that a
potential of V volts is applied between the points A and G in the circuit and that i1, i2, i3, i4 and
i5 represent current flow as shown in the diagram. Using G as a reference point, Kirchhoff’s
laws imply that the currents satisfy the following system of linear equations:
5𝑖1 + 5𝑖2 = 𝑉
𝑖3 − 𝑖4 − 𝑖5 = 0
2𝑖4 − 3𝑖5 = 0
𝑖1 − 𝑖2 − 𝑖3 = 0
5𝑖2 − 7𝑖3 − 2𝑖4
=0
1. Algorithm for Gauss Seidel Method: Find a solution of system of linear equations Ax = b.
Input: Number of unknowns n; Coefficient matrix 𝐴 = 𝑎𝑖𝑗 , where 1 ≤ 𝑖 ≤ 𝑛, and 1 ≤ 𝑗 ≤
𝑛; column vector b; Initial solution vector x0; tolerance value tol; maximum number of
iterations N.
Output: Solution (𝑥1 , 𝑥2 , ⋯ , 𝑥𝑛 ).
Step 1: For 𝑘 = 1,2, ⋯ , 𝑁 do Steps 2 – 4.
Step 2: For 𝑖 = 1,2, ⋯ , 𝑛
1 𝑖−1 𝑛
𝑥𝑖 = 𝑏𝑖 − 𝑎𝑖𝑗 𝑥𝑗 − 𝑎𝑖𝑗 𝑥0𝑗
𝑎𝑖𝑖 𝑗 =1 𝑗 =𝑖+1
Step 3: If 𝑥 − 𝑥0 < 𝑡𝑜𝑙 then OUTPUT (𝑥1 , 𝑥2 , ⋯ , 𝑥𝑛 ).
STOP
Step 4: Set x0 = x. (Update x0)
Step 5: Print OUTPUT (𝑥1 , 𝑥2 , ⋯ , 𝑥𝑛 ) (Procedure completed successfully)
STOP.
3. Use Gauss Seidel method to find the solution of the following linear systems with an initial
vector [0,0,0,0] and tolerance value 10-5 in the . ∞ norm:
4. Use Gauss Seidel method to solve the following linear system with an initial vector [0,0,0]
and tolerance value 10-3 in the . ∞ norm:
4.63𝑥1 − 1.21𝑥2 + 3.22 𝑥3 = 2.22
−3.07𝑥1 + 5.48𝑥2 + 2.11𝑥3 = −3.17
1.26𝑥1 + 3.11𝑥2 + 4.57𝑥3 = 5.11
Experiment 6: Power Method and Lagrange Interpolation
2. Determine the largest eigen-value and the corresponding eigen-vector of the following
matrices using the power method. Use x0 = [1,1,1]T and ϵ = 10-3:
4 1 0
(a) 1 20 1 . Use x0 = [1,1,1]T and ϵ = 10-3
0 1 4
1 1 0 0
1 2 0 1
(b) . Use x0 = [1,1,0,1]T and ϵ = 10-3
0 0 3 3
0 1 2 3
x x1 x2 …… xn
f(x) f(x1) f(x2) …… f(xn)
We write the following algorithm by taking n points and thus we will obtain a polynomial of
degree ≤ n – 1.
Input:The degree of the polynomial, the values x(i) and f(i), i = 1, 2,….,n and the point of
interpolation p.
Output: Value of 𝑃𝑛−1 𝑝 .
Algorithm:
Step 1. Calculate the Lagrange’s fundamental polynomials li(x) using the following loop:
for i = 1 to n
l(i) = 1
for j = 1 to n
if j ≠ i
𝑝 − 𝑥𝑗
𝑙 𝑖 = 𝑙 𝑖
𝑥 𝑖 −𝑥 𝑗
end j
end i
Step 2. Calculate the approximate value of the function at x=p using the following loop:
sum = 0
for i = 1 to n
sum = sum + l(i)*f(i)
end i
4. The following data define the sea-level concentration of dissolved oxygen for fresh water as a
function of temperature:
t 0 8 16 24 32 40
O(t) 14.621 11.843 9.870 8.418 7.305 6.413
Use Lagrange’s interpolation formula to approximate the value of O(15) and O(27).
5. Generate eight equally-spaced points from the function f(x) = sin2x from x = 0 to 2π. Use
Lagrange interpolation to approximate f(0.5), f(3.5), f(5.5) and f(6.0).
Experiment 7: Newton’s Divided Difference Interpolation
Input: Enter n the number of data points; enter n distinct numbers 𝑥1 , 𝑥2 , … , 𝑥𝑛 ; enter
corresponding function values 𝑓(𝑥1 ), 𝑓(𝑥2 ), … , 𝑓(𝑥𝑛 ) as 𝐹1,1 , 𝐹1,2 , … , 𝐹1,𝑛 ; enter an
interpolating point p.
Output: the numbers 𝐹2,2 , 𝐹3,3 , … , 𝐹𝑛,𝑛 such that
𝑛 𝑖−1
𝑃𝑛−1 𝑝 = 𝐹𝑖,𝑖 (𝑝 − 𝑥𝑗 )
𝑖=1 𝑗 =1
where Fk, k is the (k-1)th divided difference 𝑓 𝑥1 , 𝑥2 , … , 𝑥𝑘
Step 1: Evaluate 𝑭𝟐,𝟐 , 𝑭𝟑,𝟑 , … , 𝑭𝒏,𝒏
for 𝑖 = 2 to 𝑛
for 𝑗 = 𝑖 to 𝑛
𝐹𝑗 ,𝑖−1 − 𝐹𝑗 −1,𝑖−1
Evaluate 𝐹𝑗 ,𝑖 = 𝑥 𝑗 −𝑥 𝑗 −𝑖+1
.
end j
end i
Step 2: Evaluate 𝒊−𝟏 𝒋=𝟏(𝒑 − 𝒙𝒋 ) for each 𝒊 = 𝟏 𝐭𝐨 𝒏
for 𝑖 = 1 to 𝑛
Set product (i) = 1
for 𝑗 = 1 to 𝑖 − 1
product (i) = product (i) * (𝑝 − 𝑥𝑗 )
end j
end i
Step 3: Evaluate 𝑷𝒏−𝟏 𝒑
Set Sum = 0
for 𝑖 = 1 to 𝑛
Sum = Sum + ( 𝐹𝑖,𝑖 * product (i))
end i
Step 4: OUTPUT Sum ≡ 𝑃𝑛−1 𝑝
STOP
Estimate the value of f(2.25) using the Newton’s divided difference interpolation. Compare
with the exact value.
1. Write an algorithm for least square approximations to fit any curve of the forms:
𝐵
𝑦 = 𝑎 + 𝑏𝑥 , 𝑦 = 𝑎 + 𝑏𝑥 + 𝑐𝑥 2 , 𝑦 = 𝐴 𝑥 + 𝑥 , …
2. Use the method of least squares to fit the linear and quadratic polynomial to the following
data:
x -2 -1 0 1 2
f(x) 15 1 1 3 19
3. By the method of least square fit a curve of the form 𝑦 = 𝑎𝑥 𝑏 to the following data:
x 2 3 4 5
y 27.8 62.1 110 161
𝐵
4. Use the method of least squares to fit a curve 𝑦 = 𝐴 𝑥 + 𝑥 to the following data:
x 0.1 0.2 0.4 0.5 1 2
y 21 11 7 6 5 6
Experiment 9:Numerical Quadrature
3. Approximate the following integrals using the composite trapezoidal and Simpson rule by
taking different subintervals (e.g. 4, 6, 10, 20)
0.25
(a) 𝐼 = −0.25
(𝑐𝑜𝑠𝑥)2 𝑑𝑥
𝑒+1 1
(b) 𝐼 = 𝑒
𝑑𝑥
𝑥𝑙𝑛𝑥
1 −𝑥 2
(c) 𝐼 = −1
𝑒 𝑐𝑜𝑠𝑥 𝑑𝑥
4. The length of the curve represented by a function y = f(x) on an interval [a, b] is given by the
integral
𝑏
𝐼= 1 + 𝑓′ 𝑥 2 𝑑𝑥.
𝑎
Use the trapezoidal rule and Simpson’s rule with 4 and 8 subintervals, compute the length of
the curve
𝑦 = 𝑡𝑎𝑛−1 1 + 𝑥 2 , 0 ≤ 𝑥 ≤ 2.
Experiment 10: Solution of Initial Value Problem
3. Compute solution of the following differential equation by the modified Euler’s method and
Runga-Kutta fourth-order method in the interval [0,1] with step length 0.2:
(a) 𝑦 ′ = −𝑦 + 2 cos 𝑡, 𝑦 0 = 1.
(b) 𝑦 ′ = 2 + 𝑦 , 𝑦 0 = 0.8.
(c) 𝑦 ′ = (cos 𝑦)2 , 𝑦 0 = 0.