Question: How can I fix the code? When I run it is says SyntaxError: multiple statements found while compiling a single statement. The code is below

How can I fix the code?

When I run it is says

SyntaxError: multiple statements found while compiling a single statement.

The code is below the question. 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.

import sympy

from sympy import symbols

import numpy as np

from scipy.optimize import minimize_scalar

import matplotlib.pyplot as plt

# Read the entries from the file assignment1.txt and convert them to necessary data types

with open("assignment1.txt") as file:

f_expression = file.readline().strip()

a, b = map(float, file.readline().strip()[1:-1].split(","))

c = float(file.readline().strip())

E = float(file.readline().strip())

# Convert the expression string to a SymPy expression

x = symbols('x')

f = sympy.sympify(f_expression)

# Find the degree of the Taylor polynomial

midpoint = (a + b) / 2

def abs_max_func(k):

f_k = f.diff(x, k)

return -1 * abs(f_k.evalf(subs={x: midpoint}))

result = minimize_scalar(abs_max_func)

n = -1 * result.fun

# Compute the Taylor polynomial

taylor_polynomial = sympy.series(f, x, midpoint, n).removeO()

# Approximate f(x) at the given point c and find the error in this approximation

f_c = float(f.evalf(subs={x: c}))

pn_c = float(taylor_polynomial.evalf(subs={x: c}))

Rn_c = pn_c - f_c

# Plot f(x) and the Taylor polynomial pn(x) along with the points (c, f(c)) and (c, pn(c))

f_plot = sympy.plot(f, show=False, label="f(x)")

pn_plot = sympy.plot(taylor_polynomial, show=False, label="pn(x)")

f_plot.extend(pn_plot)

f_plot.title = "f(x) and pn(x)"

f_plot.xlim = [a, b]

f_plot.scatter_points = [[(c, f_c), (c, pn_c)]]

f_plot.show()

# Output the results

print("The function f(x) =", f, "with domain [", a, ",", b, "] will be approximated at the point c =", c, "using a Taylor polynomial centered at", midpoint, ".")

print("This approximation will be accurate to within an error of E =", E, ".")

print("The degree of the first Taylor polynomial pn(x) is", int(n), ".")

print("The Taylor polynomial pn(x) is", taylor_polynomial, ".")

print("The value of f(c) is", round(f_c, 2), "and the value of pn(c) is", round(pn_c, 2), ".")

print("The error Rn(c) = pn(c) - f(c) is", round(Rn_c),".")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!