Can anyone help me solve this Matlab (while loop) series summation problem?

Question: Use a while-end loop to determine how many terms are required for the series summation given below to exceed 10,000. y=Σ9*k^3-7*k^2+5*k+3
My code:
clear; clc;
nsum=0;
n=0;
k=0;
while (k<10000)
k=sum(9*k^3-7*k^2+5*k+3)
n=n+1
end
fprintf('The number of terms required to exceed 10,000 is %g and the sum is %g',n,k)
What I am stuck with is if I should do k as the sum and plug back the new k for the new while loop trial or make k=k+(1 or some number) and change to (nsum<10000), but if I do the latter then the total number of terms will be different each time I change to like k+0.5

1 commentaire

Note that
x = sum(expression_that_result_in_a_scalar)
is exactly the same as
x = expression_that_result_in_a_scalar
That is there is nothing for sum to sum since there's just one number.

Connectez-vous pour commenter.

Réponses (1)

James Tursa
James Tursa le 22 Fév 2018
Modifié(e) : James Tursa le 23 Fév 2018
To make things easier on yourself, why not use the exact same variable names as the question? E.g., use k as the indexing variable and use y to hold the sum. So an outline would be something like this:
y = 0; % Initialize sum
k = 0; % Initialize the indexing variable
while( __________ )
k = k + 1;
y = y + __________;
end
You will need to fill in the blanks above with appropriate code.

Catégories

Produits

Question posée :

+ +
le 22 Fév 2018

Modifié(e) :

le 23 Fév 2018

Community Treasure Hunt

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

Start Hunting!

Translated by