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

Is functions for strings, IS Functions for Strings: There are many fun...

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

Distributed computing on linux cluster, i want to run 4 instances of my mat...

i want to run 4 instances of my matlab code on 4 processor cores. im executing the job from head node. i created a parallel job and assigned number of workers. but i don''t get bac

Function numden, function numden: The function numden will return indi...

function numden: The function numden will return individually the numerator & denominator of a symbolic expression: >> sym(1/3 + 1/2) ans = 5/6 >> [n, d] =

Illustration sorting vectors of structures, Illustration sorting vectors of...

Illustration sorting vectors of structures: This function sorts the structures depend only on the price field. A more common function is shown next, that receives a string whi

Finding a sting - function strfind, Finding a sting - function strfind: ...

Finding a sting - function strfind: The function strfind does necessarily similar thing, except that the order of the arguments does make dissimilarity. The common form is str

Illustration of set operations, Illustration of Set operations: For il...

Illustration of Set operations: For illustration, given the vectors as shown below: >> v1 = 2:6 v1 = 2  3  4  5  6 >> v2 = 1:2:7 v2 = 1  3  5  7

Algorithm for appex subfunction, Algorithm for appex subfunction: The ...

Algorithm for appex subfunction: The algorithm for appex subfunction is as shown:  Receives x & n as the input arguments.  Initializes a variable for running sum of t

Smoothing values, How can I use the weighted moving average formula in matl...

How can I use the weighted moving average formula in matlab to smooth a column data of 404 values?

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

Sound files, Sound Files: The sound signal is an illustration of a con...

Sound Files: The sound signal is an illustration of a continuous signal which is sampled to result in a discrete signal. In this situation, sound waves traveling through the a

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