Summation with FOR loop question

조회 수: 1 (최근 30일)
oliver.s
oliver.s 2019년 2월 14일
편집: Torsten 2019년 2월 14일
I am fairly new to MATLAB. How do you get an answer for this using a FOR loop?

채택된 답변

Mark Sherstan
Mark Sherstan 2019년 2월 14일
편집: Mark Sherstan 2019년 2월 14일
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

추가 답변 (2개)

Geoff Hayes
Geoff Hayes 2019년 2월 14일
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 2019년 2월 14일
편집: Torsten 2019년 2월 14일
format long
fak = 1.0;
s = fak;
for i = 1:1600
fak = fak/i;
s = s + fak;
end
s
exp(1)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by