Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

function [ y ] = MyExp( x,n ) % % Function: y = MyExp( x, n ) % Uses the Taylor

ID: 3628327 • Letter: F

Question

function [ y ] = MyExp( x,n )
%
% Function: y = MyExp( x, n )
% Uses the Taylor series expansion for exp(x), with n terms,
% to compute an approximation to exp(x).
%
% Input: x = vector containing values at which the exponential is
% to be computed
% n = number of terms in the Taylor series
%
% Output: y = vector, having same length as x, containing exp(x)
% values
%
term = ones(size(x));
y = ones(size(x));
for k = 1:n
term = term .* (x/n);
y = y + term;
end

end



Something is wrong with this code. When I put in MyExp(1,100), it didn't give me a value close to 2.71282.
Can somebody tell me what's wrong with my code?

Explanation / Answer

The Taylor series expansion of exp(x) is 1 + x/1! + x^2/2! + x^3/3! + .... So you want to have the following code in your function. y = 1; for k = 1:n y = y + (x.^n / factorial(n)); end end