Question

In: Computer Science

You have been provided with starter code that contains the place holders and some implementations for...

You have been provided with starter code that contains the place holders and some implementations for Bisection and Secant methods. Complete the code where it says #provide this line so that it could be run for the following case:

1) ?(?) = ? 3 + 4, ?? = −3, ?? = 0, ??????? = 100, ?? = 0.2

Starter Code:

def eq(x):

          return x**3+4

def err(xr,xold):

          return abs((xr-xold)/xr)*100

def bisection(xl,xu,maxIter,sc):

    # xl is the lower bound of the initial bracket

    # xu is the upper bound of the initial bracket

    # maxIter is the maximum number of iteration for finding the root

    # sc is the stopping criteria

    

          xm=(xl+xu)/2

          for i in range(maxIter):

                    if eq(xl)*eq(xm)>0:

                        # provide this line

                        # provide this line

                    else:

                         # provide this line

                         # provide this line

                    xn=(xl+xu)/2

                    if  # provide this line

                              return xn

                    xm=xn

def secant(xi,xip1,maxIter,sc):

    # xip1 is xi plus one

    # maxIter is the maximum number of iteration for finding the root

    # sc is the stopping criteria

          for i in range(maxIter):

                    eqDer= # provide this line

                    xi=xip1

                    # provide this line

                    if eq(xip1)=1: # provide this line

                              return xip1

Solutions

Expert Solution

Bisection Method

Step1: First let us understand about bisection method.

> Given a function f(x) on number x.

> Also given two numbers ‘p’ and ‘q’ such that f(p)*f(q) < 0 and f(x) is continuous in [p, q].

Here f(x) represents algebraic or transcendental equation.

> Now we need to find the root of function in interval [p, q] such that f(x) is 0.e

Step 2: Let us have the algorithm for the bisection method

  1. Let r=(p+q)/2

  2. If f(r)=0, stop and return cc.

  3. If sign(f(p))≠sign(f(r)), then set q=r. Else if sign(f(q))≠sign(f(r)), then set p=r.

  4. Go to the beginning and repeat until convergence.

Secant Method

Step 1: It is the procedure to find the roots of an equation for two distinct values using an interpolation method.

Step 2: The algorithm for the second method is given as follows:

  1. Take the initial values of p, q and r. Here r is the stopping criteria, absolute error or the desired degree of accuracy
  2. Compute f(p) and f(q)
  3. Compute s = (p*f(q) – q*f(p)) / (f(q) – f(p))
  4. Test for accuracy of s
    If | (s – q)/s | > r, then assign p = q and q = s
    goto step 3 Else, go to step 4
  5. Display the required root as s.
  6. Stop

def eq(x):

          return x**3+4

def err(xr,xold):

          return abs((xr-xold)/xr)*100

def bisection(xl,xu,maxIter,sc):

    # xl is the lower bound of the initial bracket

    # xu is the upper bound of the initial bracket

    # maxIter is the maximum number of iteration for finding the root

    # sc is the stopping criteria

   xm=(xl+xu)/2

for i in range(maxIter):

                    if eq(xl)*eq(xm)>0:

xl=xm# provide this line

xu=xu# provide this line

                    else:

xu=xm# provide this line

xl=xl# provide this line

                    xn=(xl+xu)/2

                    if xn==0:# provide this line

                              return xn

                    xm=xn

def secant(xi,xip1,maxIter,sc):

    # xip1 is xi plus one

    # maxIter is the maximum number of iteration for finding the root

    # sc is the stopping criteria

          for i in range(maxIter):

                    eqDer=xi - (xip1-xi)*f(xi)/( f(xip1) - f(xi) ) # provide this line

                    xi=xip1

xip1=eqDer# provide this line

                    if eq(xip1)==1: # provide this line

                              return xip1


Related Solutions

Using JAVA and NETBEANS Assignment Content For this assignment, you will develop "starter" code. After you...
Using JAVA and NETBEANS Assignment Content For this assignment, you will develop "starter" code. After you finish, your code should access an existing text file that you have created, create an input stream, read the contents of the text file, sort and store the contents of the text file into an ArrayList, then write the sorted contents via an output stream to a separate output text file. Copy and paste the following Java™ code into a JAVA source file in...
You have been provided information on summary statistical data for two neighborhoods. Specifically, you have been...
You have been provided information on summary statistical data for two neighborhoods. Specifically, you have been provided calculations on the Average Price Per Square Foot, Average Age and Average, and the Average Prices for houses in the two neighborhoods and the Standard Deviation of each variable. Apply the method of Confidence Intervals at (97%) to Estimate answer the question whether: Average Age of House in each neighborhood is significantly different from 60 years Average Price of Houses in each neighborhood...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that...
I Have posted my Java code below. Fix the toString, add, and remove implementations so that the following test cases work. Note: I have removed all the unnecessary inherited List implementations. I have them to: throw new UnsupportedException(); For compilation, you could also add //TODO. Test (Main) List list = new SparseList<>(); list.add("0"); list.add("1"); list.add(4, "4"); will result in the following list of size 5: [0, 1, null, null, 4]. list.add(3, "Three"); will result in the following list of size...
Assume that you have been asked to place a value on the acquisition of Briarwood Hospital....
Assume that you have been asked to place a value on the acquisition of Briarwood Hospital. Its projected profit and loss statements and retention requirements are shown below (in millions): Year 1 Year 2 Year 3 Year 4 Year 5 Net revenues $225.0 $240.0 $250.0 $260.0 $275.0 Cash expenses $200.0 $205.0 $210.0 $215.0 $225.0 Depreciation $11.0 $12.0 $13.0 $14.0 $15.0    Earnings before interest and taxes $14.0 $23.0 $27.0 $31.0 $35.0 Interest $8.0 $9.0 $9.0 $10.0 $10.0    Earnings before...
Assume that you have been asked to place a value on the fund capital (equity) of...
Assume that you have been asked to place a value on the fund capital (equity) of BestHealth, a not-for-profit health maintenance organization (HMO). Its projected profit and loss statements and equity reinvestment (asset) requirements are as follows: 2016 2017 2018 2019 2020 Net Revenues $50.00 $52.00 $54.00 $57.00 $60.00 Cash expenses $45.00 $46.00 $47.00 $48.00 $49.00 Depreciation $3.00 $3.00 $4.00 $4.00 $4.00 Interest $1.50 $1.50 $2.00 $2.00 $2.50 Net Profit $0.50 $1.50 $1.00 $3.00 $4.50 Asset Requirements $0.40 $0.40 $0.40...
The following data have been provided to you: (all amounts are in dollars)            ...
The following data have been provided to you: (all amounts are in dollars)                                                                   April   May Sales 150,000 157,500 Merchandise purchases 107,000 112,400 Operating expenses:              Payroll 13,600         14,280      Advertising 5,400           5,700      Rent 2,500           2,500      Depreciation 7,500           7,500 End of April balances:              Cash 30,000          Bank loan payable     26,000 Additional data: Sales are 40% cash and 60% on...
You have been given a solution that contains a Group I, a Group II, and a...
You have been given a solution that contains a Group I, a Group II, and a Group III cation. · Develop a qualitative analysis scheme for these cations by selecting reagents and conditions for reactions. · Write the balanced net reaction for each cation reaction.
You have been provided the following data on the securities of three firms and the market:...
You have been provided the following data on the securities of three firms and the market: Security E[Rj] sj rjM bj Firm A 0.13 .12 ? .90 Firm B 0.16 ? 0.40 1.10 Firm C 0.25 0.24 0.75 ? Market 0.15 0.10 1 1 Risk-free 0.05 0 0 0 Assume the CAPM holds true. Fill in the missing values in the table. 0.9 = (rjM)(0.12) / 0.10 ri,m= .75 bj     = (rjM)(sj) / sm = .4 (sj) / .10...
JAVA: Create a circle that contains the following... Sample code to work from provided below. FIELDS:...
JAVA: Create a circle that contains the following... Sample code to work from provided below. FIELDS: a private double that holds the radius. CONSTRUCTORS a no argument constructor that sets the radius to zero a constructor that takes a single argument of type double which is the radius. METHODS a method called "getRadius" which returns the current radius a method called "setRadius" which takes a single parameter of type double containing the new radius a method called "getArea" which returns...
You work for a small investment management firm. You have been provided with the following historical...
You work for a small investment management firm. You have been provided with the following historical information for three stocks and the market index. The information is shown in the table below.             AAA Inc. BBB Inc. CCC Inc. Stock Price Dividend Stock Price Dividend Stock Price Dividend Market Index 2012 $47 $125 $145 20,840 2013 $52 $1.40 $135 $1.50 $160 $3.50 23,320 2014 $57 $1.40 $103 $1.55 $170 $3.60 22,220 2015 $62 $1.70 $130 $1.65 $185 $3.65 25,325 2016...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT