3.11. Functions with Array Inputs

If the MATLAB functions receive an array of input values, then the function is performed for each element of the array separately that is on element-by-element basis.

For example, let x = [0, pi/2, pi]

The statements y= sin(x) and z=cos(x) will compute sine and cosine for each element of x to form the resultant vectors y and z. The following commands are used:

x = [0, pi/2, pi];
y = sin(x)
z = cos(x)

The result obtained is as

y =
   0   1.0000    0.0000

which gives the values of sin(0), sin(pi/2) and sin(pi), respectively, and

z =
   1.0000   0.0000    -1.0000

which gives the values of cos(0), cos(pi/2) and cos(pi), respectively.

Example 3.23.

Obtain the sum of the following series:

S = 1 + r + r2 + r3 + ... + rnfor r

Get MATLAB® and Its Applications in Engineering: [Based on MATLAB 7.5 (R2007b)] now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.