Getting Started: Matlab Practice Sessions
Getting Started: Matlab Practice Sessions
1. Getting Started
Startup Matlab
Observe the following elements of the desktop;
Command Window
Current Folder Window
Command History Window
Workspace Window
Notes: If you startup matlab with –nodesktop option you will only get the command
window. Most of your interaction with Matlab (if not all) can be performed via the
command window.
a = 1.234
b= 5.6
c = a*b
D=[abc]
E = [ c ; b; a]
F = D*E
G= E*D
Exercises 2A
x + 2y + 3z = 4
3x+ 4y + 5z = 14
2x+ 6y + 6z = 20
Exercises 2B.
Make sure to make the directory containing these two files your “Current
Directory” via the “Current Folder” window.
You can open and view their contents by either double-clicking on each or via the
Matlab File-Open dialog. petals.m is contains a “Matlab Script” where as
petalfun.m contains a “Matlab Function” named petalfun.
Run the script and also the function by issuing their names on the Matlab
command window.
Observe what happens to the Workspace variables by issuing a clear command
before running the script and also the function.
Useful hint: Issuing the command pwd on the command window will output the name of
your current directory.
Useful commands to help find functions are what , lookfor, which and type.
Type lookfor zero to get a listing of functions that contain zero in their
description or name.
Next locate one of these functions by using which
Use type to list contents of that function.
Type dirdet = what ; and investigate the contents of the variable named dirdet
Exercises 5A.
Write a matlab function named mysine that will calculate sin(x) to the power 11.
Hint: Use for loops.
Improve your function by putting checks for the range of x and n supplied by the
user.
Modify the function so that the calculations are carried out to any user
specified number of terms (n).
Exercises 5B.
In the examples directory under the root folder you will find a Matlab script named
findrt.
Make sure to make directory named root your “Current Directory” via the
“Current Folder” window or alternatively add this folder to the Matlab Path via
the context-menu of the folder named root in the current-folder window
Study this script and run it under the control of Matlab debugger.
Set break-point(s) within the while loop and start executing the script. When
execution stops at each break-point hover the mouse over the variables in the
debugger window to see their current values. Alternatively these values can also
be viewed in the workspace window.
3 2
Plot the function (0.5-x) + (0.2 –x ) for x ranging from 0.0 to 1.0 in steps of
0.01.
Plot the same curve using the fplot function.
Draw grid lines on the plot. Hint: help grid
Plot the function (0.2-x)/(1.1-x) on the same plot.
Try different colours and line styles for these plots.
Exercises 6B
contourf( z , 32 ) ;
[C, h ] = contour(z,32) ;
clabel ( C, h, ‘manual’ ) ;
surf(z) ;
shading interp ;
caxis ( [ 0 8 ] ) ;
colormapeditor
load mandrill
image(X)
colormap(map)
axis off % Remove axis ticks and numbers
axis image % Set aspect ratio to obtain square pixels