in calculus, any continuous function can be written as an infinite series using
ID: 3790940 • Letter: I
Question
in calculus, any continuous function can be written as an infinite series using the Taylor series formula. The Taylor series for the exponential function is as follows: e^x=1+x+x^2/2! + x^3/3! + x^4/4! + ... + x^n/n! Write a MATLAB program which calculates the exact values for each of the following functions: e, e^3, and sqrt(e) in each of the display formats discussed in class. Then, set the display format to long, and recalculate the exact value of the functions and their Taylor series approximation. The Taylor series approximation should be made for n=6. For each function calculated and approximated, display the percent error.
Explanation / Answer
% as you have not shared which all formats were discussed in class I am sharing code for the next part
% please note function I wrote is generic enough to work beyond 6 element as well.
% good luck
function x = calculteE(pow, n)
% default format
actual_e = exp(pow)
% long format
format long
actual_e = exp(pow)
calculated_e = 1
last_fact = 1
last_pow = 1
for i = 1:n
last_pow = last_pow*pow
last_fact = last_fact*i
calculated_e = calculated_e + (last_pow/last_fact)
end
perc_err = (abs(actual_e - calculated_e)/actual_e) * 100
disp(perc_err)
format short
end
disp("e")
calculteE(1,6)
disp("e^3")
calculteE(3,6)
disp("sqrt(e)")
calculteE(1/2,6)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.