Problem 2 (While loop) The Taylor series of e can be calculated as following equ
ID: 2073945 • Letter: P
Question
Problem 2 (While loop) The Taylor series of e can be calculated as following equation. 2 eae n 1 23! Considering n is the number of terms. a) Write a program that uses a while loop to calculate ex given the two input arguments x 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) x1 00% e* Problem 3 (for loop) Redo Problem 2 part b) using for loop. Hint: you can use "break;" to terminate the for loop.Explanation / Answer
I have created all the three parts in one code only, you can comment and run each part separately.
------------------
clear all;
clc;
% finds the value of the Taylor series given an x and "n" number of terms
x=input('enter value of x: ');
n=input('enter value of n: ');
% --------------------------------------------------
% using for loop
% for i=1:n
% y(i) = x.^(i)./factorial(i);
% end
% sum_series=1+sum(y)
% break;
% ---------------------------------------------------
% using while loop
y=1;
i=0;
while i<n || i==n
y = y + x.^(i)/factorial(i);
i=i+1;
end
sum_series=y;
display('Sum of series using while loop');
display(sum_series);
% ----------------------------------------------------
% % program to calculate the error to be <0.01% for x=3
% j=1;
% error=10;
% termsum=1;
% while error>0.01
% y(j) = x.^(j)./factorial(j);
% termsum=termsum+y(j);
% error=(abs(termsum-exp(x))/exp(x))*100;
% j=j+1;
% end
% display('number of terms required for error<0.01% for x=3');
% display(j-1);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.