Upload
matthew-cole
View
234
Download
1
Embed Size (px)
DESCRIPTION
variable operations
Citation preview
����������� �������� ��������� �
������������ ������� � � �� ��
Lecture 2 - Variables, Operations
� �����
• � ����������� ����� ������� ��������������� ��� �
• !���� �"���!�����!�� ������#�������$
• ���������� ����������� � %���&���!���������
• �������������' ��� ��� �������(��� ��)�������#������• �������������' ��� ��� �������(��� ��)�������#������
!������������ ��$
• *� %�+ ��� � ����" !���� %���&��(��� ��� �
��� �����
• � ����������' ��,��������� "����!����������!�� ����
�-
• �(����������������� � ����� "������!������&�
• .����(��!�� ������ �������/������������� ���• .����(��!�� ������ �������/������������� ���
[ ] [ ]
���
���
=
==
011001-
1.35.1 5.3
C
BA
���
����
�
=
20001-1011
C
������������������� ��� ���
• � ������������!�� ���������*� ��� ���+
• � ��� � #� �×××× �$� �������&���� ����������"��� ����' ��
#� $���������� ���#�$
• ������1�5�×××× 5�� ��� �
• 2�' �������1�5�×××× ��� ��� �
6���� ��������1�� �×××× 5�� ��� �• 6���� ��������1�� �×××× 5�� ��� �
[ ] [ ]
���
�
�
���
�
�
−−−−
−=
���
�
�
���
�
�
−==
−==
22.715.05.92.3425231
21.73.2
02.5'
21.73.202.5 27.5
DBC
BA
• ����(������������������������#�* ���������������* ���������������������+�������,�
• %���������������������������- ��� �* ���������������#�������,./#01+��&'�&2�����������#�������,./#01+��&'�&2
• (�����������������������* ���������������#���������������������������(�����������!�����+������,����������32�!������
>> pians =
3.1416
>> size(pi)ans =
1 1
>> a=[1 2 3; 4 5 6]a =
1 2 3 4 5 6
>> size(a)ans =1 1 ans =
2 3
14163.pi ====��������
������������
����====
654321
a
» x=3+5-0.2x =
7.8000» y=3*x^2+5y =187.5200
» z=x*sqrt(y)z =106.8116
» A=[1 2 3; 4 5 6]A =
» who
Your variables are:
A b y C x z
» whosName Size Bytes Class
MATLAB Example
A =1 2 34 5 6
» b=[3;2;5]b =
325
» C=A*bC =
2252
A 2x3 48 double arrayC 2x1 16 double arrayb 3x1 24 double arrayx 1x1 8 double arrayy 1x1 8 double arrayz 1x1 8 double array
» save
Saving to: matlab.mat
» save matrix1
default filename
Filename “matrix1”
6 ��(���' ��������� �������������������� ��
• Long rows can be continued on the next line through the use • Long rows can be continued on the next line through the use of a comma and three periods (an ellipsis)
•Elements of a matrix can be changed individually by referring to a specific location
If S = [5,6,4]we can change the second element of S from 6 to 8 by issuing the command S(2) = 8
•We can define a matrix using previously defined matrices. •We can define a matrix using previously defined matrices. For example, if S=[5,6,4], we can do the following
S
� ��� ��6��������� ��
[[[[ ]]]] [[[[ ]]]] 497y ; 321x ========
[[[[ ]]]] [[[[ ]]]] 497321yxz ========
[[[[ ]]]] [[[[ 321y ;x u ========
[[[[ ]]]] [[[[]]]] 321497
497321 xy ;y xv ========
[[[[ ]]]] [[[[]]]] 497
321y ;x u ========
6������ �������• �����������������������!������* ��(������������(����������"���* �
����������7����������#������������������������������������������!�������(�������������� ����������������������������������(�����������������(��������(������������#�����������������������������������* �����������
C = [-1,0,0; 1,1,0; 1,-1,0; 0,0,2]
x = C (:,1); % stores the first column of C in the column vector x.
y = C (:,2); % stores the second column of C in the column vector y.
z = C (:,3); % stores the third column of C in the column vector z.
6������ �������
• 6���� �"���' �� ��� ����&��� ������ �� �"�� ��� �
C = [1,2,5; -1,0,1; 3,2,-1; 0,1,4]
F = C(:, 2:3) = [2,5; 0,1; 2,-1; 1,4]
����������������
����
����
����������������
����
����
−−−−====
����������������
����
����
����������������
����
����
−−−−−−−−
====
41121052
F
410123101521
C
6������ �������• 6���� �"���' �� ��� ����&��� ������ �� �"�� ��� �
�������� 521
C = [1,2,5; -1,0,1; 3,2,-1; 0,1,4]
E = C(2:3,:) = [-1 0 1; 3 2 -1]
��������
������������
����
−−−−−−−−
====
����������������
����
����
����������������
����
����
−−−−−−−−
====123101
E
410123101521
C
6������ �������
• 6���� �"���' �� ��� ����&��� ������ �� �"�� ��� �
�������� 521
C = [1,2,5; -1,0,1; 3,2,-1; 0,1,4]
G = C(3:4,1:2) = [3,2; 0,1]
��������
������������
����====
����������������
����
����
����������������
����
����
−−−−−−−−
====1023
G
410123101521
C
�(�������
• ����������������������������"�����!����������������
�������#��!������������������������������������
�����������������������������
�-�517
������������������������������
��-�85179
����������������* �������
The default increment is 1, but if you want to use a different increment put it between the first and final values
6������ �������
• ��� ����:��� ��-��1����1�
� � ��-��;�1�;71<;7
� � ��-�8�;����;7��5;���5;7��<;���<;79
• =�"�� !�� ����� ���• =�"�� !�� ����� ���
!������-�5�151<
!������-�85���>��?��@��A��7��B����<9
� �������)���� ��
• � ������#�5���<$" !���5����!���,��������!���������' �����5������<
��-�� ������#�5��<$
• � ������#�����$"�����������/����,���������� ����
���' ������������
��-�� ������#�����$
» linspace(0,2,11)
ans =
Columns 1 through 7
0 0.2000 0.4000 0.6000 0.8000 1.0000 1.2000
Columns 8 through 11
1.4000 1.6000 1.8000 2.0000
��"������)���� ��
• ��"�����#�����$��"�������������"�� �(� ����,��/����,����������' �!�����
��-���"�����#�����$
• ��"�����#���$�"���������7����"�� �(� ����,��/����,�
��������� ���
��-���"�����#���$��-���"�����#���$
» logspace(-4,2,7)
ans =
0.0001 0.0010 0.0100 0.1000 1.0000 10.0000 100.0000
• ����� - �����������������* ��������������������������
��������#���"�
''��89:
��8
Note that an empty matrix is different from a matrix that contains only zeros.
��8
9:
���� ��������������� ��� ���
• ���������(��������#�- ��� �������* ������!����������(�����!�����������������"���������
; �����������������- ��� �(������"����������������#���"�#
<���
����8�/�303=
; �#�>����������������!����
; �(��������- ��� ����������(�����(�����#�����#�3�?
; @����������- ��� ����������������(������������ �+
�(��������������(���?�?����������
; �������������������������������#���"�#
<������
����8
3�?�A??/�B
3�CCD?�?�??E?�?�?2D?�?�?2/?�?�?3=?�?�?F?E
; �����������������������������������"�(�����
''�����
����8����8
3F&%��&2??D
; ��������������������(������"&����������������* ������* �
��������������((��#���"�#
<����
����8�2�22?0�&?3=
���� ���� ��� ���
��������
��������====����
��������
������������
====
������������
����
����
������������
����
����
====������������
����
����
������������
����
����
====
1111ones(2,4) 111
111)3(ones
000000
zeros(3,2) 100010001
)3(eye
��������
��������
====������������
����������������
����
====1111
ones(2,4) 111111)3(ones
C= [1 2 3; 4 2 5]; D = eye(size(C));
>> D = eye(size(C))
D =
1 0 00 1 0
• %�������(������������
zeros(n) Returns a n X n matrix of zeros zeros(m,n) Returns a m X n matrix of zeros
ones(n) Returns a n X n matrix of onesones(m,n) Returns a m X n matrix of onesones(m,n) Returns a m X n matrix of ones
size(A)For a m X n matrix A, returns the row vector [m,n] containing the number of rows and columns in matrix
length(A) Returns the larger of the number of rows or columns in A
.����3����6����!�������(�����������������������������������"�������������������"�����
�������������#�* ��������������������������"����������* �����(�����������������
!���������������������������!�������������(�����(���������������!��������������
�������������������#�����������������������������������
=�� ����0 ����,
• %������(���@�������
– ������"����������������������������������������������#����
���G����* �������������* ����������������!�������"�����!����
��������������
– ��- ��� #�!����������������(��������������������"������– ��- ��� #�!����������������(��������������������"������
* ������������* �������������������������������������
.H����������������������������������������������1�
It is important to omit blanks between the decimal number and the exponent. For example, MATLAB will interpret
6.022 e23as two values (6.022 and 1023 )
0 ����,�)��� ��
• - ����������������(������������!�������
• @���������* ������������(����������������#�- ��� �
����������������������(������"����������������������
�������������
• - ��� ������������������"������������������������• - ��� ������������������"������������������������
���(������"��������������
File -> Preferences
Changing the data format
>> value = 12.345678901234567;format short → 12.3457format long → 12.34567890123457format short e → 1.2346e+001format long e → 1.234567890123457e+001format long e → 1.234567890123457e+001format short g → 12.346format long g → 12.3456789012346format rat → 1000/81
���������������.1��������������������������������������
����������(���������
>> disp('A Text String to Display.')A Text String to Display.>> M = [1, 2; 3, 4];>> disp(M)
1 23 4
A fully customizable way of printing both text and matrices is the fprintf() command
fprintf(format_string,matrices)
���(�����������"�������������������* �������������������"�
* ������� ���� ����� #�J �#�J (#�����J "#�* ����������(�����(�
��* ��������������������!��!��������������������������
; J �#�J (#����J " ������������������#�(����������#����"�����
.�������J (����J ��* ������!�������������1
; K��������- ��� ��������������* �����; K��������- ��� ��������������* �����''�(�����(.LM����#�* ����!����8�J (��������8�J =�0(��K�L#���
�#��1
M����#�* ����!����8�20�F=D???��������8�/�303=��
''�(�����(.LM����#�* ����!����8�J (��������8�J 32�0(��K�L#�#��1
M����#�* ����!����8�20�F=D???��������8�������/�303=��
''�(�����(.LM����#�* ����!����8�J (��������8�J 32�=(��K�L#�#��1
M����#�* ����!����8�20�F=D???��������8�����/�303FC/��
''�(�����(.LM����#�* ����!����8�J (��������8�J 3?�=(��K�L#�#��1
M����#�* ����!����8�20�F=D???��������8���/�303FC/��
�����)���������* �����
• =������������� ����������������������������������������
����N��+
• =��������"�������9
O������ "����������������� �+O������ "����������������� �+
O�=������������������"���������������"���� +
• )����������"���"�������9
O� ��� "�������"��������������
O�"�����""�������"��"������ ������+
�#����� ��������� ��
• �����������������#��������3 �-� ��3 # �#������������
�� ���������#���������3 �#��#��@��� ���$'A������ ���
$��� ���9�/� ����� ������% �����0'&
• �# ������� �����4�����������% ������������� ����#��• �# ������� �����4�����������% ������������� ����#��
���% ��� ��� ������� ���.������3 ���"-���� �������������
���"���� ����& �% "������3 �# ���#�����% ������ ���
����@����������% % ����A�3 # �#���������3 #��������
#�3 ��#����������������"��3 � ����&
•)������������������!������"������������������� �����
������� ��� � ������� +�)������������������!���� ������ ������������������+
•=������������������������ �������� �"������ "������������� �+�)���"�����!����� �������������� ���������� ��+
��������������������
•F ��"���������������!
•F �"������������"�������������������!���"���� •F �"������������"�������������������!���"����
•F "�"����������"�����!�����������
•F ��"����������"�����!����������������������"���������
•F !�"�����������������������"���"��������F "���F �
•C�������������������������������!•C���������������������������!
���% ����)�������� �F !��������������!������������������,�F "���������"��� ������������,�F ��� �����������!��,��� ��F ���������!+ ���������������������������������������BF ���� ������������������������"��� ��� ����� �������������"� �!�����"�������� �����������+ 1������������>����� 9
• "�����"�PF G!P,?Q ���?Q
• "�����"�PF ?Q+@"P,?JI+@GA ��?JI+@GA�• "�����"�PF ?Q+@"P,?JI+@GA ��?JI+@GA�
• "�����"�PF ?Q�P,�P"�� P ������"��
������������������������������!�"������������ ���������������
B������ ��"������9
• ����>������P5�������������9�P #
• ����>������P5�������������9�P,�P�P #
��� ���$�'����������
temp = 98.6;
fprintf(‘The temperature is %8.1f degrees F.\n’, temp);
The temperature is 98.6 degrees F.
fprintf(‘The temperature is %8.3e degrees F.\n’, temp);
The temperature is 9.860e+001 degrees F.
fprintf(‘The temperature is %08.2f degrees F.\n’, temp);
The temperature is 00098.60 degrees F.
��� ���$�'�������� ���
Score = [1 2 3 4; 75 88 102 93; 99 84 95 105]
Game 1 score: Houston: 75 Dallas: 99
fprintf(‘Game %1.0f score: Houston: %3.0f Dallas: %3.0f \n’,Score)
Game 1 score: Houston: 75 Dallas: 99
Game 2 score: Houston: 88 Dallas: 84
Game 3 score: Houston: 102 Dallas: 95
Game 4 score: Houston: 93 Dallas: 105
fprintf control codes\n Start new line \t Tab
��������� �(� �� ��� ����� ���
bababaabaabaab
÷÷÷÷−−−−−−−−−−−−
/ ;* ; division and tionMultiplica/ * Negation
^ tionExponentia^ Form MATLABOperation Symbol
� In order of priority
bababa
baabba
bababaab
−−−−++++±±±±−−−−++++
====
÷÷÷÷
; nsubtractio and Addition
\ \ division Left\
/ ;* ; division and tionMultiplica/ *
(Matrix inverse)
� ������&�������������&��� �(� �� ��� ����� ���
1. Parentheses, starting with the innermost pair2. Exponentiation, from left to right3. Multiplication and division with equal precedence, 3. Multiplication and division with equal precedence,
from left to right4. Addition and subtraction with equal precedence,
from left to right
� ������&�� ����� ��
5*(3+6) = 45
5*3+6 = 21
White space does not matter!!!
5*3 + 6 = 21
6�� �����&��� ���&������ "(�
5*6/6*5 = 25
5*6/(6*5) = 15*6/(6*5) = 1
������(����
• ���������.�1
• N�O�����9�:��������������"���((������
• - ��� ��������������������������
5 * (3+4) not 5(3+4)
� ������&�������������&��� �(� �� ��� ����� ���
� The priority order can be overridden with parentheses» y = -7.3^2
y =
-53.2900
» a=3; b=5; c=2;
» s1 = a-b*c
s1 =
» y=(-7.3)^2
y =
53.2900
-7
» s2=(a-b)*c
s2 =
-4
Exponentiation has higher priority than negation
Multiplication has higher priority than subtraction
=�� �� ����� � ��� ���• �������������"���(���������- ��� �����* ����* �������(����
±3?&/?E ���±3?/?E
• (��!��(��* �.��������������"�1��������- ��� ���
��������������������(��(#�������"���(�����
• (������(��* �.����������������1��������- ��� ��
��������������������(�?>> a = 5.00e200; % same as 5*10^200>> b = 3.00e150; % same as 3*10^150>> b = 3.00e150; % same as 3*10^150>> a*b
ans =
Inf
>> 1/a * 1/b
ans =
0>>
• 4 �������������������������* ��������������������(�
���������������"�? ����?�?�
• 7�������(����������- ��� �"�!�����������������"���(���!��� ���
$�����������������(
• ������������������- ��� �"�!�����������������"���(
��!�������$����������������@�@
2���� �����������" ����� ��������
Operator Description���� Returns � for every element location that is � � � �
(nonzero) in both arrays, and � for all other elements.
���� Returns � for every element location that is � � � �(nonzero) in either one or the other, or both, arrays and � for all other elements.
���� ����� Complements each element of input array, �.
���� Less than�������� Less than or equal to���� Greater than�������� Greater than or equal to� �� �� �� � Equal to�������� Not equal to
��" ���������� ���
>> A = [2 5 7 1 3];>> B = [0 6 5 3 2];>> A >= 5ans =0 1 1 0 0>> A >= Bans =ans =1 0 1 0 1>> C = [1 2 3]>> A > C??? Error using ==> gt
Matrix dimensions must agree;
��" ���������� ����#���� ����$
>> A = [true true false false];
>> B = [true false true false];
>> A & B
ans =
1 0 0 0
>> A | B>> A | B
ans =
1 1 1 0
��)�������1��(��& ���&���� ��
• )��������"�������������!��>> C = find(B)
ans =
[1 3]
• ��� (���������������������"�����• ���find(...) (���������������������"�����
!�����������������������������������������(�
��������������(������!��������������������
����,�� ����� ���
• �������,������� ��� �����&��� ������� ����,���� ���
B(3);* A(3) C(3)B(2);* A(2) C(2)B(1);* A(1) C(1)
============
B(5);* A(5) C(5)B(4);* A(4) C(4)B(3);* A(3) C(3)
============
MATLAB: C = A.*B;
��� ����, ��� ����� ����� ���
] ,[] ,[] ,[nsubtractio Array-] ,[] ,[] ,[addition Array
] ,[] ,[nsubtractio array-Scalar-] ,[] ,[addition array-Scalar
ExampleFormOperationSymbol
343864BA9123864BA
32638bA97364bA
−−−−====−−−−−−−−====++++++++++++
−−−−====−−−−−−−−====++++++++++++
] ,[]^ ,^[] ,[].^ ,[] ,[]^ ,^[] ,[.^
] ,[]^ ,^[].^ ,[.^tionexponentia Array.^].,.[]\ ,\[] ,[\]. ,[division left Array\.
].,.[]/ ,/[] ,/[]. ,[division right Array./] ,[] ,[*]. ,[tionmultiplica Array*.
812543254235243953235238643234324BA
71430667257835873A.\B4001375057835873A./B
1863263A.*B
========================
================
−−−−====−−−−
MATLAB interprets * to mean matrix multiplication. The arrays a and b are not the correct size for matrix multiplication in this example
�(��� ��� ������� � �,��&�� ������� �C���
�����,������������ � !���������� ���
• 7����������#��������������!����������(���"����
�����"��������������* ����������������!�������
������������������
– 7�������������!���������������������
– P��(�������������������
π2360RD =
Either the * or the .* operator can be used for this problem, because it is composed of scalars and a single matrix
The value of pi is built into MATLAB as a floating point number, called pi
������������ ��� �������� ���
But a*b gives an error (undefined) because
������������
����
����
������������
����
����
====++++������������
����
����
������������
����
����
====������������
����
����
������������
����
����
====1173
ba 642
b 531
a
But a*b gives an error (undefined) because dimensions are incorrect. Need to use .*
������������
����
����
������������
����
����
====������������
����
����
������������
����
����
====30122
6*54*32*1
b.*a
������ %���� ��� ��� ����� ���
[[[[ ]]]][[[[ ]]]]
[[[[ ]]]]240122B.*AC
2541B
1832A
========
====
====
[[[[ ]]]][[[[ ]]]]
[[[[ ]]]][[[[ ]]]]9243813B).^3(F
15122783.^AE
5.06.175.02B/.AD
240122B.*AC
========
========
========
========
����,�� ����� ����&���
� �×××× ��� ��� ���
��������
�������� 2015105
[[[[ ]]]]������������
����
����
������������
����
����
−−−−−−−−−−−−−−−−−−−−========
121343214321
1 - 21 3-4;:-1:1 -4;:1A
������������
����
����
������������
����
����
−−−−−−−−−−−−−−−−−−−−========
51051520151052015105
5.*AB
������������
����
����
������������
����
����
−−−−−−−−−−−−−−−−−−−−========
18127642781642781
3.^AC
� ��� �����������[[[[ ]]]] [[[[ ]]]]
[[[[ ]]]]84124
2-13
y' ; 324
'x
213y ; 324x
��������
�������� −−−−
��������
��������
������������
����
����
������������
����
����
====������������
����
����
������������
����
����
−−−−====
−−−−====−−−−====
[[[[ ]]]]
[[[[ ]]]] 4)2(3)1)(2()3)(4(2-13
324'y*x
639426213
32y'*x
====−−−−++++−−−−++++====������������
����
����
������������
����
����
−−−−====
������������
����������������
���� −−−−−−−−−−−−====−−−−
������������
����������������
����
−−−−====
The transpose operator makes it easy to create tables
table =[degrees;radians]’ would have given the same result
The transpose The transpose operator works on both one dimensional and two dimensional arrays
Linear Combinations
����&���(���������(��������$��"�
������• ���.�1��,�����������������"����!���������
• ���.�1 ,�����������������������!���������
• ���.�1���,�������������������(������������������
• ����.�1��,�����������������������(�����������������
• ����.�1)����������������.�!���"��!����1�(��������������• ����.�1)����������������.�!���"��!����1�(��������������
�(�����!��������
• ������.�1,���������������������!������(�����������������
����!�������
• �����.�1����������!������* ��������!�������(����� ��������"�
������
���� ��
* �8�9?��&2��D:��������8�9/�&3�F�D:�������8�9�3�/�D+�2�E�0+�=�&3�&2:�
����������������� ������� � ��� ��
• ���.* 1��
• ���.�1�
• ���.*#�1�• ���.*#�1�
• ����.�1�
• ������.* 1�
• ����.2B* A�1�
• ����.�1
��� ��� ������� �����
• The way in which the numbers are presented in the computer is a source of roundoff errors.
Ex:>>1-5*0.2=0;>>1-5*0.2=0;>>format long1-0.2-0.2-0.2-0.2-0.2=5.551115123125783e-017
• WHY?
#���������������������������%������ �� ���
122792.02792.12 = x 210
ExponentMantissa
#������%���� �������� ��
64 bits
Mantissa (52 bits)
Exponent (11 bits)
Sign of mantissa (1 bit)
��� �)����� ������������������
Numbers: precision and accuracyNumbers: precision and accuracy
Good Accuracy Good Precision
Good PrecisionPoor Accuracy
Good AccuracyPoor Precision
Poor AccuracyPoor Precision
•Low precision: π = 3.14•High precision: π = 3.140101011•Low accuracy: π = 3.10212•High accuracy: π = 3.14159•High accuracy & precision: π = 3.141592653
Precision and accuracy are not the same!