Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can i build this code with python? The file assignment3.txt will contain the following inputs: Line 1: An elementary function f(x) of the variable

How can i build this code with python?

The file assignment3.txt will contain the following inputs:

Line 1: An elementary function f(x) of the variable x, in the form of a string that can be converted to a SymPy expression.

Line 2: An interval of the form [a,b] that contains exactly one root of f(x), where a and b are floating point numbers.

Line 3: An error tolerance , in the form of a floating point number.

Line 4: A polynomial p(x) that can be factored completely into the product of linear terms, whose roots all lie in the interval [1000,1000], and whose distinct roots are all separated by more than one unit.

A sample file assignment3.txt is included with the assignment, for students to use while testing their code.

3*x-exp(x) [0,1] 0.00001 2*x**6-309*x**5-1618395*x**4-14559654*x**3+211748765892*x**2+22758856748365*x-53702556738295

The file used when grading student submissions will have the same types of entries, but the specific values in it will be different.

This assignment consists of two related, but distinct, parts. As a preliminary step, student code should do the following:

(1) Read the entries from the file assignment3.txt and, where necessary, convert them to the correct data types.

The sympify function in SymPy will be helpful here. In the first part, student code should do the following:

(2) Approximate the root of f(x) in [a,b] to within an error of using each of the following methods:

(I) The bisection method with initial interval [a,b] (II) Newtons method with initial guess the midpoint of [a,b]

(III) The secant method with initial guesses a and b Note: Students must code their own algorithms for each of the three methods.

Best practice is to write separate functions for each.

They may not simply call preexisting rootfinding functions in any of the various Python packages.

(3) In a single table, list the approximations for the root provided by each of the three methods, plus the number of iterations of that methods core loop that were used to produce it (start each counter with the first computation of a new term, i.e., dont count the initial guess).

In the second part, student code should do the following:

(4) Find an interval of length one guaranteed to contain a root of p(x).

(5) Employ the bisection method on that interval to approximate that root r to within an error tolerance of .

(6) Employ synthetic, or polynomial, division to produce an approximate factorization p(x) = (xr)p1(x), where p1(x) is a polynomial of one degree less than p(x). Note: The algorithm for synthetic division was discussed with Horners method for evaluating polynomials. Students must code this algorithm themselves, rather than relying on preexisting functions in any of the various Python packages.

(7) Return to step (4) with p1(x). Continue until p(x) has been completely factored into a product of linear terms. Note: Students should consider carefully how the errors in the roots of these polynomials compound. The first root found this way will be within of an actual root of p(x), but will the second? The third? In fact, the errors in later roots can be larger than the errors in earlier ones. However, such careful error analysis is beyond the scope of this project.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Pro SQL Server Administration

Authors: Peter Carter

1st Edition

1484207106, 9781484207109

More Books

Students also viewed these Databases questions