Summation with FOR loop question

2 views (last 30 days)
oliver.s
oliver.s on 14 Feb 2019
Edited: Torsten on 14 Feb 2019
I am fairly new to MATLAB. How do you get an answer for this using a FOR loop?

Accepted Answer

Mark Sherstan
Mark Sherstan on 14 Feb 2019
Edited: Mark Sherstan on 14 Feb 2019
Please take the time to fully understand what is going on but this should get you started.
f = 0; % Set the initial conditions
for ii = 0:1600 % Looping from 0 to 1600 as denoted in the summation
f = f + 1/factorial(ii); % Add the previous response to the new response (hence summation)
end
fprintf("The answer is %0.3f\n",f) % Display the answer to 3 decimal places

More Answers (2)

Geoff Hayes
Geoff Hayes on 14 Feb 2019
Oliver - since this is most likely homework and I'm assuming that you have been instructed to use a for loop, see for loop to repeat specified number of times and factorial. An alternative to using a loop is vecorization..see using vectorization for more details.

Torsten
Torsten on 14 Feb 2019
Edited: Torsten on 14 Feb 2019
format long
fak = 1.0;
s = fak;
for i = 1:1600
fak = fak/i;
s = s + fak;
end
s
exp(1)

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!