8.5. Some Illustrative Examples

Some illustrative programs are given in this section to help the user understand the concepts discussed earlier.

Example 8.3.

Write a function subprogram to calculate the sum of the following series:

S = 1 + r + r2 + ... rn

Solution:

The function subprogram is given as follows:

function [s] = seriessum(r,n)   %function definition line
%This function calculates the sum of a series - H1 line
%r is the common rates & n is the no. of terms - Help Text
%Function text body follows
nvector = 0:n;
series = r.^nvector;
s = sum(series)

This function series can be called as follows:

  1. seriessum(2, 4)
    

    The function will be called and executed. Its output will not be stored. However, it is displayed in the Command Window as there ...

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.