How can I sum a number of terms as a polynomial?

3 views (last 30 days)
My code performs the individual calculation of each term of my polynomial, which is not exactly a polynomial, however, I need it to output the total sum of all these terms, I have no idea what to do. In my code x is the input, I have been using 1 to facilitate the calculations, and n is the number of terms that must be added to my final result:
while i<=n
ex = (x^i);
fat = factorial(i-1);
soma = ex / fat;
i=i+1;

Accepted Answer

dpb
dpb on 20 Aug 2023
"Dead ahead" straightforward would be
S=0;
for i=1:n
S=S+x^i/factorial(i-1);
end
Alternatively, vectorized could be
i=1:n;
S=sum(x.^i./factorial(i-1));
  14 Comments
Bruno Luong
Bruno Luong on 20 Aug 2023
Edited: Bruno Luong on 20 Aug 2023
(hard to find or easy to find it, depending on your understanding of Taylor)
Torsten
Torsten on 20 Aug 2023
Yes, but you have to know that it's f(x) = x*exp(x) that you are approximating. Usually the explicit form of f is unknown.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!