Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Need help with Part 2 of the problem. Heres my code so far. ignore first code. its wrong. please start from part 1 In numerical

image text in transcribed

image text in transcribed

image text in transcribed

Need help with Part 2 of the problem. Heres my code so far.

ignore first code. its wrong. please start from part 1
In numerical methods, one source of error occurs when we use an approximation for a mathematical expression that would otherwise be too costly to compute in terms of run-time or memory resources. One routine example is the approximation of infinite series by a finite series that mostly captures the important behavior of the infinite series. In this problem you will implement an approximation to the exp(x) as represented by the following infinite series, Your approximation will be a truncated finite series with N + 1 terms, expl,n) = n=0 Part 1 For the first part of this problem, you are given a random real number x and will investigate how well a finite series expansion for exp(x) approximates the infinite series. Compute exp(x) using a finite series approximation with N E (0,9] C N (i.e. N is an integer). Save the 10 floating point values from your approximation in a numpy array named exp_approx. exp_approx should be of shape (10,) and should be ordered with increasing N (i.e. the first entry of exp_approx should correspond to exp(x, N) when N = 0 and the last entry when N = 9). Part 2 For the second part of this problem, you are asked to find an integer value Ncutoff (0,9 such that the finite series is guaranteed to have a relative error less than some desired amount. The relative error is with respect to the true exp(x) value that would result from an infinite series. Ncutoff should be the smallest integer that satisfies the desired relative error constraint. First, save the absolute and relative errors for each value of N in abs_error and rel_error. Given a desired decimal relative error, desired_rel_error, find the smallest N that guarantees the relative error is below desired_rel_error. Assign this N to a variable N_cutoff. Plot the relative error vs. N (error on the y-axis and N on the x-axis). Make the y-axis log scale. You should have 10 values. Provide appropriate formatting (e.g. labels, title, markers, et cetera). Input/Output The setup code provides the following variables: Name Type Description float random real number where the function will be evaluated float the error limit in computing the value of N desired_rel_error Your code snippet should define the following variables: Name exp_approx abs_error rel_error N_cutoff plot Type 1-D Numpy Array 1-D Numpy Array 1-D Numpy Array int subplot Description array giving your estimates for exp(x) array giving the absolute error for using the taylor approximation array giving the relative error for using the taylor approximation the smallest N that satisfies the desired relative error constraint semilogy plot of the relative error vs. N. Your code snippet mote also generate the following plot: Plot of relative error vs. N, log-scale on the y-axis (plt. semilogy). Provide appropriate axes labels and a title. Save your plot to a variable plot. user_code.py 1 2 import numpy as np import matplotlib.pyplot as plt 4 x=float(input("Enter x: ")); exp_approx=[1]; 6 t=1; N=9; 8 - for i in range(1,N): t=t*x/i; 10 exp_approx.append(exp_approx[i-1]+t); 11 print(exp_approx); 12 14 #exp_approx = np.zeros(10) #abs_error = np.zeros(10) #rel_error = np.zeros(10) #N_cutoff = 0 16 17 18 19 # Save plot for grading #plot = plt.gca() Restore original file In numerical methods, one source of error occurs when we use an approximation for a mathematical expression that would otherwise be too costly to compute in terms of run-time or memory resources. One routine example is the approximation of infinite series by a finite series that mostly captures the important behavior of the infinite series. In this problem you will implement an approximation to the exp(x) as represented by the following infinite series, Your approximation will be a truncated finite series with N + 1 terms, expl,n) = n=0 Part 1 For the first part of this problem, you are given a random real number x and will investigate how well a finite series expansion for exp(x) approximates the infinite series. Compute exp(x) using a finite series approximation with N E (0,9] C N (i.e. N is an integer). Save the 10 floating point values from your approximation in a numpy array named exp_approx. exp_approx should be of shape (10,) and should be ordered with increasing N (i.e. the first entry of exp_approx should correspond to exp(x, N) when N = 0 and the last entry when N = 9). Part 2 For the second part of this problem, you are asked to find an integer value Ncutoff (0,9 such that the finite series is guaranteed to have a relative error less than some desired amount. The relative error is with respect to the true exp(x) value that would result from an infinite series. Ncutoff should be the smallest integer that satisfies the desired relative error constraint. First, save the absolute and relative errors for each value of N in abs_error and rel_error. Given a desired decimal relative error, desired_rel_error, find the smallest N that guarantees the relative error is below desired_rel_error. Assign this N to a variable N_cutoff. Plot the relative error vs. N (error on the y-axis and N on the x-axis). Make the y-axis log scale. You should have 10 values. Provide appropriate formatting (e.g. labels, title, markers, et cetera). Input/Output The setup code provides the following variables: Name Type Description float random real number where the function will be evaluated float the error limit in computing the value of N desired_rel_error Your code snippet should define the following variables: Name exp_approx abs_error rel_error N_cutoff plot Type 1-D Numpy Array 1-D Numpy Array 1-D Numpy Array int subplot Description array giving your estimates for exp(x) array giving the absolute error for using the taylor approximation array giving the relative error for using the taylor approximation the smallest N that satisfies the desired relative error constraint semilogy plot of the relative error vs. N. Your code snippet mote also generate the following plot: Plot of relative error vs. N, log-scale on the y-axis (plt. semilogy). Provide appropriate axes labels and a title. Save your plot to a variable plot. user_code.py 1 2 import numpy as np import matplotlib.pyplot as plt 4 x=float(input("Enter x: ")); exp_approx=[1]; 6 t=1; N=9; 8 - for i in range(1,N): t=t*x/i; 10 exp_approx.append(exp_approx[i-1]+t); 11 print(exp_approx); 12 14 #exp_approx = np.zeros(10) #abs_error = np.zeros(10) #rel_error = np.zeros(10) #N_cutoff = 0 16 17 18 19 # Save plot for grading #plot = plt.gca() Restore original file

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

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

3rd Edition

0128012757, 978-0128012758

More Books

Students also viewed these Databases questions

Question

Prepare for a successful job interview.

Answered: 1 week ago

Question

Describe barriers to effective listening.

Answered: 1 week ago

Question

List the guidelines for effective listening.

Answered: 1 week ago