Trying to calculate partial sums of e^10 in Matlab?

后端 未结 2 1015
情歌与酒
情歌与酒 2021-01-25 01:54

I\'ve written the function:

function [sum] = func(a,b)
sum = 0;
for i = 0:b
    sum = sum + ((a.^i)/(factorial(i)));
end

In the console, the co

2条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-25 02:41

    f = @(x,i) (x.^i)./factorial(i)
    r=f(10,0:50); %50 terms in taylor series more than enough for good approx.
    cumsum(r)
    
    1.0e+04 *
    
    0.0001
    0.0011
    0.0061
    0.0228
    ....
    2.202646579479054
    2.202646579480280
    

    Verify

    >> exp(10)
        2.202646579480672e+04
    

提交回复
热议问题