Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 显示迭代并使用e^x的Maclaurin展开式_Matlab_If Statement - Fatal编程技术网

Matlab 显示迭代并使用e^x的Maclaurin展开式

Matlab 显示迭代并使用e^x的Maclaurin展开式,matlab,if-statement,Matlab,If Statement,我一直在尝试使用持久变量和在这里显示每个迭代的想法,但我无法让代码正常工作。我真的非常感谢你的帮助! 函数的目的是迭代计算给定x的f(x)=e^x的Maclaurin展开式的部分和,并在运行后在命令窗口中显示结果 function ex = Exp_series(x) persistent n ee; % declare variables that will be carried to the next call if isempty(n) || isempty(ee)

我一直在尝试使用持久变量和在这里显示每个迭代的想法,但我无法让代码正常工作。我真的非常感谢你的帮助! 函数的目的是迭代计算给定x的f(x)=e^x的Maclaurin展开式的部分和,并在运行后在命令窗口中显示结果

function ex = Exp_series(x) 
   persistent n ee;
   % declare variables that will be carried to the next call
   if isempty(n) || isempty(ee)
      % initiate the persisten value
      n = 0;
      ee = 1;
   else
      n = n+1;
      ee = ee+x^n/factorial(n);
   end
   ex = ee;
end

只需使用for循环。但是你没有问一个具体的问题。看起来没问题。您正确地使用了
persistent
。现在将整个函数放在一个循环中,正如Daniel所说的,必须使用for循环。但是你没有问一个具体的问题。看起来没问题。您正确地使用了
persistent
。现在,按照Daniel所说,将整个函数放入一个循环中