0% found this document useful (0 votes)
55 views

Matlab Exercises 2: X (0:360) Y1 Sin (X Pi/180) Y2 Cos (X Pi/180) Y3 Tan (X Pi/180)

This document provides instructions for several MATLAB exercises involving plotting functions and 3D surfaces. The first exercise has the user plot sine, cosine, and tangent functions on a 2x2 subplot grid using properties to customize the plots. The second exercise has the user plot sine and cosine curves in one axis and tangent in another, setting various axis properties. The final exercise involves generating a 3D peaks surface and plotting it using functions like pcolor, mesh, and contour3 while customizing aspects like shading, contours, and colorbars.

Uploaded by

User Alcatel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views

Matlab Exercises 2: X (0:360) Y1 Sin (X Pi/180) Y2 Cos (X Pi/180) Y3 Tan (X Pi/180)

This document provides instructions for several MATLAB exercises involving plotting functions and 3D surfaces. The first exercise has the user plot sine, cosine, and tangent functions on a 2x2 subplot grid using properties to customize the plots. The second exercise has the user plot sine and cosine curves in one axis and tangent in another, setting various axis properties. The final exercise involves generating a 3D peaks surface and plotting it using functions like pcolor, mesh, and contour3 while customizing aspects like shading, contours, and colorbars.

Uploaded by

User Alcatel
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

9/6/2020 Matlab Exercises 2

MATLAB EXERCISES 2

Create the arrays:

x=[0:360];
y1 = sin(x*pi/180);
y2 = cos(x*pi/180);
y3 = tan(x*pi/180);

Plot each in a separate subplot in a 2-by-2 grid on the same figure.

Use the property editor to:

Change the x-axis limits to 0 to 360


Change x-axis tick intervals to 30, ie ticks at [0 30 60…360]
Set y-axis limits on plot of tan to -100 to 100
Add axis labels to state which function (sin, cos, tan) is plotted on each axis
Try plotting x against 1./x, and change the x- and y-axis scales from linear to log.

Note – the property editor changes as you select different axes/lines. If you select 'more properties' button a
separate window will open showing ALL the properties available. Experiment with different axis and line properties.

The Figure Palette / Annotations menu allows you to add arrows, lines, text etc. Try adding arrows with labels
pointing out different features on the plots.

While the interactive property editor is useful for quick one-off changes to a single figure, it would be tedious to
adjust a large number of figures like this. It is much better, and often faster, to use the command line to make
changes - this is also essential in order to write programs that create high quality figures. In the tasks below, use the
command line only, saving the handles to objects as you create them and then using these to change the properties
of the objects. You can see ALL the properties of a given object (both names and values) with the command
get(H) where H is the object's handle - if you don't know the property name you need to adjust, this is a good
place to start looking...the other place is the helpdesk's handle graphics property browser.

Create a new figure window and create a first set of axes with

ax1 = subplot(2,1,1)

Plot both the sine (y1) and cosine (y2) series against x on this axis, saving the handles to the lines as you do so.
Now try the following:

Change the line properties for the sine curve to colour red and width 2
Change the cosine curve to a green dashed line with a width of 3
Change the x-axis limits to 0 to 360
Change x-axis tick intervals to 30
Use the xlabel function to add an axis label to the x-axis
Use the legend function to add a legend showing which line is which.
Add a second axis below the first ( subplot(2,1,2) ) and plot the y3 against x in it.
set the x-axis limts and tick intervals to match those in the first axis, and the ylimits to +/- 60 with ticks at intervals of
20.
Add an x-label, and legend for the line.
increase the font size on the axis to 12 point.
set the linewidths of both axes to 2

You should end up with a figure like this. [how]

If you don't save an object's handle when it is created you can always discover it later - if you click on an object it
becomes the current object, you can then use the functions gcf (get current figure), gca (get current axis) and
gco (get current object) to access the handle - note that clicking on a line makes the line the current object, the axis
it is in the current axis, and the figure it's in the current figure.
homepages.see.leeds.ac.uk/~lecimb/matlab/exercises-2.html 1/2
9/6/2020 Matlab Exercises 2
Try adding a few lines to a figure, then select each in turn and removing them with delete(gco)

3D Plots Exercise

Use the peaks function to generate a dataset consisting of x and y coordinates and z a function of x and y:

[x,y,z] = peaks;

Each of x,y, and z is a square matrix.

Try the following plot types:

pcolor(x,y,z) Comparar pcolor() con las funciones


image() e imagesc()
and try the following:
shading flat
shading interp
shsading faceted
mesh(x,y,z)
surf(x,y,z) (the shading command works on this too - a pcolor plot is really a surf plot seen from above)
contour3(x,y,z)
waterfall(x,y,z)

Now create a new figure with a pcolor(x,y,z)plot, and make the additions and adjustments noted below -
save handles as you go...you may need them:

set the shading to flat and add a colorbar


set the range of the color scale to +/- 8
add contours of negative values at integer intervals as black dotted lines
add contours of positive values at interger intervals as solid black lines
add a zero contour as a thick solid black line
set the axis linewidth to 3, and the colorbar linewidth to 2
set the axis font size to 14 and the colorbar fontsize to 12

you should have a figure like this. Now make the following changes:

Change the colormap to 'bone'


change the negative contour lines to solid white
add contour labels with the clabel function and set their color to match the corresponding line color.

You should end up with a figure like this.

[how]

Home | IMB's Homepage

homepages.see.leeds.ac.uk/~lecimb/matlab/exercises-2.html 2/2

You might also like