Question: Here is an example of MATLAB code that takes as input a positive parameter k and a desired error threshold err , and returns the
Here is an example of MATLAB code that takes as input a positive parameter k and a desired error threshold err, and returns the first value n so that the nth-order Taylor Polynomial for f(x)=e^(kx) about x=0 is within err of e^(kx) for all x in the interval [-1,1]:
function n = taylor_polynomial_order(k, err)
% This function takes as input a positive parameter k and a desired error
% threshold err, and returns the first value n so that the nth-order
% Taylor Polynomial for f(x) = e^(kx) about x = 0 is within err of e^(kx)
% for all x in the interval [-1,1].
k=1
err=0.01
% Initialize n to 0
n = 0;
% Calculate the first n terms of the Taylor series for e^(kx) about x = 0
taylor_series = 1;
% Initialize the error to be greater than the desired threshold
error = err + 1;
while error > err
% Calculate the next term in the Taylor series
n = n + 1;
term = (k^n)/factorial(n);
taylor_series = taylor_series + term;
% Calculate the error
error = max(abs(taylor_series - exp(k*(-1:0.01:1))));
end
% Return the value of n
n
end
the problem I am having with this is that for example when testing this function by calling it with specific values for k and err, such as taylor_polynomial_order(1, 0.01). This should return the value 13, which is the first value of n that satisfies the condition in the problem statement.
I am not getting thirteen and when I change k and err and I keep getting these errors should please help fix this so that it runs and doesn't give me issues.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
