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
Displaying expressions: The good-looking function will show such expressions by using exponents; for illustration, >> b = sym('x^2') b = x^2 >> pretty(b)

Indexing into Vectors of structures: Frequently, when the data structure is a vector of structures, it is essential to iterate through the vector in order by various fields. F

IS Functions for Strings: There are many functions for strings, that return logical true or false. The function isletter returns the logical true when the character is a lette

Illustration of initializing the data structure: illustration of initializing the data structure by preallocating is here as shown: >> cyls(3) = struct('code', 'c', 'dimen

Comparing strings: There are few functions which compare strings and return logical true when they are equivalent or logical false when not. The function strcmp compares the s

Application: Menu driven Modular Program Numerous longer, more involved programs which have interaction with the user are menu-driven, that means that the program prints a men

analyzing traffic; determine motion of flow; calculate tracklets; detect abnormalities;

Replacing a string - function strrep: The function strrep finds all the occurrences of a substring within the string, and substitutes them with a new substring. The order of a

Logical scalar values: The MATLAB also has or and and operators which work element wise for the matrices: These operators will compare any of the two vectors or matric

Algorithm for expfn function: The algorithm for expfn function is as shown:  receives the value of x as the input argument.  Prints the value of exp(x).  assigns a