Matlab Assignment File
Matlab Assignment File
Name:………………………….
Roll No.:……………………….
Group:…………………………
Instructor:………………………
Experiment 1: Bisection Method
Algorithm of Intermediate Value Theorem (IVT): To determine all the sub intervals [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
1. 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.
Determine the proper resistor to dissipate to 1% of its original value q/q0 = 0.01 in t =
0.05 second with L = 5H and C = 10− 4 F. Use bisection method with an error of less than
0.0005.
3. A total charge Q is uniformly distributed around a ring shaped conductor with radius a . A
charge q is located at a distance x from the centre of the ring. The force exerted on the charge
1 qQx
by the ring is given by F = 4 π e 1.5 , where e0 = 8.854 × 10−12 C2 / Ν m2 . Find the
0 x2 +a2
distance x where the force is 1.2 N if q and Q are 1.25 × 10−5 c for a ring with radius of
0.9 m.
Experiment 2: Newton’s Method
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
1. 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. 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.
��
4. A mass balance for pollutant in a well-mixed lake can be written as: � �� = � − �� − �� �.
Given the parameter value � = 2 × 106 �3 , � = 2 × 105 �3 /yr., � = 2 × 106 g/yr, and
� = 0.33 �0.5 /�0.5 /��, the root can be located with fixed - point iteration for steady - state
�−�� 2 �−�� �
concentration as: (I) � = ��
(ii) � = �
. Only one will converge for initial
guesses of 2 < � < 6. Select the correct one and demonstrate why it will always work.
Experiment 4: Gauss Elimination Method
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.
2. 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
3. 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
4. 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
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.
1. 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:
2. 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
3. The following system of equations was generated by applying the current law to the circuit :
60 �1 − 40 �2 =200; − 40 �1 + 150 �2 − 100�3 =0; − 100 �2 + 130�3 =230. Solve it by
using Gauss - Seidel method correct to three decimal place with an initial vector 0, 0, 0 .
Experiment 6: Power Method and Lagrange Interpolation
1. 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
1 20 1 . Use x0 = [1,1,1]T and ϵ = 10-3
0 1 4
2. In a spring -mass system, the displacements of three masses from their equilibrium positions are the components of vector � . By
carrying out a force balance, the following equation was obtained:
−4 2 0 � �
2 −6 4 � =� � .
0 8 − 16 � �
An eigenvalue is related to a natural frequency of oscillation. Each solution of this
problem represents the condition under which the displacement masses oscillate in
phase at the same frequency. Find the eigenvalues and eigenvectors of the matrix using Power
method by taking initial vector �0 = 1, 1, 0 �
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
1. 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).
Determine � at � = 0.23.
3. You measure the voltage drop � across a register for a number of different values of current �.
The results are:
� 0.25 0.75 1.25 1.5 2.0
� - 0.45 - 0.6 0.7 1.88 6.0
Use first - through fourth order interpolating polynomial to estimate the voltage drop for
� = 1.15. Interpret your result.
Experiment 7: Newton’s Divided Difference Interpolation
��−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 ��� ≡ ��−� �
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 11: Correlation Coefficient and Regression lines
1 � 1 �
where � = �
�
�=1 �
and � = �
�.
�=1 �
Write an algorithm to determine Karl Pearson’s Correlation Coefficient.
Regression lines:
Given a set of n observations �� , �� , i = 1,2,…,n of the pair of variables x and y.
If the variables in a bivariate distribution are related, then the scatter diagram of that
distribution will concentrate around some curve called as curve of regression. If this curve is a
straight line then the line is called line of regression. The line of regression is the line of best
fit and is obtained by the principle of least squares.
The equation of line of regression of y on x is:
���
� − � = ��� � − � where ��� = ��
The equation of line of regression of x on y is:
��
� − � = ��� � − � where ��� = � �
�
1 � 1 �
Here �� = � �=1
�� − � 2 and �� = � �=1
�� − � 2 are the standard deviations.
Write an algorithm to determine regression lines.
3. The population (p) of a small community on the outs curt of a city grows rapidly over a 20 -
year period:
t 0 5 10 15 20