Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Zongqiang Liao
Research Computing Group
UNC-Chapel Hill
Purpose
2
Course agenda
Introduction
Getting started
Mathematical functions
Matrix generation
Reading and writing data files
Basic plotting
Basic programming
3
Introduction
MATLAB desktop
5
Getting started
>> pi
ans =
3.1416
More examples:
>> sin(pi/4)
>> 2^(log(4))
>> sqrt(9)
6
Getting started
>> x=5
x=
5
>> y = 'Bob'
y=
Bob
7
Getting started
Suppressing output
You can suppress the numerical output by putting a
semicolon (;) at the end of the line
>> t=pi/3
>> u=sin(t)/cos(t);
>> v= u- tan(t);
Case sensitive
Example: “time” and “Time” are different variables
>> time=61;
>> Time=61;
8
Getting started
>> clear t
or
>> clear all
9
Getting started
Miscellaneous commands
To clear the Command Window
>> clc
To continue a line
…
10
Getting started
Getting help
Use help to request info on a specific function
>> help sqrt
11
Mathematical functions
Special functions
>> help specfun
Such as
sin(x), cos(x), tan(x), ex, ln(x)
12
Mathematical functions
Example 1
Calculate z=e-asin(x)+10 y for a=5, x=2, y=8
Example 2
log(142), log10(142)
13
Matrix generation
14
Matrix generation
Entering a matrix
15
Matrix generation
>> A=[1 2 3; 4 5 6; 7 8 9]
>> A=
1 2 3
4 5 6
7 8 9
16
Matrix generation
Matrix indexing
>>A(1,3)
>>ans =
3
17
Matrix generation
Examples:
A(2:3, 2:3)
A(2, :)
A(2:end, :)
18
Matrix generation
Transposing a matrix
The transposing operation is a single quote (’)
>>A’
Concatenating matrices
Matrices can be made up of sub-matrices
>>B= [A 10*A; -A [1 0 0; 0 1 0; 0 0 1]]
19
Matrix generation
>>x=0:0.1:5;
20
Matrix generation
Save command
• Example 1, save all variables in the workspace into a binary
file:
>> x = [1 3 -4];
>> y = [2 -1 7];
>> z = [3 2 3];
>> save Filename.mat
22
Reading and writing data files
Save command
or
23
Reading and writing data files
load command
The data can be read back with the load command
>> load Filename.mat
24
Reading and writing data files
25
Reading and writing data files
26
Reading and writing data files
27
Basic plotting
28
Basic plotting
29
Basic plotting
30
Basic plotting
>> x = 0:pi/100:2*pi;
>> y1 = 2*cos(x);
>> y2 = cos(x);
>> y3 = 0.5*cos(x);
>> plot(x,y1,‘--’,x,y2,‘-’,x,y3,‘:’)
>> xlabel(‘0 \leq x \leq 2\pi’)
>> ylabel(‘Cosine functions’)
>> legend(‘2*cos(x)’,‘cos(x)’,‘0.5*cos(x)’)
>> title(‘Typical example of multiple plots’)
31
Basic plotting
32
Basic plotting
Subplot
33
Basic plotting
Subplot
>> x = 0:1/100:1;
>> y1 = sin(3*pi*x);
>> y2 = cos(3*pi*x);
>> y3 = sin(6*pi*x);
>> y4 = cos(6*pi*x);
>> title(‘Typical example of subplots’)
>> subplot(2,2,1), plot(x,y1)
>> xlabel(‘0 \leq x \leq 1’), ylabel(‘sin(3 \pi x)’)
>> subplot(2,2,2), plot(x,y2)
>> xlabel(‘0 \leq x \leq 1’), ylabel(‘cos(3 \pi x)’)
>> subplot(2,2,3), plot(x,y3)
>> xlabel(‘0 \leq x \leq 1’), ylabel(‘sin(6 \pi x)’)
>> subplot(2,2,4), plot(x,y4)
>> xlabel(‘0 \leq x \leq 1’), ylabel(‘cos(6 \pi x)’)
34
Basic plotting
Subplot
35
Programming in MATLAB
M-File scripts
36
Programming in MATLAB
M-File scripts
Enter the following statements in the file
load 'soilT.dat';
time=soilT(:,1);
soil_temp_mor=soilT(:,2);
soil_temp_aft=soilT(:,3);
plot(time,soil_temp_mor,'--',time,soil_temp_aft,'-');
xlabel('Time');
ylabel('Soil temperature');
legend('Morning','Afternoon');
title('Soil Temperature');
37
Programming in MATLAB
M-File scripts
Run the file Soil Temperature
8
Morning
6 Afternoon
2
Soil temperature
-2
-4
-6
-8
-10
11 11.5 12 12.5 13 13.5 14
Time
38
Programming in MATLAB
M-File scripts
MATLAB treats anything that appears after the % on a line
as comments and these line will be ignored when the file
runs
% -------------------------------------------------------
% scriptexample.m is to display soil temperature in the morning and
% the afternoon.
% -------------------------------------------------------
39
Programming in MATLAB
M-File functions
Functions are routines that are general and applicable to
many problems.
To define a MATLAB function:
Decide a name for the function, making sure that it does
not conflict a name that is already used by MATLAB.
Document the function
The first command line of the file must have this format:
function[list of outputs]=functionname(list of inputs)
…….
Save the function as a M-file
40
Programming in MATLAB
M-File functions
x2 if 1 x 0.5
F
0.25 if 0.5 x 1
41
Programming in MATLAB
M-File functions
It is convenient to have a separate file which can do a
specific calculation.
function [F]= eff(x)
% Function to calculate values
% Input x
% Output F
for i=1:length(x)
if x(i)<0.5
F(i)=x(i)^2;
else
F(i)=0.25;
end
end
42
Programming in MATLAB
M-File functions
To evaluate this function, a main program is needed. This main
program provides input arguments
43
Programming in MATLAB
M-File functions
Run the main file
The Piecewise Defined Function:
1
0.9
0.8
0.7
0.6
0.5
F
0.4
0.3
0.2
0.1
0
-1 -0.8 -0.6 -0.4 -0.2 0 0.2 0.4 0.6 0.8 1
x
44
Questions and Comments?
Email: research@unc.edu
Phone: 919-962-HELP
Submit help ticket at http://help.unc.edu
45