Summation with FOR Loop

조회 수: 71 (최근 30일)
Anistasia Ufo
Anistasia Ufo 2021년 6월 20일
댓글: Matt J 2024년 2월 6일
  댓글 수: 2
Steven Lord
Steven Lord 2021년 6월 20일
편집: Jan 2022년 6월 22일
This sounds like a homework assignment. If it is, show us the code you've written to try to solve the problem and ask a specific question about where you're having difficulty and we may be able to provide some guidance.
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the MATLAB Onramp tutorial ( https://www.mathworks.com/support/learn-with-matlab-tutorials.html ) to quickly learn the essentials of MATLAB.
If you aren't sure where to start because you're not familiar with the mathematics you'll need to solve the problem, I recommend asking your professor and/or teaching assistant for help.
By the way, there's no summation in your problem statement. The only place even addition is used is in computing the denominator from the numerator.
Matt J
Matt J 2024년 2월 6일
n/(n+1) is not the correct expression for the sequence you have shown.

댓글을 달려면 로그인하십시오.

답변 (2개)

Udoi
Udoi 2022년 6월 22일
편집: Jan 2022년 6월 22일
We can calculate the sum using a simple for loop in MATLAB.
We take the input value of n from the user.
After taking the input value of n from the user,we initiated the sum variable to be zero.
We can simply iterate over from 2 to n,calculating the terms as depicted by the formula(where each term is of the form (x/x+1)).
In the en we can display the sum.
prompt="Enter the value of n";
n=input(prompt);
sum=0;
for i=2:n
numerator=i;
denominator=i+1;
sum=sum+(numerator/denominator);
end
disp(sum);
If you aren't sure where to start because you're not familiar with how to write MATLAB code, I suggest you start with the MATLAB Onramp tutorial .
  댓글 수: 1
Jan
Jan 2022년 6월 22일
The question concerned a homework and therefore it is usual in this forum not to post a solution, but to offer assistance only. Fortunatly you do not provide a solution also: Your code calculates a sum, while the question concerns a product. The terms are not i/(i+1) also: The numerators are 2,4,6, ..., not 2,3,4,...
Shadowing a built-in function as "sum" by a variable causes unexpected behavior frequently, when the user try to use the function later:
sum = 17
...
sum(1:10) % Fails with an error

댓글을 달려면 로그인하십시오.


Sami
Sami 2024년 2월 6일
prompt="Enter the value of n";
n=input(prompt);
sum=0;
for i=2:n
numerator=i;
denominator=i+1;
sum=sum+(numerator/denominator);
end
disp(sum);

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by