write a script file USING MATLAB that calculates exp(2) by a Maclaurin series us
ID: 669554 • Letter: W
Question
write a script file USING MATLAB that calculates exp(2) by a Maclaurin series using a while loop.
exp(x) = x^0 / 0! + x^1 / 1! + x^2 / 2! + x^3 / 3! + ... + x^n / n!
The while loop should add each of the series terms. The program should exit the loop when the absolute error is smaller than 0.01.
Error is the exact value (exp(2)), minus the approximate value (the current series sum).
use fprintf within the loop such that, during each round of iteration, the number of iteration, current approximation, and the absolute error are displayed. Also report exact value once at the end of the program using fprintf. You can use built in functions like factorial( ) and abs ( ).
Report exact value once at the end of the program using fprintf
Explanation / Answer
n = 2
%exp(x) = x^0 / 0! + x^1 / 1! + x^2 / 2! + x^3 / 3! + ... + x^n / n!
exactValue = exp(n);
error = 1;
exp2Val = 0;
i = 0;
while(abs(error) >= 0.01)
exp2Val += power(2, i) / factorial(i);
error = (exactValue - exp2Val);
i++;
fprintf(" iteration = %d, Approximate Value : %f error = %f ",i, exp2Val,error);
end
fprintf("Exact Value : %f error = %f ",exp2Val,error);
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.