我试图通过修改它来使下面的代码工作,以便 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