Finding sums by for loop, MATLAB in Engineering

Assignment Help:

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

 


Related Discussions:- Finding sums by for loop

Signals, Q.1: Consider the transmission of a sinusoid x(t) = cos(2f0t) thr...

Q.1: Consider the transmission of a sinusoid x(t) = cos(2f0t) through a channel a ected by multipath and Doppler. Let there be two paths, and assume the sinusoid is being sent fro

Function iscellstr - string, Function iscellstr - string function: The...

Function iscellstr - string function: The function iscellstr will return the logical true when a cell array is a cell array of all the strings, or logical false if not. >>

Replacing, Replacing, Finding, and separating strings: There are numer...

Replacing, Finding, and separating strings: There are numerous functions which find and replace the strings, or parts of strings, within the other strings and functions which

Program to counting in a while loop, Program to Counting in a while loop: ...

Program to Counting in a while loop: The script initializes variable counter to 0. Then, in the while loop action, each and every time the user successfully enter a number, th

Storing strings in cell arrays, Storing Strings in Cell Arrays: The on...

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

True color matrice - image processing, True color matrice: The true co...

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

Uses of function handles, Uses of Function handles: The Function handl...

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

Solving 2 × 2 systems of equations, Solving 2 × 2 systems of equations: ...

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

Displaying expressions, Displaying expressions: The good-looking funct...

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

Derivatives and plot, Use polyval to evaluate the derivative at xder. This...

Use polyval to evaluate the derivative at xder. This will be the % slope of the tangent line, "a" (general form of a line: y = ax + b). % 4. Calculate the intercept, b, of t

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd