Question

Write a program in python to find a single root of a function f(x) on an...

Write a program in python to find a single root of a function f(x) on an interval [a,b] using the bisectional
method. Test your program on an equation that you may easily solve analytically.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#*******Python code***************

def findFuncValue(c,f):
#Lets say f(x)= a*x^n+b*x^n-1+c*x^n-3+..........+1 . Here f =[a,b,c,....,1]
#value to store the function value
value = 0.0
for i in range(0,len(f)):
#find value at each coefficient
value+=(f[i]*(c**(len(f)-i-1)))
return value
  
def getSingleRootOfFuncf(f,a,b):
#variable to count the iterations
iteration =1
#mid point of a and b
c = (a+b)/2.0
#run while loop till func value at c is 0 or
#diff between b-a is very small
while findFuncValue(c,f)!=0 or (b-a)/2 > 0.001:
#print the value at each iteration
print "iteration=%s\ta=%s\tb=%s\tc=%s\tf(c)=%s"%(iteration,a,b,c,findFuncValue(c,f))
#if sign of func value at c and
#function value at a is of same sigh
if findFuncValue(c,f)*findFuncValue(a,f) > 0:
#then update value of a to c
a=c
else:
#if funcation value at a and c are of different sign
b=c
#update the mid point c,based on updated a and b for next iteration
c = (a+b)/2.0
iteration+=1
return c

if __name__ == '__main__':

#***Test case 1**
f =[1,0,-1,-2]
a =1
b=2
#***Test case 2****
f=[1,-2]
a = 1
b =4
#call function to get a root using bisection method
root = getSingleRootOfFuncf(f,a,b)
print 'A Root of f is: ',root

  

#***********Python code screenshot*******

  

#*********test for function f = x^2-2x where a =1 and b=4

#***Value of iteration number , a, b and c and f(c) till it converges******

iteration=1 a=1 b=4 c=2.5 f(c)=0.5

iteration=2 a=1 b=2.5 c=1.75 f(c)=-0.25

iteration=3 a=1.75 b=2.5 c=2.125 f(c)=0.125

iteration=4 a=1.75 b=2.125 c=1.9375 f(c)=-0.0625

iteration=5 a=1.9375 b=2.125 c=2.03125 f(c)=0.03125

iteration=6 a=1.9375 b=2.03125 c=1.984375 f(c)=-0.015625

iteration=7 a=1.984375 b=2.03125 c=2.0078125 f(c)=0.0078125

iteration=8 a=1.984375 b=2.0078125 c=1.99609375 f(c)=-0.00390625

iteration=9 a=1.99609375 b=2.0078125 c=2.001953125 f(c)=0.001953125

iteration=10 a=1.99609375 b=2.001953125 c=1.9990234375 f(c)=-0.0009765625

iteration=11 a=1.9990234375 b=2.001953125 c=2.00048828125 f(c)=0.00048828125

iteration=12 a=1.9990234375 b=2.00048828125 c=1.99975585938 f(c)=-0.000244140625

iteration=13 a=1.99975585938 b=2.00048828125 c=2.00012207031 f(c)=0.0001220703125

iteration=14 a=1.99975585938 b=2.00012207031 c=1.99993896484 f(c)=-6.103515625e-05

iteration=15 a=1.99993896484 b=2.00012207031 c=2.00003051758 f(c)=3.0517578125e-05

iteration=16 a=1.99993896484 b=2.00003051758 c=1.99998474121 f(c)=-1.52587890625e-05

iteration=17 a=1.99998474121 b=2.00003051758 c=2.00000762939 f(c)=7.62939453125e-06

iteration=18 a=1.99998474121 b=2.00000762939 c=1.9999961853 f(c)=-3.81469726562e-06

iteration=19 a=1.9999961853 b=2.00000762939 c=2.00000190735 f(c)=1.90734863281e-06

iteration=20 a=1.9999961853 b=2.00000190735 c=1.99999904633 f(c)=-9.53674316406e-07

iteration=21 a=1.99999904633 b=2.00000190735 c=2.00000047684 f(c)=4.76837158203e-07

iteration=22 a=1.99999904633 b=2.00000047684 c=1.99999976158 f(c)=-2.38418579102e-07

iteration=23 a=1.99999976158 b=2.00000047684 c=2.00000011921 f(c)=1.19209289551e-07

iteration=24 a=1.99999976158 b=2.00000011921 c=1.9999999404 f(c)=-5.96046447754e-08

iteration=25 a=1.9999999404 b=2.00000011921 c=2.0000000298 f(c)=2.98023223877e-08

iteration=26 a=1.9999999404 b=2.0000000298 c=1.9999999851 f(c)=-1.49011611938e-08

iteration=27 a=1.9999999851 b=2.0000000298 c=2.00000000745 f(c)=7.45058059692e-09

iteration=28 a=1.9999999851 b=2.00000000745 c=1.99999999627 f(c)=-3.72529029846e-09

iteration=29 a=1.99999999627 b=2.00000000745 c=2.00000000186 f(c)=1.86264514923e-09

iteration=30 a=1.99999999627 b=2.00000000186 c=1.99999999907 f(c)=-9.31322574615e-10

iteration=31 a=1.99999999907 b=2.00000000186 c=2.00000000047 f(c)=4.65661287308e-10

iteration=32 a=1.99999999907 b=2.00000000047 c=1.99999999977 f(c)=-2.32830643654e-10

iteration=33 a=1.99999999977 b=2.00000000047 c=2.00000000012 f(c)=1.16415321827e-10

iteration=34 a=1.99999999977 b=2.00000000012 c=1.99999999994 f(c)=-5.82076609135e-11

iteration=35 a=1.99999999994 b=2.00000000012 c=2.00000000003 f(c)=2.91038304567e-11

iteration=36 a=1.99999999994 b=2.00000000003 c=1.99999999999 f(c)=-1.45519152284e-11

iteration=37 a=1.99999999999 b=2.00000000003 c=2.00000000001 f(c)=7.27595761418e-12

iteration=38 a=1.99999999999 b=2.00000000001 c=2.0 f(c)=-3.63797880709e-12

iteration=39 a=2.0 b=2.00000000001 c=2.0 f(c)=1.81898940355e-12

iteration=40 a=2.0 b=2.0 c=2.0 f(c)=-9.09494701773e-13

iteration=41 a=2.0 b=2.0 c=2.0 f(c)=4.54747350886e-13

iteration=42 a=2.0 b=2.0 c=2.0 f(c)=-2.27373675443e-13

iteration=43 a=2.0 b=2.0 c=2.0 f(c)=1.13686837722e-13

iteration=44 a=2.0 b=2.0 c=2.0 f(c)=-5.68434188608e-14

iteration=45 a=2.0 b=2.0 c=2.0 f(c)=2.84217094304e-14

iteration=46 a=2.0 b=2.0 c=2.0 f(c)=-1.42108547152e-14

iteration=47 a=2.0 b=2.0 c=2.0 f(c)=7.1054273576e-15

iteration=48 a=2.0 b=2.0 c=2.0 f(c)=-3.5527136788e-15

iteration=49 a=2.0 b=2.0 c=2.0 f(c)=1.7763568394e-15

iteration=50 a=2.0 b=2.0 c=2.0 f(c)=-8.881784197e-16

iteration=51 a=2.0 b=2.0 c=2.0 f(c)=4.4408920985e-16

iteration=52 a=2.0 b=2.0 c=2.0 f(c)=-2.22044604925e-16

A Root of f is: 2.0

#*****************Output Screenshot**********

#***Please do let me know if you have any doubts or want me to modify the code*************

Add a comment
Know the answer?
Add Answer to:
Write a program in python to find a single root of a function f(x) on an...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Matlab function for: 1. Root Finding: Calculate the root of the equation f(x)=x^3 −5x^2...

    Write a Matlab function for: 1. Root Finding: Calculate the root of the equation f(x)=x^3 −5x^2 +3x−7 Calculate the accuracy of the solution to 1 × 10−10. Find the number of iterations required to achieve this accuracy. Compute the root of the equation with the bisection method. Your program should output the following lines: • Bisection Method: Method converged to root X after Y iterations with a relative error of Z.

  • A5.2. Write a matlab program to find out the root of equation f (x)-x*-3x - 1, using false-positi...

    numerical method lab A5.2. Write a matlab program to find out the root of equation f (x)-x*-3x - 1, using false-position method. Use initial and upper guesses -1 and 1.5 False Position method: xr-Xu Explanation of False Position f(x)(x-xu) Hints change) xu far-feval (foxr) ; root-0.3294 Xr A5.2. Write a matlab program to find out the root of equation f (x)-x*-3x - 1, using false-position method. Use initial and upper guesses -1 and 1.5 False Position method: xr-Xu Explanation of...

  • 2. (a) We want to find the root x of the function f(x); that is, we need f(r) = 0 . This can be done using Newton&#3...

    2. (a) We want to find the root x of the function f(x); that is, we need f(r) = 0 . This can be done using Newton's method, making use of the iterative formula f(xn) Show that the sequence ofiterates (%) converges quadratically if f'(x) 0 in some appropriate interval of x-values near the root χ 9 point b) We can get Newton's method to find the k-th root of some number a by making it solve the non-linear cquation...

  • c++ Newton method for iteratively finding the root f(x) = 0. The equation is Where f(x)...

    c++ Newton method for iteratively finding the root f(x) = 0. The equation is Where f(x) is the function, f'(x) is the derivative of f9x), Write a C++ program to find root for the function of f(x). The function is on your C++ homework 2 for F(x) = x + 2x -10 You may have two functions, for example, float f(float x) float f=x*x-4; //any function equation return f; float prime(float x) float prime = 2 * x; //derivative of...

  • This program has to be written in matlab 2. Write a function to find a root...

    This program has to be written in matlab 2. Write a function to find a root of f(c) using the secant method. Call this routine secant, call the file secant.m; its first line should be function [x,nf] = secant (fname, x0, x1, tol) fname is the name of the m-file which evaluates the function f(x), 20 and 21 are initial approximations to .c", and tol is a stopping tolerance. Your code should return an approximation x = 2X+1 to 3*...

  • Questions: 1. Write a MATLAB program to find all the roots of a given, twice continuously differe...

    Questions: 1. Write a MATLAB program to find all the roots of a given, twice continuously differentiable, function f E C2[a, b]. Your program should first probe the function f(x) on the given interval to find out where it changes sign. (Thus, the program has, in addition to f itself, four other input arguments: a, b, the number nprobe of equidistant values between a and b at which f is probed, and a tolerance tol.) For each subinterval [ai,b] over...

  • 13. Write a MATLAB program to find all the roots of a given, twice continuously differentiable,...

    13. Write a MATLAB program to find all the roots of a given, twice continuously differentiable, function f e C2la,b]. on the given interval to find out where it Your program should first probe the function f(x) changes sign. (Thus, the program has, in addition to f itself, four other input arguments: a, b, the number nprobe of equidistant values between a and b at which f is probed, and a tolerance tol.) For each subinterval [a,b;] over which the...

  • In MATLAB please Consider the nonlinear function: y = f(x) = x3 cos x a. Plot...

    In MATLAB please Consider the nonlinear function: y = f(x) = x3 cos x a. Plot y as a function of x as x is varied between -67 and 67. In this plot mark all the locations of x where y = 0. Make sure to get all the roots in this range. You may need to zoom in to some areas of the plot. These locations are some of the roots of the above equation. b. Use the fzero...

  • Write a matlab program to implement the secant root finding method in matlab. The function name...

    Write a matlab program to implement the secant root finding method in matlab. The function name should be Secant and it should take the equation as input whoes root has to be found and the two initial values of a and b and maximum tolerable error. Consider the following example: Your code should generate the following: >> secantAssg5(@(x)(x^4+x^2+x+10),2,3,0.0001)    Xn-1      f(Xn-1)      Xn      f(Xn)      Xn+1      f(Xn+1) 2.0000   32.0000    3.0000 103.0000    1.5493   19.7111 ….. ….. ….. Root is x = 0.13952 ans...

  • Given 2x+4y-z 2 -2x -3y +7z 10 Write a program to solve the linear system above using Gauss- Elim...

    write c++ to solve this linear system. Given 2x+4y-z 2 -2x -3y +7z 10 Write a program to solve the linear system above using Gauss- Elimination. Question2 Let f(x) = x3-6x2 + 11x-6, Write a program to i) Check whether the root exist in the interval: a) .5, 5.0] b) 2.5, 4.0] ii Based on interval found in 4(i), find the root using Bisection method. Do your calculation in 4 decimal points and 0.0001. Programming language: C or C++ Given...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT