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

Getting Started: Matlab Practice Sessions

This document provides exercises to practice using Matlab. It begins with getting started in Matlab and observing the different windows. Later sections cover exercises with matrices, data types, scripts and functions, programming, and plotting graphs and images. Exercises include defining and manipulating matrices, storing data in different formats, writing functions, debugging scripts, plotting various functions and data, and image processing. The goal is to get hands-on experience with the main capabilities and interfaces of Matlab.

Uploaded by

John Hellen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Getting Started: Matlab Practice Sessions

This document provides exercises to practice using Matlab. It begins with getting started in Matlab and observing the different windows. Later sections cover exercises with matrices, data types, scripts and functions, programming, and plotting graphs and images. Exercises include defining and manipulating matrices, storing data in different formats, writing functions, debugging scripts, plotting various functions and data, and image processing. The goal is to get hands-on experience with the main capabilities and interfaces of Matlab.

Uploaded by

John Hellen
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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.

Practice in the command window:


 Enter the following lines into the command window and observe what happens:

a = 1.234
b= 5.6
c = a*b
D=[abc]
E = [ c ; b; a]
F = D*E
G= E*D

 Investigate each variable on the Workspace Window


 Use the who and whos command to get information about the assigned
variables.
 Note how the variables are stored and how much storage is used.
 Complex Numbers: Issue the commands below and observe output;

a= 3+2*i , b = 4 -7i , c = 4j – 5 , c = a*b


i = 99 , a = 3 + 2*i , a=3 + 2i
clear i , a= 3 + 2*i

2. Exercises on using Matrices

Exercises 2A

 Define the following matrix in matlab:

Introduction to Matlab, D.Savas, M.Griffiths Sheffield University 2012


1 2 3
A   3 4 5
 2 6 6

 Define a row vector X which is set to the second row of matrix A

 Define a column vector Y, which is set to the first column of A


 Define a column vector Z, which is set to the first row of A
 Define a column vector V, which contains the third column of A in reverse order
 Find out X*Y and Y*X
 Find the determinant of A
 Solve the following set of linear equations;

x + 2y + 3z = 4
3x+ 4y + 5z = 14
2x+ 6y + 6z = 20

Exercises 2B.

 Using the RAND function define a 16 by 16 matrix R of random numbers,


elements of which range between 0 and 10.
 Using the PASCAL function set B to the 5 by 5 Pascal triangle
 Find the inverse of matrix B and store it in C
 Using the SUM function find the sum of each column of matrix C
 Define a new matrix D which is the subset of B and made up of rows 2 to 4 and
columns 2 to 4 of the B matrix.

3. Exercises on data types


Store the following table -

(a) As a cell array


(b) As a structure

Introduction to Matlab, D.Savas, M.Griffiths Sheffield University 2012


Alkali metal Standard atomic weight (u) Density (g·cm−3) Colour Index

Lithium 6.941 0.534 [ 0.3 0.4 0.3 ]

Sodium 22.990 0.968 [0.6 0.8 0.7]

Potassium 39.098 0.89 [ 0.2 0.3 0.2]

4. Exercises on using Matlab Scripts & Functions


In the examples directory under the graphics folder you will find two files named
petals.m and petalfun.m.

 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

5. Exercises on Matlab programming

Exercises 5A.

Sin(x) can be represented by the series;

Introduction to Matlab, D.Savas, M.Griffiths Sheffield University 2012


Sin(x) = x – x3/3! + x5/5! – x7/7! + …

 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.

6. Exercises on simple graph plotting


Exercises 6A.

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

While located in the top Matlab_Examples directory.


Read a data file and experiment with various styles of plotting:
load field.dat ;
plot( field(:,1) ) ;
plot( field ) ;
bar(field ); barh(field(:,1) )
bar3(field(:,1));bar3(field(:,1:2)
bar3(field(:,1:2),’group’ )

Introduction to Matlab, D.Savas, M.Griffiths Sheffield University 2012


area(field(:,3) ;
area(field(:,2:3) % note that area curves are accumulative.
pie(field(:,1) ) ; pie( field(1,:) ) % note values are normalised
pie( field(1,:)./11.0 ) % note that if sum of values less than 1.0
% no normalization takes place

Generate a set of 1000 random numbers and draw histogram of them

yy = randn(1000,1); hist(yy ) ; hist(yy, 20 )

7. Exercises on more graphic with Matlab

7.1 Plotting graphs 2D & 3D :


t = 0:0.2:10*pi;
x = sin(t).*exp(-t/30);
y =cos(t).*exp(-t/30);
plot(x,y) ;
plot3( x , y , t ) ;

7.2 Surface Plotting , contours etc.


z = peaks;
pcolor(z) ;
shading interp ;

contourf( z , 32 ) ;
[C, h ] = contour(z,32) ;
clabel ( C, h, ‘manual’ ) ;

surf(z) ;
shading interp ;
caxis ( [ 0 8 ] ) ;
colormapeditor

7.3 Image manipulation

load flujet ; image(X); colormapeditor

load mandrill
image(X)
colormap(map)
axis off % Remove axis ticks and numbers
axis image % Set aspect ratio to obtain square pixels

Introduction to Matlab, D.Savas, M.Griffiths Sheffield University 2012

You might also like