How to sum a series of numbers?

조회 수: 12 (최근 30일)
AStar
AStar 2019년 12월 7일
댓글: Matt J 2019년 12월 7일
How would I go about creating a program that sums a series of fractions? The program will prompt the user to enter the number of terms in the summation.
For example if i wanted to sum 2/3+4/5 +6/7+8/9+... and the user said they want to use 3 terms in the summation for example.
n=input('Enter the number of terms in the summation: ');
for i=1:n
numerator=[2:2:(i+2)]...
Im thinking something along these lines, I know Id need to create separate vectors for the numerator and denominator and then sum them. If someone could please write me a sample code to explain how this would be done it would be greatly appreciated. Thanks.

답변 (1개)

Matt J
Matt J 2019년 12월 7일
sum(numerator./denominator)
  댓글 수: 2
AStar
AStar 2019년 12월 7일
I know that, I am asking how to setup the for loops and what the numerator and denominator vectors should actually contain.
Matt J
Matt J 2019년 12월 7일
If I told you that, I would deprive you of the opportunity to do any work. However, I will say that I don't think any of this requires any for loops. The numerator is simply a vector of even numbers
numerator=[2,4,6,8,...]
and the denominator is simply going to be
denominator = numerator+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