Finding products by for loop, MATLAB in Engineering

Finding products by for loop:

an illustration, when 5 is passed to be the value of the input argument n, the function will compute and return 1 + 2 + 3 + 4 + 5, or 15:

>> sum_1_to_n(5)

  ans =

    15

Note that the output was suppressed whenever initializing the sum to 0 and when adding to it throughout the loop.

The other very general application of a for loop is to find a running product. For illustration, rather than of finding the sum of the integers 1 through n, we can find the product of the integers 1 through n. Principally, we want to implement

501_Finding product by for loop1.png

or compute the product 1 * 2 * 3 * 4 *... * n, that is known as the factorial of n, written n!.

 

Posted Date: 10/19/2012 3:35:59 AM | Location : United States







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

Write discussion on Finding products by for loop
Your posts are moderated
Related Questions
Illustration of Image processing: This displays that there are 64 rows, or in another word, 64 colors, in this specific colormap. It also displays that the first five colors a

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

Example of Interpolation and extrapolation: The MATLAB has a function to do this, known as polyfit. The function polyfit finds the coefficients of the polynomial of the partic

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

Help command: The help command is used with the script rectarea, the function readlenwid, and the major function printrectarea. To see the first comment in the subfunction, as

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

Scaling:   change a row by multiplying it by a non-zero scalar sri →  ri For illustration, for the matrix:

Interpolation and extrapolation: In most cases, it is desired to estimate values other than at the sampled data points. For illustration, we may want to estimate what the temp

Evaluating a string: The function eval is used to compute a string as a function. For illustration, below is the string 'plot(x)'is interpreted to be a call to plot the functi

Function strncmp: The function strncmp compares only the first n characters in the strings and ignores the rest. The initial two arguments are strings to compare, and third ar