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

For exp(x), separate the integer from the fractional component of the argument.

ID: 3817101 • Letter: F

Question

For exp(x), separate the integer from the fractional component of the argument. For example if the function is called as exp(3.123), separate the 3 from the fractional component 0.123. Tip: you can use x-floor(x) to do this. Recognizing that exp(x+y)=exp(x)*exp(y), compute the integer portion of the argument using a simple loop that performs the multiplication and calculate the fractional component using the series expansion.

Additional Information:

You may use the following code fragments for the sin(x), cos(x), and exp(x) functions. Note, however, that these routines calculate a fixed number of terms, based on the value of N. you will have to modify the routines to continue to calculate terms until the accuracy criterion has been established. Tips: do NOT try to write the complete, fully-functional routine all at once! Better is to work on the individual pieces, test them independently, then assemble together for the final functional version.

% calculate exp(x), given x and N (number of terms)

s= 1; % this is the sum of terms and our final answer

xt= 1; % first term is 1, so let's just put it in to start

fact= 1; % factorial of first term is also 1, so put it in here

for i=1:N % now lets add N terms, as requested by user

xt= xt*x; % calculate numerator of next term (x^i)

fact= fact*i; % calculate denominator of next term (i!)

s=s + xt/fact; % add term to total sum, and go back for more

end

Explanation / Answer

function [s] = mexp(x)

n = floor(x); % get integer part of x
x = x - floor(x); % get the decimal part

p = 1; % initialize the p to 1 to store e^n
for i = 1:n
p = p * exp(1); % p will store the product of n e's i.e e^n
end

epsilon = 0.000005; % for accuracy
ps = 0; % initialize previous term sum to zero
i = 1; % to iterate in while loop
s = 1; % this is the sum of terms and our final answer
xt= 1; % first term is 1, so let's just put it in to start
fact= 1; % factorial of first term is also 1, so put it in here
while abs(s - ps) > epsilon % check if difference of ps and s is less then epsilon
xt= xt*x; % calculate numerator of next term (x^i)
fact= fact*i; % calculate denominator of next term (i!)
ps = s; % store previous sum
s=s + xt/fact; % add term to total sum, and go back for more
i = i + 1; % increment i
end

s = s * p; % s is the final answer that is returned by function

end