01.09.2014 Views

Programming with SCILAB (pdf)

Programming with SCILAB (pdf)

Programming with SCILAB (pdf)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Examples of functions that include loops<br />

<strong>SCILAB</strong> provides <strong>with</strong> the while … end and the for … end structures for creating<br />

programming loops. <strong>Programming</strong> loops can be used to calculate summations or<br />

products. For example, to calculate the summation<br />

n<br />

1<br />

S( n)<br />

= ∑ ,<br />

1<br />

2<br />

k = 0 k +<br />

we can put together the following function:<br />

function [S] = sum1(n)<br />

//|----------------------------------------|<br />

//| This function calculates the summation |<br />

//| |<br />

//| n |<br />

//| \--------- |<br />

//| \ 1 |<br />

//| S(n) = \ ______ |<br />

//| / 2 |<br />

//| / k + 1 |<br />

//| /--------- |<br />

//| k = 0 |<br />

//| |<br />

//|----------------------------------------|<br />

S = 0 //Initialize sum to zero<br />

k = 0 //Initialize index to zero<br />

//loop for calculating summation<br />

while k nn = [0:10];<br />

-->m = length(nn);<br />

-->SS = [];<br />

//vector of values of n (nn)<br />

//length of vector nn<br />

//initialize vector SS as an empty matrix<br />

-->for j = 1:m //this loop fills out the vector SS<br />

--> SS = [SS sum1(n(j))]; //<strong>with</strong> values calculated from function<br />

-->end<br />

//sum1(n)<br />

-->plot(nn,SS)<br />

//show results in graphical format<br />

The resulting graph is shown below:<br />

14

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!