Problem 2 (While loop) The Taylor series of e* can be calculated as following eq
ID: 2257832 • Letter: P
Question
Problem 2 (While loop) The Taylor series of e* can be calculated as following equation. e' = 1 2 3! Considering n is the number of terms. a) Write a program that uses a while loop to calculate e given the two input arguments» and n. For x-3, find the minimum number n, which gives the error between your equation and the function ex less than 0.01%. Hint: the error is defined as b) your equation-e e* error = x100% Problem 3 (for loop) Redo Problem 2 part b) using for loop. Hint: you can use "break," to terminate the for loop.Explanation / Answer
MATLAB code
close all
clear
clc
x = 3;
true_val = exp(x);
for n=0:1:Inf
if 100*abs((my_exp(x,n) - true_val)/true_val) < 0.01
break;
end
end
fprintf('Minimum value of n = %d ', n);
function y = my_exp(x,n)
y = 0;
for i=0:n
y = y + (x^i)/factorial(i);
end
end
output
Minimum value of n = 11
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.