0

我试图通过修改它来使下面的代码工作,以便 e^-x 可以工作,本质上我正在尝试修改它,以便 e^-x 是 1/e^x 我真的不知道如何这样做..这是我的代码..

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
4

1 回答 1

4

要得到1/e^x你只需要计算e^(-x). 用代替提供你的eTaylor功能,你就完成了!-xx

 oneOverExp = eTaylor( -x, n ); 

PS,
最好不要i在 Matlab 中用作变量名

于 2014-09-30T11:53:05.500 回答