With MATLAB Use a while loop to determine how many terms in

With MATLAB, Use a while loop to determine how many terms in the series 2k(k+1), k 1,2,3,......, are required for the sum of the terms to exceed 5000. What is the sum for this number of terms? Use fprintf to display the number of terms with the corresponding sum.

Solution

Matlab code:-


% intialize the first term to zero
s=[0];

% Loop through 1 to 100 to store the calculated values in s
for i=1:100
s(i+1)=2*(k*(k+1));
end

% Initialize the sum to zero
summ=0;

%initialize the k to 1
k=1;

%initialize the number of terms to zero
terms=0;

%While loop to terminate when the sum of terms is more than 5000 at this term
while summ<=5000
summ=summ+s(k);
terms=terms+1;
k=k+1;
end
%Print the number of terms and the sum calculated greater than 5000 for this series.
fprintf(\'\ The number of terms is %d.\ and the sum for this terms is %d.\ \',terms,summ);

Run the above program in the Matlab Command window.

Or you can paste the code in the editor and Save the file in the .m format and run it. You will get the output in the COmmand window

With MATLAB, Use a while loop to determine how many terms in the series 2k(k+1), k 1,2,3,......, are required for the sum of the terms to exceed 5000. What is t

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site