Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

How can I build this code with python?

The file assignment2.txt will contain the following inputs:

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

Line 2: The coefficients ak of a finite series, indexed by k, that when summed from 1 to n produce f(n), i.e., n k=1 ak = f(n)

Line 3: A list of positive integers in the form N1,...,Nm (without spaces between terms)

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

n/(n+1) 1/(k*(k+1)) 10,100,1000,10000,100000

The file used when grading student submissions will have the same types of entries, but the specific values in it will be different. Student code should do the following:

(1) Read the entries from the file assignment2.txt and, where necessary, convert them to the correct data types. The sympify function in SymPy will be helpful here.

(2) For each i from 1 to m (where m is the largest index of the N1,...,Nm), compute Ni k=1 ak using each of the following methods:

(I) LS (largest-to-smallest) with chopping to ten decimal places

(III) LS (largest-to-smallest) with rounding to ten decimal places

(II) SL (smallest-to-largest) with chopping to ten decimal places

(IV) SL (smallest-to-largest) with rounding to ten decimal places Warning: Students should code their own functions to compute these sums, rather than relying on preexisting summation functions, to be sure that the correct methods are being used. The built-in round() and int() functions may be used to round and chop, respectively (the latter requires scaling and will only chop nonnegative numbers correctly).

(3) Compute the correct values f(Ni), rounded to ten decimal places.

(4) Compute the errors Ei = f(Ni) Ni k=1 ak for each of the four methods, rounded to ten decimal places.

(5) Create a single table that lists, for all four methods, all of the approximations of f(Ni) and the corresponding errors Ei . The tabulate() and PrettyTable() functions, from their respective packages, may be helpful here.

While working on the assignment, students may find it helpful to consult the documentation on the built-in file input/output functions in Python (https://docs.python.org/3/tutorial/inputoutput.html), the SymPy package (https://docs.sympy.org/latest/index.html), the tabulate package (https://pypi.org/project/tabulate/), or the prettytable package (https://pypi.org/project/prettytable/). Various other tutorials are also available online.

Output: Student submissions should product the following output:

(1) The function f(n) to be approximated, the finite series that will be used to approximate it, and the values N1,...,Nm at which it will be approximated.

(2) The correct values of f(Ni), rounded to ten decimal places.

(3) A single table that lists, for all four methods, all of the approximations of f(Ni) and the corresponding errors Ei , all rounded to ten decimal places.

Text output may be produced using the print function and should be easily understandable by human readers (e.g., The function f(n) = n n+1 , which equals the finite series n k=1 1 k(k+1) , will be approximated at 10, 100, 1000, 10000, and 100000 using largest-to-smallest (LS) and smallest-to-largest (SL) summation with chopping and rounding.)

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

More Books

Students also viewed these Databases questions

Question

4. Identify the challenges facing todays organizations

Answered: 1 week ago