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
I dont know how to input different videos on matlab program

deblank function: The deblank function eliminates only trailing blanks from the string, not leading the blanks. The strtrim function will eliminate both the leading and traili

Storing Strings in Cell Arrays: The one good application of a cell array is to store strings of various lengths. As cell arrays can store various types of values in the elemen

Polyhedron - graphics objects: The field polyhedron.vertices is a matrix in which each row presents (x,y,z) points. The field polyhedron.faces defines the faces: for illustrat

Expanding a function: The expand function will multiply out terms, and factor will do the opposite: >> expand((x+2)*(x-1)) ans = x^2 x-2 >> factor(ans)

For Loops which do not use an iterator Variable in the action: In all the illustrations that we seen so far, the value of the loop variable has been used in same way in the ac

Defined a variable in work space: The variables defined in the script will become a part of the workspace: >> clear >> who >> mysummfile    15 >> who

Plotting File data: It is frequently essential to read data from a file and plot it. Generally, this entails knowing the format of the file. For illustration, let us suppose t

Individual structure variable: The individual structure variable for one software package may look like this: The name of the structure variable is a package; it has f

Displaying the cell arrays: There are several techniques of displaying the cell arrays. The celldisp function shows all elements of the cell array:   >> celldisp(cellro