Ch02 - MATLAB Basics (Part 2)
Ch02 - MATLAB Basics (Part 2)
Ming Jiang
www.wcomms.com
Chapter 2: MATLAB Basics
(Part 2)
Ming Jiang
www.wcomms.com
MATLAB Programming for Engineers
Chapter 2: MATLAB Basics (Part 2)
Ming Jiang
Scalar Operations
Example 1a
(a) a + b =
(b) a .* b =
(c) a * b = Only
supported
by latest
(d) a * c = versions
(e) a + c = 4 3
4 3
(f) a + d =
(g) a .* d =
(h) a * d =
(i) d ./ a = (m) a \ d =
Example 1b ans = Error using \ Scalar
5.0000 Inf Matrix dimensions
2.5000 5.0000 must agree.
xn = ?
X = A \ B
Ming Jiang
Hierarchy of Operations
Operations' Precedence
• The contents of all parentheses are evaluated, starting from the
innermost parentheses and working outward
• All exponentials are evaluated, working from left to right
• All multiplications and divisions are evaluated, from left to right
• All additions and subtractions are evaluated, from left to right
Ming Jiang
Built-in MATLAB Functions (1/2)
What is a Function
• Function = an expression that accepts one or more
input values and calculates the results from them
• Functions for scientific and technical calculations
are more complex than +, -, *, \, ^ operations
Examples of Functions
• Commonly used function examples:
• Trigonometric functions, logarithms, square roots, etc.
• Rarer functions examples specific to a single or a few problems
• Hyperbolic functions, Bessel functions, etc.
Example
>> maxval = max ([1 -5 6 -3])
maxval =
6
Example
x =
0 1.5708 3.1416 4.7124 6.2832
>> y = sin(x)
y =
0 1.0000 0.0000 -1.0000 -0.0000
>> 2^3
Cannot execute the >> power([1 2 3],[1 2 3])
ans =
math operation with ans =
8
non-square matrix 1 4 27
>> x = [1 2 3]
>> log2([1 2 4]) = ?
>> x^3 = ?
>> log10([1 10 100]) = ?
Error using ^
ans =
Inputs must be a scalar
0 1 2
and a square matrix.
n 2 3
Examples x x x
ex 1 x
n 0 n! 2! 3!
>> k = 2;
>> B = [1 2; 3 4] n 2 3
X X X
B = 1 2 eX 1 X
3 4 n 0 n! 2! 3!
>> k.^B = ?
ans = 2 4 Bln k
8 16 k (e ) e
B ln k B Square
Matrix
>> k^B = ?
ans = >> k^B = expm(B*log(k))
10.4827 14.1519
21.2278 31.7106
>> str2num('3.1415926')
ans =
3.1416
Ming Jiang
Introduction to Plotting
>> spy = ?
Example
x = 0:1:10;
y = x.^2 - 10.*x + 15;
plot(x,y);
title('Example Plot');
ylabel('y');
xlabel('x');
grid on;
f ( x) sin(2 x)
d f ( x)
g ( x) dx 2 cos(2 x)
x = 0:pi/100:2*pi;
y1 = sin(2*x);
y2 = 2*cos(2*x);
plot(x,y1,x,y2);
% Incorrect
>> plot(x,y1,y2)
>> plot(x,y1;x,y2)
Example f ( x) sin(2 x)
d f ( x)
g ( x) dx 2 cos(2 x)
x = 0:pi/100:2*pi;
y1 = sin(2*x);
y2 = 2*cos(2*x);
plot(x,y1,'k-',x,y2,'b--');
title('Plot of f(x) and g(x)');
xlabel('x');
ylabel('y');
legend('f(x)','g(x)','location','NorthWestOutside');
grid on;
legend('f(x)','g(x)','location','NorthWestOutside');
legend('f(x)','g(x)','location','Best');
Least conflict
with data in plot
plot semilogx
semilogy loglog
Logarithmic Scales (3/3)
Example
SNR: Signal-to-Noise Ratio
Logarithmic
SNR = [0 2 4 6 8 10]; scale is useful
BER = [0.1 3e-2 6e-3 5e-4 2e-5 6e-7];
for expanding
BER = Bit Error Rate small data
ranges
plot semilogy
• Prerequisites:
• Voltage source: V = 120 V Example:
• Internal resistance: RS = 50 ohms
Power Circuits
• Load of resistance: RL
• Questions:
• Plot the power supplied to the load as a
function of RL
• Find the max power PLmax supplied to the load
V V
I R
RS RL
TOT
P I 2 R
L L
V 2 RL
PL I RL
2
( RS RL ) 2
% Define variables:
% PL -- Power supplied to load (watts) M-file
% RL -- Resistance of the load (ohms)
% RS -- Internal resistance of the power source (ohms)
% V -- Voltage of the power source (volts)
Solution:
RL = 50 ohms
PL,max = 72 V
Ming Jiang
Debugging MATLAB Programs (1/2)
• Heaven • Taxes
Check statements
• Check the placement of parentheses
• Break long assignment statement into smaller ones
Check functions
• Ensure input/output data of functions are correct
■ 本课程的课件
● 主要内容:本人制作
● 部分内容:参考了中山大学数据科学与计算机学院张雨浓教授
的2008年版课件
● 少量内容:参考了Kasetsart University的James Brucker博士
早期的课件
■ 下载地址:
http://www.wcomms.com/lectures/course-matlab.html
本课件及相关作业,仅限本课程教学使用
请勿上传互联网
谢谢合作!
WWW.WCOMMS.COM