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
Illustration of gauss-jordan: Here's an illustration of performing such substitutions by using MATLAB >> a = [1 3 0; 2 1 3; 4 2 3] a = 1 3 0 2 1 3 4 2

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

Solving 2 × 2 systems of equations: However this may be easy in a MATLAB, in normal finding solutions to the systems of equations is not. The systems which are 2 × 2 are, thou

Function rmfield - structure: The function rmfield eliminates a field from the structure. It returns a new structure with field eliminated, but does not modify the original st

Illustration of Matrix solutions: For illustration, consider the three equations below with 3unknowns x 1 ,x 2 , and x 3 : We can write this in the form Ax = b here A

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

Illustration of anonymous functions: Dissimilar functions stored in the M-files, when no argument is passed to an anonymous function, the parentheses should still be in the fu

Example of file ploting data: As the other example, a data file called 'compsales.dat' stores the sales figures (in millions) for divisions in a company. Each line in the f

Write a program to examine exponential function: We will write a program to examine the value of e and the exponential function. It will be a menu-driven. The menu options wil

Uses of Function handles: The Function handles can also be generated for functions other than anonymous functions, both built-in & user-defined functions. For illustration, th