Finding sums by for loop, MATLAB in Engineering

Finding sums and products:

A very general application of a for loop is to compute sums and products. For illustration, rather than of just printing the integers 1 through 5, we can compute the sum of the integers 1 through 5 (or, in common, 1 through n, here n is any positive integer). Principally, we want to implement

2479_Finding sums by for loop.png

or compute the sum 1 + 2 + 3 + ... + n.

In order to do this, we require to add each value to a running sum. The running sum is a sum which will keep changing; we keep adding to it. At First the sum has to be initialized to 0, then in this situation it will be 1 (0 + 1), then 3 (0 + 1 + 2), then 6 (0 + 1 + 2 + 3), and so on.

In a function to compute the sum, we require a loop or iterator variable i, and also a variable to store the running sum. In this situation we will use the output argument runsum as the running sum. Each time through the loop, the later value of i is added to the value of the runsum. This function will return the end outcome that is the sum of all the integers from 1 to the input argument n stored in the output argument runsum.

1244_Finding sums by for loop1.png

 

Posted Date: 10/19/2012 3:33:47 AM | Location : United States







Related Discussions:- Finding sums by for loop, Assignment Help, Ask Question on Finding sums by for loop, Get Answer, Expert's Help, Finding sums by for loop Discussions

Write discussion on Finding sums by for loop
Your posts are moderated
Related Questions
Execute a exponential function program: Running the script will take up the menu as shown in the figure: Then, what happens will totally depend on which button(s) the

str2num function - String: The function str2num does the opposite; it takes the string in which a number is stored and converts it to the type double: >> num = str2num('123.

Calling of Function polyval: The curve does not appear very smooth on this plot, but that is as there are only five points in the x vector. To estimate the temperature

Variable Scope: The scope of any of variable is the workspace in which it is valid. The workspace generated in the Command Window is known as the base workspace. As we know

I have a 400x2 vel.dat and 20x2 xy.dat file, how can i plot a streamslice graph on matlab with this two files.

Algorithm for the function e: The algorithm for the function eoption is as shown: Use the menu function to show the 4 choices. Error-check (an error would take place

True color matrice: The true color matrices are the other way to represent images. The true color matrices are 3-dimensional matrices. The first two coordinates are the coordi

Function used in binary search: The function below implements this binary search algorithm. It receives two arguments: the sorted vector and a key (on the other hand, the func

Built-in colormaps: The MATLAB has numerous built-in colormaps which are named; the reference page on colormap shows them. Calling the function colormap without passing any ar

Creating the structure Variables: Creating a structure variable can be accomplished by simply storing the values in fields by using assignment statements, or by using the stru