How can i use for loop instead of this?

4 views (last 30 days)
clear all
clc
x= 1;
n=-1;
b=x;
a=1;
i=1
term(1,i)=(a^1)*n/factorial(1)
i=2
term(1,i)=(a^1)*(b^(1))*(n*(n-1))/factorial(2)
i=3
term(1,i)=(a^1)*(b^(2))*(n*(n-1)*(n-2))/factorial(3)
i=4
term(1,i)=(a^1)*(b^(3))*(n*(n-1)*(n-2)*(n-3))/factorial(4)
i=5
term(1,i)=(a^1)*(b^4)*(n*(n-1)*(n-2)*(n-3)*(n-4))/factorial(5)

Accepted Answer

David Hill
David Hill on 3 Apr 2021
Edited: David Hill on 3 Apr 2021
No loop needed. n*(n-1)*(n-2).../factorial() is just -1 or 1 alternating, placed -1 with x raised to power to alternate this sign.
term=a*(-x).^(0:4);

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!