Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 1

Matlab Basics: Exercise 3 (Loops and Conditional Statements)

1.) Write an m-file called find_ave.m that reads in a vector x and outputs
the average of the components of x. Design this so x can be a vector
of any length by setting n=length(x) (since you will need to divide the
sum of those components by the number of components).
2.) Write an m-file that computes the standard deviation of a vector called
std_dev.m. The function should read in a vector x and then call
find_ave.m to get the average. To compute the standard deviation we
need to compute s

1
n 1

( x(i) x
i 1

average

)2

where here n=length(x)

and x(i) is the i-th component of x (In other words the i-th entry in our
list of numbers) and xaverage is the average you would get by calling
find_ave.m.
3.) Write an m-file called increment.m that first set counter=0 and then in
a loop adds one onto counter n times. You can write this so n is
your input. The final value of counter should be n.
4.) Look at the code on page 281 of the text (or page 19 of the MATLAB
tutorial). Code this up and play with it! What does it do?

You might also like