0% found this document useful (0 votes)
478 views14 pages

Matlab File Uma011

The document provides instructions for numerical analysis experiments using MATLAB to be completed by students for their B.E. Second Year course at Thapar Institute of Engineering and Technology, Patiala. It includes algorithms and examples for solving nonlinear equations using bisection, Newton's, and secant methods, linear systems using Gauss elimination and LU factorization, and fixed-point iteration. Students are asked to implement the algorithms in MATLAB and apply them to example problems to find roots, eigenvalues, and numerical solutions.

Uploaded by

GUNJAN KHULLAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
478 views14 pages

Matlab File Uma011

The document provides instructions for numerical analysis experiments using MATLAB to be completed by students for their B.E. Second Year course at Thapar Institute of Engineering and Technology, Patiala. It includes algorithms and examples for solving nonlinear equations using bisection, Newton's, and secant methods, linear systems using Gauss elimination and LU factorization, and fixed-point iteration. Students are asked to implement the algorithms in MATLAB and apply them to example problems to find roots, eigenvalues, and numerical solutions.

Uploaded by

GUNJAN KHULLAR
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

NUMERICAL ANALYSIS

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

2. Algorithm of Bisection Method: To determine a root of f(x) = 0 that is accurate within a


specified tolerance value ϵ, given values a and b such that f(a) * f(b) < 0.
Define c = (a + b)/2.
if f(a) * f(c) < 0, then set b = c, otherwise a = c.
end if.
Until |a – b| ≤ ϵ (tolerance value).
Print root as c.
Stopping Criteria: Since this is an iterative method, we must determine some stopping
criteria that will allow the iteration to stop. Criteria |f(ck)| very small can be misleading since
it is possible to have |f(ck)| very small, even if ck is not close to the root.
The interval length after N iterations is (b – a)/2N. So, to obtain an accuracy of ϵ, we must
have
log 𝑏 − 𝑎 − 𝑙𝑜𝑔 ∈
𝑁 ≥ .
log 2

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)

4. An oscillating current in an electric circuit is described by 𝑖 = 9𝑒 −𝑡 sin⁡


(2𝜋𝑡), where t is in
seconds. Determine the lowest value of t such that i = 3.5.
Experiment 3: Fixed-point Iteration Method

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.

2. Algorithm for LU factorization method: Find a solution of system of linear equations.


Input: Number of unknowns and equations n, matrix 𝐴 = 𝑎𝑖𝑗 , 1 ≤ 𝑖 ≤ 𝑛, 1 ≤ 𝑗 ≤ 𝑛
evaluated by executing Steps 1 to 6 of Gauss Elimination method, mji, 1 ≤ 𝑖 ≤ 𝑛, 1 ≤ 𝑗 ≤ 𝑛
evaluated in Step 5 Gauss Elimination method.
Step 1: Take U = A.
Step 2: Set lji = mji.
Step 3: Set lii = 1
Step 4: Rewrite Ax = b as (LU)x = b
Step 5: Solve Ly = b for y and Ux = y for x.

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

Take V = 5.5 and solve the system.


Experiment 5: Gauss-Seidel and SOR Methods

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.

2. Write an algorithm for Successive-Over-Relaxation (SOR) method.

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:

(a) 10x + 8y – 3z + u = 16 (b) 4𝑥1 + 𝑥2 − 𝑥3 + 𝑥4 = −2


2x + 10y + z – 4u = 9 𝑥1 + 4𝑥2 − 𝑥3 − 𝑥4 = −1
3x – 4y + 10z + u = 10 −𝑥1 − 𝑥2 + 5𝑥3 + 𝑥4 = 0
2x + 2y – 3z + 10u = 11 𝑥1 − 𝑥2 + 𝑥3 + 3𝑥4 = 1

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

1. Algorithm for Power method:


Step 1: START
Step 2: Define matrix A and initial guess x.
Step 3: Calculate y = Ax
Step 4: Find the largest element in magnitude of matrix y and assign to K.
Step 5: Calculate fresh value x = (1/K) * y.
Step 6: If 𝐾 𝑛 − 𝐾 𝑛 − 1 > error, goto Setp 3.
Step 7: STOP.

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

3. Algorithm for Lagrange interpolation: Given a set of function values

x x1 x2 …… xn
f(x) f(x1) f(x2) …… f(xn)

To approximate the value of a function f(x) at x = p using Lagrange’s interpolating


polynomial 𝑃𝑛 −1 𝑥 of degree ≤ n – 1, given by
𝑃𝑛−1 𝑥 = l1(x) f(x1) + l2(x) f(x2) +…………. + ln(x) f(xn)
𝑛 (𝑝−𝑥 𝑗 )
where 𝑙𝑖 𝑥 = 𝑗 =1; 𝑖≠𝑗 (𝑥 −𝑥 ) at x = p.
𝑖 𝑗

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

Step 3. Print sum.

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

1. Algorithm for Newton’s divided difference interpolation:


Given n distinct numbers 𝑥1 , 𝑥2 , … , 𝑥𝑛 and their corresponding function values
𝑓(𝑥1 ), 𝑓(𝑥2 ), … , 𝑓(𝑥𝑛 ).
Approximate the value of a function f(x) at x = p using Newton’s divided difference
interpolating polynomial 𝑃𝑛−1 𝑥 of degree ≤ n – 1.

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

2. The following data represents the function 𝑓 𝑥 = 𝑒 𝑥 .


x 1 1.5 2.0 2.5
f(x) 2.7183 4.4817 7.3891 12.1825

Estimate the value of f(2.25) using the Newton’s divided difference interpolation. Compare
with the exact value.

3. Approximate f(0.43) by using Newton’s divided difference interpolation, construct the


interpolating polynomials for the following data.
f(0) = 1, f(0.25) = 1.64872, f(0.5) = 2.71828, f(0.75) = 4.4816.
Experiment 8: Least Square Approximation

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

1. Algorithm for Composite Trapezoidal rule:


Step 1: Input function f(x); end points a and b; and N number of subintervals.
𝑏−𝑎
Step 2: Set 𝑕 = 𝑁
.
Step 3: Set sum = 0
Step 4: for i = 1 to N-1
Step 5: Set 𝑥 = 𝑎 + 𝑕 ∗ 𝑖
Step 6: Set 𝑠𝑢𝑚 = 𝑠𝑢𝑚 + 2 ∗ 𝑓(𝑥)
end i
Step 7: Set 𝑠𝑢𝑚 = 𝑠𝑢𝑚 + 𝑓 𝑎 + 𝑓(𝑏)
𝑕
Step 8: Set 𝑎𝑛𝑠 = 𝑠𝑢𝑚 ∗ 2
STOP

2. Algorithm for Composite Simpson’s rule:


Step 1: Input function f(x); end points a and b; and N number of subintervals (even).
𝑏−𝑎
Step 2: Set 𝑕 = .
𝑁
Step 3: Set sum = 0
Step 4: for i = 1 to N-1
Step 5: Set 𝑥 = 𝑎 + 𝑕 ∗ 𝑖
Step 6: if rem(i,2) = = 0
𝑠𝑢𝑚 = 𝑠𝑢𝑚 + 2 ∗ 𝑓 𝑥
else
𝑠𝑢𝑚 = 𝑠𝑢𝑚 + 4 ∗ 𝑓 𝑥
end if
end i
Step 7: Set 𝑠𝑢𝑚 = 𝑠𝑢𝑚 + 𝑓 𝑎 + 𝑓 𝑏
𝑕
Step 8: Set 𝑎𝑛𝑠 = 𝑠𝑢𝑚 ∗ 3
STOP

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

1. Algorithm for Euler Method


Approximate the solution of the initial value problem 𝑦 ′ = 𝑓 𝑡, 𝑦 𝑎 ≤ 𝑡 ≤ 𝑏, 𝑦 𝑎 = 𝛼 in
the interval [a, b] with step length h.
Input: function 𝑓 𝑡, 𝑦 ; endpoints a, b; step length h; initial condition 𝑡1 = 𝑎 and 𝑦1 = 𝛼.
Output: approximation of 𝑦 in the interval [a, b].
Step 1: Evaluate number of sub-intervals 𝑛 = (𝑏 − 𝑎)/𝑕.
Step 2: For 𝑖 = 1,2, … , 𝑛 do Steps 3 and 4.
Step 3: Evaluate 𝑦𝑖+1 = 𝑦𝑖 + 𝑕 ∗ 𝑓 𝑡𝑖 , 𝑦𝑖
Step 4: Set 𝑡𝑖+1 = 𝑡𝑖 + 𝑕
Step 5: Output 𝑡, 𝑦 .
STOP

2. Algorithm for Runge-Kutta of fourth-order method:


Approximate the solution of the initial value problem 𝑦 ′ = 𝑓 𝑡, 𝑦 𝑎 ≤ 𝑡 ≤ 𝑏, 𝑦 𝑎 = 𝛼 in
the interval [a, b] with step length h.
Input: function 𝑓 𝑡, 𝑦 ; endpoints a, b; step length h; initial condition 𝑡1 = 𝑎 and 𝑦1 = 𝛼.
Output: approximation of 𝑦 in the interval [a, b].
Step 1: Evaluate number of sub-intervals 𝑛 = (𝑏 − 𝑎)/𝑕.
Step 2: For 𝑖 = 1,2, … , 𝑛 do Steps 3 to 5.
Step 3: Set 𝐾1 = 𝑕 ∗ 𝑓 𝑡𝑖 , 𝑦𝑖 ;
𝑕 𝐾1
𝐾2 = 𝑕 ∗ 𝑓 𝑡𝑖 + , 𝑦𝑖 + ;
2 2
𝑕 𝐾2
𝐾3 = 𝑕 ∗ 𝑓 𝑡𝑖 + 2
, 𝑦𝑖 + 2
;
𝐾4 = 𝑕 ∗ 𝑓 𝑡𝑖 + 𝑕, 𝑦𝑖 + 𝐾3 .
𝐾1 +2𝐾2 +2𝐾3 +𝐾4
Step 4: Set 𝑦𝑖+1 = 𝑦𝑖 + (𝐶𝑜𝑚𝑝𝑢𝑡𝑒 𝑦𝑖+1 )
6
Step 5: Set 𝑡𝑖+1 = 𝑡𝑖 + 𝑕. (𝐶𝑜𝑚𝑝𝑢𝑡𝑒 𝑡𝑖 )
Step 6: Output 𝑡, 𝑦 .
STOP

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.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy