Академический Документы
Профессиональный Документы
Культура Документы
EG168
P R O B L E M S O LV I N G I N M AT L A B
CONTENTS
Getting started...................................................................................................... 2
Logging on.......................................................................................................... 3
Starting Matlab................................................................................................... 4
Help.................................................................................................................... 4
Recommended Text............................................................................................ 5
Assessment........................................................................................................ 5
1. Basic calculations and commands...................................................................6
1.1 Mathematical Operations..........................................................................6
1.2 Variables and Functions............................................................................ 7
1.3 Vectors...................................................................................................... 8
1.3.1 Using Vector Indices............................................................................9
1.3.2 Operations on Vectors.........................................................................9
1.4 Matrices.................................................................................................. 11
1.4.1 Using Matrix Indices..........................................................................11
1.5 Troubleshooting and debugging..............................................................12
Version 2014A EG168 Problem Solving in Matlab 03/05/2017
GETTING STARTED
Matlab is a high level scripting language. The best way to learn any programming
language is to try it for yourself
. Each example builds on the previous one, therefore it is ESSENTIAL that you
understand each example and exercise in sequence. There are examples and
exercises throughout the workbook which should be completed in order and
WEEKLY TESTS ON BLACKBOARD for the first FOUR WEEKS.
Matlab is an incredibly useful tool, used widely in industry and academia. It can
perform the simplest calculations, process and plot large quantities of data
quickly or even be used to run complex control programs for large infrastructure
such as power stations. Matlab is great for for applications where large datasets
need repetitive calculations to be carried out.
Advantages of Matlab:
LOGGING ON
The first time you log in, your password is your student number followed by
your date of birth.
STARTING MATLAB
Open Matlab. You will see the GUI (graphical user interface) as below:
Change this to the H:\ drive where you can save your data.
HELP
You will not find all the answers in this workbook, be prepared to do your own
research to solve problems. If you get stuck, try the following:
RECOMMENDED TEXT
ASSESSMENT
A short group report submission via Turnitin on the Bloodhound supersonic car
(40%). The report will show the presentation of data processed using Matlab.
Version 2014A EG168 Problem Solving in Matlab 03/05/2017
At the end of the session complete Matlab Test 1 (5% of module mark). You will
need to have completed all the examples and exercises in order to complete the
Test.
1.1MATHEMATICAL OPERATIONS
At the simplest level, Matlab can be used as a calculation tool by using text
entered into the command window. The correct operators need to be used and
the BODMAS rule applies.
Exercise 1.1
a) Convert the following equations into the Matlab syntax and enter into the
command window. Press Enter to execute the command.
b) Type or copy & paste the correct Matlab code into the 2 nd column
c) Write the correct answer in the 3rd column (select answers below the
table).
10 (2+10) 210
2
23 2 +2
+113
17 17
-8.3, 11.3
Example
sqrt(9)
round(3.4)
See the end of the chapter for more examples of inbuilt functions.
variablename = expression
Example
Type the following into the Matlab Command Window, pressing Enter after each
line to execute each command separately
a=1
A=2
c=3
Version 2014A EG168 Problem Solving in Matlab 03/05/2017
Type the following into the Matlab Command Window, pressing Enter to execute
the command:
Exercise 1.2
Pre-stored variables
There are several variables and matrices pre-stored in Matlab. Type the following
into Matlab and write the value in the right hand column
Variable Value
pi
i
j
eps
realmin
realmax
inf
1.3VECTORS
This allows more information to be stored. In the table below, data for classic
concept cars is stored:
Example
Copy and paste the following into Matlab line by line (press enter after each line):
Vector1 = [1:10]
Vector2 = [2:2:20]
Speed_mph(1) = 186
Speed_mph(2) = 192
Speed_mph(3) = 268
Speed_mph(4) = 202
Time0to60_s = [4;4.6;2.4;3.9]
These are all ways of creating vectors. You will use the Power_bhp, Speed_mph
and Time0to60_s vectors later in the chapter
The last three in the above example vectors from Table 1a. Power_bhp and
Speed_mph are row vectors and Time0to60_s is a column vector. They will now
have appeared in the Workspace.
The position of a number within a vector is known as the index (plural: indices)
or sometimes the element.
Example
For this example you must have completed the examples in the previous section.
Type:
Power_bhp(3)
Type:
Time0to60_s(1)
This returns the value stored in index 1 (position 1) of the Time0-60_s vector.
Operation Symbol
Sum of vector sum( )
contents
Size of vector size( )
Average (mean) mean( )
of vector
contents
Maximum value max( )
Minimum value min( )
Example
sum(Power_bhp)
Example
Data1=[1 2 3 4]
Data2=2+Data1
Exercise 1.3
Below Table 1 is repeated. Add the values for the sum and mean for the
Power_bhp, Speed_mph and Time0to60_s columns
Convert the Power_bhp and Speed_mph vectors from Imperial to Metric units.
Create two new vectors, Power_kW and Speed_kmh using the calculations above
and fill in the last two columns of Table 1b.
You can check your answers by using the same data in a calculator or in Excel.
Martin
Bulldog
(1980)
3 Bugatti 1183 268 2.4
Veron
Supersport
(2010)
4 Ferrari 500 202 3.9
Aurea
(2003)
Sum
Mea
n
1.4MATRICES
Example
Matrix1=[1 3 5 7 9 ; 2 4 6 8 10 ; 11 12 13 14 15]
The commands above produce identical 3 x 5 matrices. Notice that once again,
spaces or commas create rows and semi-colons create columns.
As with vectors, matrix indices give the position of a number within the matrix,
but now a row and a column index must be given. The row index is always given
first.
Example
Typing Matrix1(1,3) will return the item in the 1st row and 3rd column = 5
Typing Matrix1(3,5) will return the item in the 3rd row and 5th column = 15
Exercise 1.4
Results
a Matrix1(:,3)
d Matrix1(1:2,3:5)
f Sum(Matrix1(2,:))
g Mean(Matrix1(3,1:2))
h Matrix1(2:3,2:end)
Version 2014A EG168 Problem Solving in Matlab 03/05/2017
Try typing the commands in the left column of the table above into the Command
Window. Match with the results on the right side. Compare the original Matrix1
with the results from the index combinations. What is the effect of using the
semicolon? What is the effect of using end?
Exercise 1.5
Matrices can be used to store different types of linked data. Create a matrix with
the original data about the Concept Cars:
ConceptCarMatrix=[295,186,4;650,192,4.6;1183,268,2.4;500,202,3.9]
Check the values are correct by comparing with the table above. This is now
stored as a 4 x 3 matrix (4 row by 3 column). Check by typing:
size(ConceptCarMatrix)
Each row is a different car, each column is a different data type. Following the
same logic as the previous exercise, what are the commands to extract the
following information from ConceptCarMatrix?
You can check your answers by comparing with the original data table.
Comon pitfalls:
Matlab has a very helpful error check tool. Red text in the command window
indicates a problem, and this will usually tell you what and where the problem is,
e.g.:
Matlab detects
incorrect
operator *
Red line shows
location of
incorrect
operator
I define
MatrixA with 6
values This instruction
asks for the 7th
value (it doesnt
In the problem above, MatrixA has six values. When it is interrogated for the
seventh index value, it returns the error message Index exceeds matrix
dimensions.
Blackboard Test 1
Ensure you have completed all the examples and exercises in the chapter.
You have Matlab open. You will need to have Matlab open before taking
the test. Once you open the test, the 20 minutes begins.
Version 2014A EG168 Problem Solving in Matlab 03/05/2017
Summary Table 1