what is matlab - bgutools/matlab/matlabnew.pdf · characteristics of matlab • programming...
TRANSCRIPT
![Page 1: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/1.jpg)
![Page 2: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/2.jpg)
What is MATLAB?
• It stands for MATrix LABoratory
• It is developed by The Mathworks, Inc (http://www.mathworks.com)
• It is an interactive, integrated, environment
– for numerical computations
– for symbolic computations
– for scientific visualizations
• It is a high-level programming language
![Page 3: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/3.jpg)
Characteristics of MATLAB
• Programming language based (principally) on matrices .
– Slow (compared with fortran or C) because it is an interpreted language ,i.e . not pre-compiled. Avoid for loops; instead use vector form whenever possible .
– Automatic memory management, i.e., you don't have to declare arrays in advance .
– Intuitive, easy to use .
– Shorter program development time than traditional programming languages such as Fortran and C .
– Can be converted into C code via MATLAB compiler for better efficiency .
• Many application-specific toolboxes available .
![Page 4: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/4.jpg)
Start menu Matlab MATLAB
“>>” – שורת הפקוד ה
>> date
![Page 5: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/5.jpg)
Getting Help
>> help date
>> helpwin date
helpwin gives you the same information as help, but in a different window.
![Page 6: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/6.jpg)
Getting Help
>> doc date
>> lookfor date % search for keywords that best describe the function
>> Ctrl+C % stop Matlab from running
>> clc % clear screen
![Page 7: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/7.jpg)
Special characters
• >> % default command prompt
• % % comment - MATLAB simply ignores anything to the
right of this sign (till the end of the line).
>> % my comment
• ; % semicolon at the end of the line will prevent MATLAB
from echoing the information you type on the screen.
>> a=20
>> B=20;
![Page 8: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/8.jpg)
Creating Variables
• Matlab as a calculator:
• >>2+5
• >>7*10+8
• >>5^2
• ‘ans’ - "answer", used in MATLAB as the default variable.
![Page 9: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/9.jpg)
Defining Your Own Variables
• When Matlab comes across a new variable name it
automatically creates it.
• Begins with a LETTER, e.g., A2z.
• Can be a mix of letters, digits, and underscores (e.g.,
vector_A, but not vector-A)
• Not longer than 31 characters.
• No spaces
• Different mixes of capital and small letters = different
variables.
For example: "A_VaRIAbLe", "a_variable", "A_VARIABLE",
and "A_variablE
• >> String='this is a string'
![Page 10: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/10.jpg)
Listing & Clearing Variables
>> whos
>> clear %clear variables from memory
>> a=10
>> b = 20
>> the_average = (a + b ) / 2
![Page 11: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/11.jpg)
Creating vectors
Column separator:
space/coma (,)
Creating sequences:
From : jump: till
Row separator:
Semicolon (;)
![Page 12: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/12.jpg)
Creating Matrices
• Matrices must be rectangular.
•Creating random matrices:
2-by-4 random matrix
(2 rows and 4 columns).
![Page 13: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/13.jpg)
Creating Matrices
• You can combine existing vectors as matrix elements:
• You can combine existing matrices as matrix elements:
![Page 14: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/14.jpg)
Indexing Into a Matrix
>> mat_from_mat(8)
>> mat_from_mat(4,2)=0
>> mat_from_mat
• The row number is first, followed by the column number.
![Page 15: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/15.jpg)
Linear Algebra Operations
![Page 16: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/16.jpg)
Matrix Multiplication
• Inner dimensions must be equal
• Dimension of resulting matrix = outermost
dimensions of multiplied matrices
• Resulting elements = dot product of the rows of
the 1st matrix with the columns of the 2nd matrix
![Page 17: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/17.jpg)
Type the following:
>>a=[2 3]
>>b=[3 2]
>>a*b
>>a.*b
>>a.*b'
>>a*b'
Vector Multiplication
![Page 18: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/18.jpg)
Example: Solving Equations
• Solve this set of simultaneous equations
![Page 19: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/19.jpg)
Array Operations
![Page 20: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/20.jpg)
Boolean Operators & Indexing
![Page 21: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/21.jpg)
String Arrays
• Created using single quote delimiter (')
• Each character is a separate matrix element (16 bits of
memory per character)
• Indexing same as for numeric arrays
![Page 22: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/22.jpg)
String Array Concatenation
![Page 23: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/23.jpg)
Working with String Arrays
![Page 24: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/24.jpg)
Line Plots in Two Dimensions
• Plot (x,y)
• makes a two-dimensional line plot for each point in X and its
corresponding point in Y: (X(1),Y(1)), (X(2),Y(2)), (X(3),Y(3)), etc.,
and then connect all these points together with line.
• Example:
• >> x=1:1:5;
• >>y=[2 7 0 -8 6];
• >> plot (x,y);
• >> xlabel ('label for x-axis')
• >> ylabel ('label for y-axis')
• >> title ('title')
![Page 25: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/25.jpg)
• Check the following:
• x_points = [-10 : .05 : 10];
• plot(x_points, exp(x_points));
• grid on
• hold on
• plot(x_points, exp(.95 .* x_points), 'm');
• plot(x_points, exp(.85 .* x_points), 'g');
• plot(x_points, exp(.75 .* x_points), 'p');
• xlabel('x-axis'); ylabel('y-axis');
• title('Comparing Exponential Functions');
• legend ('1', '2', '3', '4')
Multiple Plots
-10 -5 0 5 100
0.5
1
1.5
2
2.5x 10
4
x-axisy-
axis
Comparing Exponential Functions
1
2
3
4
![Page 26: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/26.jpg)
Subplots
• multiple plots in the same window, each with their own
axes.
• Subplot (M,N,P)
• M – rows
• N - columns
• P – number of subplot
in the figure
Subplot (2,2,1)
![Page 27: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/27.jpg)
More about figures
• Figure % Open a new figure without closing old figures
• Figure (i) % Open the i-th figure
• Close all % close all open figures
• axis ([xmin xmax ymin ymax]) % sets scaling for the x-
and y-axes on the current plot.
![Page 28: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/28.jpg)
Special Graph Annotations (TeX)
![Page 29: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/29.jpg)
Plot Editor Toolbar
![Page 30: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/30.jpg)
Exercise
• x = (1, 1.05, 1.1, 1.15… 5)
• Y=sin(x)
• Z=log(x)
• Put your name in the title
• Hint: check the doc “linespec”.
Create the following:
1 1.5 2 2.5 3 3.5 4 4.5 5-1
-0.5
0
0.5
1
1.5
2Merav's graph
x
ysin(x)
log(x)
![Page 31: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/31.jpg)
Solution
• >>x=1:0.05:5;
• >>y=sin(x);
• >>z=log(x);
• >> hold on
• >> plot (x,y,'-.r*')
• >> plot (x,z,'-.go')
• >> hold off
• >> title ('Merav''s graph');
• >> xlabel ('x')
• >> ylabel ('y')
• >> legend ('sin(x)', 'log(x)');
![Page 32: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/32.jpg)
More exercise
• Make a 3 three-dimensional graph of (x,y,z) – use Matlab
help.
• Make two separate 2-D graphs, with separate axis, in the
same window: y vs. x, and z vs. x.
1
2
3
4
5
-1
-0.5
0
0.5
10
0.5
1
1.5
2
x
3D graph
y
z
1 2 3 4 5-1
-0.8
-0.6
-0.4
-0.2
0
0.2
0.4
0.6
0.8
1sin(x)
x
y=
sin
(x)
1 2 3 4 50
0.2
0.4
0.6
0.8
1
1.2
1.4
1.6
1.8
x
log(x)
z
![Page 33: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/33.jpg)
Solution
• 3-D graph:
• >> plot3(x,y,z)
• >> grid
• >> xlabel ('x')
• >> ylabel('y')
• >> zlabel('z')
• >> title ('3D graph')
• Subplots
>> subplot (1,2,1);
>> plot(x,y);
>> title ('sin(x)');
>> xlabel('x');
>> ylabel('y=sin(x)');
>> grid;
>> subplot (1,2,2);
>> plot(x,z);
>> xlabel('x');
>> title ('log(x)');
>> grid;
>> ylabel ('z');
![Page 34: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/34.jpg)
Surface Mesh plots
1) Generate a grid of points in the xy-plane using the
meshgrid command.
2) Evaluate the three-dimensional function at these points.
3) Create the surface plot with the mesh command.
Meshgrid - returns all possible combinations of (x,y) points,
where x is taken from X and y is taken from Y, in the form of two
matrices.
you will see in the generated surface mesh plot a bunch of
rectangles, of width and length equal to the regular spacing values
for X and Y, and the height of the corners of the rectangles will be
equal to the value of the function at the rectangles' corner points)
![Page 35: What is MATLAB - BGUtools/MATLAB/matlabnew.pdf · Characteristics of MATLAB • Programming language based (principally) on matrices . – Slow (compared with fortran or C) because](https://reader030.vdocuments.net/reader030/viewer/2022021722/5c65090109d3f2a86e8c0b9a/html5/thumbnails/35.jpg)
1. >> x_points = [-10 : 1 : 10];
>> y_points = [-10 : 4 : 10];
>> [X, Y] = meshgrid(x_points,y_points);
2. >> Z = X.^2 + Y.^2;
3. >> mesh(X,Y,Z);
>> xlabel('x-axis');
>> ylabel('y-axis');
>> zlabel('z-axis');
-10-8
-6-4
-20
24
68
10
-10
-5
0
5
10
0
20
40
60
80
100
120
140
160
180
200
x-axis
X: -9
Y: -10
Z: 181
y-axis
z-a
xis