Summation with FOR Loop

63 visualizzazioni (ultimi 30 giorni)
Anistasia Ufo
Anistasia Ufo il 20 Giu 2021
Commentato: Matt J il 6 Feb 2024
  2 Commenti
Steven Lord
Steven Lord il 20 Giu 2021
Modificato: Jan il 22 Giu 2022
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 il 6 Feb 2024
n/(n+1) is not the correct expression for the sequence you have shown.

Accedi per commentare.

Risposte (2)

Udoi
Udoi il 22 Giu 2022
Modificato: Jan il 22 Giu 2022
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 Commento
Jan
Jan il 22 Giu 2022
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

Accedi per commentare.


Sami
Sami il 6 Feb 2024
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);

Categorie

Scopri di più su Mathematics in Help Center e File Exchange

Tag

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by