0

I'm trying to make the code below work by modifying it so that e^-x will work, essentially I'm trying to modify it so that e^-x is 1/e^x I'm really not sure how to do that..Here is my code..

function [result] = eTaylor(x, n) 
% approximate e^x 
% using first n non-zero terms of the Taylor series 
% e^x = 1 + x + x^2/(2!) + x^3/(3!) + x^4/(4!) + ... 
% Input arguments: 
% x = real argument to function e^x 
% n = number of non-zero terms of Taylor series 
result = 0.0; term = 1.0; 
for i = 1:1:n 
result = result + term; 
term = term*x/i; 
end
Megan
  • 1

1 Answers1

4

To get 1/e^x you simply need to compute e^(-x). supply your eTaylor function with -x instead of x and you are done!

 oneOverExp = eTaylor( -x, n ); 

PS,
It is best not to use i as a variable name in Matlab.

Community
  • 1
  • 1
Shai
  • 111,146
  • 38
  • 238
  • 371