Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

How can I write this code with Python? The file assignment1.txt will contain the following inputs: Line 1: An elementary function f(x), in the form

How can I write this code with Python? The file assignment1.txt will contain the following inputs:

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

Line 2: The domain of f(x) in the form [a,b], where a and b are floating point numbers

Line 3: A point c in [a,b] at which to approximate f(x), in the form of a floating point number

Line 4: A maximum allowable error E for that approximation, in the form of a floating point number

assignment1.txt is exp(sin(x^2)) [0.0,4.0] 3.21 0.02

Student code should do the following:

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

(2) Determine the degree n of the first Taylor polynomial pn(x), centered at the midpoint of [a,b], guaranteed by Taylors theorem to approximate f(x) to within the given error E on the entire interval [a,b]. Warning: To find this, your code will need to find the absolute maximum of | f (k) (x)| on [a,b]. In this assignment, you may do that using the optimization functions in the SciPy package, specifically the minimize scalar function. (Remember that maximizing a function may be done by minimizing its negative.) However, minimize scalar only finds a local minimum! For simplicity, you may assume that | f k (x)| has only one local maximum on [a,b] (which must then be its absolute maximum).

(3) Use the Taylor polynomial of degree n to approximate f(x) at the given point c and find the error in this approximation (of the form Rn(c) = pn(c) f(c)).

(4) Plot f(x) and the Taylor polynomial pn(x) using the plot function in SymPy. Also, plot the points (c, f(c)) and (c, pn(c)) on the appropriate graphs.

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 pack- age (https://docs.sympy.org/latest/index.html), and the SciPy package (https://docs.scipy.org/doc/). Various other tutorials are also available online.

Output: Student submissions should product the following output:

(1) The function f(x) to be approximated, its domain [a,b], the point c at which it will be approximated, and the desired error bound E

(2) The degree n of the first Taylor polynomial pn(x), centered at the midpoint of [a,b], that is certain to approximate f(x) to within E on all of [a,b]

(3) The Taylor polynomial pn(x) from (2)

(4) The values of f(c) and pn(c) and the error Rn(c) = pn(c) f(c), all rounded to two decimal places

(5) Plots of f(x) and pn(x) on the same axes, along with the points (c, f(c)) and (c, pn(c))

Text output may be produced using the print function and should be easily understandable by human readers (e.g., The function f(x) = sin(e x ) with domain [0.0,4.0] will be approximated at the point c = 3.21 using a Taylor polynomial centered at 2.0. This approximation will be accurate to within an error of E = 0.02.) Plots should be produced using the SymPy function plot and should be color-coded and include a key.

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

Database Fundamentals Study Guide

Authors: Dr. Sergio Pisano

1st Edition

B09K1WW84J, 979-8985115307

More Books

Students also viewed these Databases questions