Assignment 01 - SP22
Assignment 01 - SP22
Islamabad Campus
Department of Computer Science
b) A new method for solving a nonlinear equation f(x) = 0 is proposed. The method is similar to the
bisection method. The solution starts by finding an interval [a, b] that brackets the solution. The first
estimate of the solution is the midpoint between x = a and x = b. Then the interval [a, b] is divided into
four equal sections. The section that contains the root is taken as the new interval for the next iteration.
Write a user-defined function(program) that solves a nonlinear equation with the proposed new
method. Name the function Xs = QuadSecRoot(Fun, a, b), where the output argument Xs is the
solution. The input argument Fun is a name for the function that calculates f(x) for a given x (it is a
dummy name for the function that is imported into QuadSecRoot), a and bare two points that bracket
the root.
The iterations should stop when the tolerance, is smaller than 10-6 xNs (xNs is the current estimate of
the solution).
Use the user-defined QuadSecRoot function to solve the equation cosx -5x +2 = 0
Question # 2
a) Find the root of the equation cosx = 5x - 2 between 0.4 and 0.6 correct to four decimal places using
method of false position (Regula Falsi method).
b) Write a computer program (in C++) to implement the method.
SP22 Page 1
Question # 3
a) Find the root of the equation 2coshx*sinx =1 using secant method, with an accuracy of 5 dp. Take 0.4
and 0.5 as the two starting values.
b) Write a computer program (in C++) to implement the method.
Question # 4
a) Find the root of the equation f(x) = x3 -2x -2, correct to 3dp, using Newton Raphson method.
b) Bisection algorithm and Newton-Raphson algorithm are discussed in class. Design a new algorithm to
compute the roots of nonlinear equations f(x)=0 by combining Bisection algorithm and Newton-
Raphson algorithm. Here is the Pseudocode:
Given f , f’, a , b , and δ=10-6 (tolerance)
Step 1 : for i=1 to 2
Step 2 : xi = (a+b) / 2
Step 3 : if f (xi ) = 0 or f (xi ) < δ , then step 10
Step 4 : if f (a) * f (xi) < 0 , then b= xi
Step 5 : else a = xi
Step 6 : end for
Step 7 : x = xi - [ f ( xi ) / f’(xi) ]
Step 8 : if f (x) < δ , then go to step 10
Step 9 : xi = x , and go to step 7
Step 10 : stop iteration .
Find the real root of quartic function f(x) = x4 +3x3 – 15x2 -2x + 9 with tolerance δ=10-6 using
i. Bisection Method
ii. Newton Raphson Methods
iii. Hybrid Algorithm
c) Compute the approximate percent relative errors for your solutions in part i, ii, and iii.
Question # 5
π
a) Find the root of the equation 2x -cosx -3 = 0 correct to 3dp, using iteration method. Take x0 ¿
2
b) Write a computer program (in C++) to implement the method.
SP22 Page 2