MECE 2350 Numerical Methods Lab 7 pe au M-file that calculates e2 by the Maclaur
ID: 3199302 • Letter: M
Question
MECE 2350 Numerical Methods Lab 7 pe au M-file that calculates e2 by the Maclaurin series using a while loop. The while loop should increase the number of polynomials per iteration. The program should exit the loop when the absolute error is smaller than 0.01, abs defined below. The error is the exact value minus the approximate value absolute error- leract-approzimation Be sure to use fprintf within the loop in order to display the iteration number, current approximation, and the absoluté error after each iteration. Also, display the exact value at the end of the program using fprintf 2. If you integrate cos(t) from t0tot, for what value of b does the integral become 0.5? Use a while loop to stop the iteration when the integral is greater than or equal to 0.5. Generate an output on the command window that reports the upper limit b and the corresponding integral. F5 F6 F7 F8 F9Explanation / Answer
%%%%% SCRIPT to calculate e^2
x=2;
%The exact value is
exact=exp(x);
%We set the approximation equal to 0
approx=0;
abserror=abs(exact-approx);
%we set n=0
n=0;
fprintf('Iteration Approximation Abs. Error ')
fprintf('%d %f %f ',n,approx,abserror)
while abserror>0.01
approx=approx+(x^n)/factorial(n);
n=n+1;
abserror=abs(exact-approx);
fprintf('%d %f %f ',n,approx,abserror)
end
fprintf('The exact value is %f ',exact)
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.