MATLAB EXERCISES 1

There are hints, answers and links to how to do the more complex exercises below - try NOT to look at them unless you really need to - try and figure things out using the helpdesk and experimentation first.


Create an array a

a = [2 4 6 8 10];

and plot the values of a

plot(a)

Try plotting it with a symbol instead of a line - note that the values of a are plotted on the y axis against x-values equal to the element number in the array (or the index to each element). Use the hold command to allow you to add lines to the figure, and then try:

plot(2*a,'g')
plot(2*a + 6,'g--')

Plot 3*a (on y axis) against a on the axis.
Now try creating a second variable, b, equal to the square of a; note that you will need an element-by-element operation [hint], and plotting it as a red line against a (ie a on x-axis, b on y-axis) [how...don't peak until you've tried it!]

Find the result of a + a2 - a3 [answer]

Find the square root of a [answer] [hint]


Create the matrix A

A = [1 2 0; 2 5 -1; 4 10 -1]

Note that variable names are case sensitive, you should now have variables a and A in your workspace - try the who command to check.

What do you expect from the command plot(A)?...did you expect what you actually get? This is an example of matlab's default column-oriented operation.

Find the mean of each column in A [answer], and the mean of each row in A [answer] [hint]


Simple Indexing

In this exercise you may need one or more of the functions:

min(A)- find the minimum value in A,
max(A)- find the maximum value in A
find(A)- returns the numeric indices to TRUE (non-zero) values in A

along with the relational operators <, <=, >, >=, ==.

clear any existing variables from your workspace and create variables

x = [-100:100];
and create a variable y = x3 + 50x2 - 100

Now find the following:

(i) The minimum and maximum values of y [answer]
(ii) The maximum value of y for negative values of x [answer]
(iii) The minimum value of y for positive values of x [answer]
(iv) and the values of x at which the results in (ii) and (iii) are obtained [answer]
(v) the value of x at which y first becomes negative [answer]
(vi) the index into y at which y has values between 0 and 1000 [answer] and corresponding values of x [answer]

hint: create a logical index to select parts of the data meeting the required conditions, then find the value required from this subset of data.


Indexing Exercise 1

Before starting this, clear your workspace of any variables left over from previous exercises (clear). Now create a variable equal to the current year, e.g.

thisyear = 2010

Now create an array

years = [1900:thisyear]

Create a variable B = the year you were born

Create a logical index into the array years for the years since you were born

Create an array, the same size as years, containing your age in each year, and zeros before you were born. Now plot your age against the years as blue circles. Create an index for the years you were an undergraduate, and over-plot your age in red triangles for those years. You should get a plot looking something like this (but probably with rather younger ages!!)

if you're not sure how to plot the different line symbols or colors, use the command 'help plot' to find out.

[how]


Indexing Exercise 2

Create the arrays

x = [0:360];
y = sin(x*pi/180);

Plot y against x, with a blue line where y is positive, and a red line where y is negative.

Now create another variable z equal to the cosine of x (N.B. you will need to convert x from degrees to radians as in the definition of y above).

Add a plot of z against x as a cyan line...and overplot circles where both sine and cosine are negative. You should end up with a figure like this.

[how]


Indexing Exercise 3

Create the row vector

x = [1:10]

and the column vector

y = x'

and the 10x10 matrix

z = y*x

Now find the following:

(i) The number of elements in z with values greater than 10 but less than 90 [answer] (hint : you will need to use the sum function) [how]
(ii) The mean of all the values in z which do not lie along an edge of the matrix [answer] [how]


Home | IMB's Homepage