Predefined MATLAB Functions

Download Report

Transcript Predefined MATLAB Functions

Predefined MATLAB Functions
ELEC 206
Computer Applications for
Electrical Engineers
Dr. Ron Hayne
Elementary Math Functions








abs(x)
sqrt(x)
sign(x)
round(x), fix(x)
ceil(x), floor(x)
rem(x,y)
exp(x)
log(x), log2(x), log10(x)
206_M3
2
Trigonometric Functions
 Angle in radians (180 degrees = pi radians)


sin(x), cos(x), tan(x)
asin(x), acos(x), atan(x)
 Angle in degrees


sind(x), cosd(x), tand(x)
asind(x), acosd(x), atand(x)
angle_deg = angle_rad*(180/pi);
angle_rad = angle_deg*(pi/180);
206_M3
3
Data Analysis Functions
 Minimum and Maximum


max(x), [a,b]=max(x), max(x,y)
min(x), [a,b]=min(x), min(x,y)
 Averages

median(x), mean(x), std(x)
 Sums and Products


sum(x), prod(x)
cumsum(x), cumprod(x)
206_M3
4
Data Analysis Functions
 Sorting

sort(x)
 Size


size(x), [a,b]=size(x)
length(x)
206_M3
5
Random Numbers
 Uniform Random Numbers



rand('seed',n), rand(n), rand(m,n)
Interval 0 to 1
Interval a to b

x = (b - a)*r + a;
 Gaussian Random numbers

randn('seed',n), randn(n), randn(m,n)

Normal Distribution


Mean = 0, Standard Deviation = 1.0
Modified Distribution


Mean = b, Standard Deviation = a
x = a*r + b;
206_M3
6
Problem Solving Applied
 Weather Data

Problem Statement


Using the data in the file Weather_Data.xls, find the
total monthly precipitation, the total precipitation for
the year, and the day on which it rained the most.
Input/Output Description
Total Monthly Precipitation
Total Precipitation
Day of Most Rain
Weather_Data.xls
206_M3
7
Problem Solving Applied

Hand Example

12x31 Matrix
 Rows = Months
 Columns = Days
 Hundreths of inches
 Invalid = -99999
tot_jan = 2.72 in
 tot_feb = 1.66 in
 tot_jan+feb = 4.38 in
 max = Jan 3

206_M3
0
0
272
0
61
103
0
2
2
0
17
27
260
1
0
0
47
0
0
0
0
0
30
42
0
0
0
0
0
45
0
0
0
0
0
0
0
0
0
14
1
163
5
0
0
0
0
0
8
Problem Solving Applied

Algorithm Development
Import spreadsheet into matrix
 Change -99999 values to 0
 Interchange rows and columns
 Monthly Totals (inches) = sum columns/100
 Yearly Total = sum Monthly Totals
 Find max and location

206_M3
9
MATLAB Solution
206_M3
10
MATLAB Solution
clc
% Example 3.3 - Weather Data
% Find the total precipitation for each month, and
% for the entire year, using a data file
%
wd=Weather_Data;
wd(2,29)=0;
% Change -99999 values to 0
wd(2,30)=0;
wd(2,31)=0;
wd(4,31)=0;
wd(6,31)=0;
wd(9,31)=0;
wd(11,31)=0;
206_M3
11
MATLAB Solution
% Use transpose operator to change rows to columns
wd = wd';
% Sum of each column is sum for each month
monthly_total = sum(wd)/100
% Find the annual total
yearly_total = sum(monthly_total)
% Find the annual maximum, and month
[maximum_precip,month] = max(max(wd));
% Find the annual maximum, and day
[maximum_precip,day] = max(max(wd'));
% Print max data
maximum_precip = maximim_precip/100
month
day
206_M3
12
Manipulating Matrices
 Defining Matrices





A = [1 2 3; 4 5 6];
A = [1 2 3;
4 5 6];
B = [1 2 3]
B = [1 2 ...
3];
C = [A; B]
= [1 2 3; 4 5 6; 1 2 3];
206_M3
(2x3)
(1x3)
(3x3)
14
Manipulating Matrices
 Changing Matrices





D = [3 4 5];
D(2) = 8;
D =
3 8 5
D(5) = 7;
D =
3 8 5 0 7
206_M3
15
Manipulating Matrices
 Defining Matrices with the Colon Operator




E = 1:4
E =
1 2 3 4
F = 0.0:0.5:2.0
F =
0.0 0.5 1.0 1.5 2.0
206_M3
16
Manipulating Matrices
 Extracting Data with the Colon Operator





G = [1 2 3; 2 3 4; 3 4 5; 4 5 6];
x = G(3, :)
x =
3 4 5
y = G(:, 2:3)
y =
2 3
3 4
4 5
5 6
206_M3
17
Manipulating Matrices
 Extracting Data with
the Colon Operator



G = [1 2
2 3
3 4
4 5
G(3,2)
ans = 4
G(7)
ans = 4

3;
4;
5;
6];
206_M3
G(:)
ans =
1
2
3
4
2
3
4
5
3
...
18
Computational Limitations
 Double Range

 Double Precision
10-308 to 10+308

 Exponent Overflow


 Divide by Zero
y = 2e200;
y^2
ans = Inf

 Exponent Underflow


eps
ans = 2.2204e-016

x = 2e-200
x/y
ans = 0
206_M3
y/0
ans = Inf
0/0
ans = NaN
19
Special Values and Functions
 Special Values





pi
i
j
Inf
NaN
 Special Functions

clock


Year Month Day Hour Minute Seconds
date

dd-Mth-yyyy
206_M3
20
Summary
 MATLAB Functions





Math, Trig, Data Analysis, Random Numbers
Problem Solving Applied
Manipulating Matrices
Special Values and Functions
End of Chapter Summary

MATLAB Summary


Characters, Commands and Functions
Key Terms
206_M3
21