You are on page 1of 2

1.

PROBLEM STATEMENT:
Write a program which will compute e^x to an accuracy of 0.00001 given

2. INPUT/OUTPUT DESCRIPTION:

 Input
User Input x in e^x

 Output
The number of terms of the equation so that the equation is within 0.00001

3. HAND EXAMPLE:
e^x = 1+x+(x^2/2!)…(x^n/n!)
e^1 = 2.7482818
1+1+(1^2/2!)+ (1^3/3!)+ (1^4/4!)+ (1^5/5!)+ (1^6/6!)+ (1^7/7!)+ (1^8/8!) =2.718279
Difference=2.7482818-2.718279=2.828459x10^-6

4. ALGORITHM DEVELOPMENT
1. Ask the user to input a value for x (e^x)
2. Set factorial = 1; sum=0; difference = 1;
3. Create a for loop which contains the if statement that calculates the factorial, the for loop
calculates the sum of the terms.
4. Print the value within 0.00001
5. Print the actual value.

5. IMPLEMENTATION in MATLAB.
%Cody Donecker October 15, 2009
clear
clc
x=input('Enter a value for x (e^x): ');
factorial = 1;
sum=0;
difference = 1;
disp('Term Value')
for n = 0:22
if n==0
factorial = 1;
elseif n==1
factorial = 1;
else factorial = factorial*n;
end
sum=sum+((x^(n))/factorial);
if exp(x)-sum <=0.00001;
fprintf(' %g %f\n',n,sum)
end
end
disp('First term on list is the value of e^x within 0.00001')
fprintf('%f is the exact value of e^x\n',exp(x));
6. TESTING AND DEBUGGING:

You might also like