You are on page 1of 5

Note: All the examples are related to each other

Introduction to MatLab
Command Window: Used to enter variables and to run functions and M-file scripts. Workspace: consists of the set of variables built up during a MATLAB session and stored in memory. Editor: Use the Editor to create and debug M-files, which are programs you write to run MATLAB functions (saved as .m files). MATLAB file operations use the current directory and the search path as reference points. Any file you want to run must either be in the current directory or on the search path. The MATLAB Start button provides easy access to tools, demos, shortcuts, and documentation. Basic Commands: If you end a line with a semicolon ; and then press Enter, MATLAB runs the statement but does not display any output. Help command: followed by the name of the function & MATLAB displays the help for that function (e.g. help plot). Clc command: clear the command window without affecting the workspace. Clear command: clears workspace save command: saves workspace (.mat) load command: loads workspace Who command: lists all of the variables in your workspace Whos command: lists all of the variables in your workspace with their properties (value, type, size, ) clicking on UP ARROW recalls previous commands
1

Note: All the examples are related to each other

Matrices and Vectors: MATLAB uses variables that are defined to be matrices. A matrix is a collection of numerical values that are organized into a specific configuration of rows and columns. Nx1 and 1xN are vectors. The scalar is a 1x1 matrix. Examples: a=2 matrix 1x1 b= [ 2 5 4] vector 1x3 c = [ 6 3;0 2; 1 9 ] matrix 3x2 or c=[ 6 3 02 1 9] d= [ 1; 4; 8] or [1 4 8] vector 3x1 Variables can be of any type ( integer, float, complex,) without a prior declaration An individual element of a matrix can be specified with the notation b(i) or c( i;j) {where i&j starts from 1} Examples: b(2) is 5 and c(3,1) is 1 A matrix can be created using vectors of compatible dimensions Example: m= [ 1 3] n= [ 2 5; 1 0] p= [m;n] which is equivalent to p=[1 3; 2 5; 1 0] Special matrices: Ones, zeros, eye, diag (Use Help command to know more)

Note: All the examples are related to each other

COLON: : is one of the most commonly used special characters in MATLAB. If : between 3 numbers in the form of J:D:K, J is the initial value, D is the step size and K is the termination value. Example: e= 0:2:8 is the same as e =[0 2 4 6 8] If : between 2 numbers in the form of J:K, D is assumed = 1. Examples: f= 1:4 is the same as f=[1 2 3 4] c(1:2, 2) is [3;2] If : is used alone, it means any possible value Example: p(1,: ) is [1 2 1] Mathematical operations: Only matrices of the SAME ORDER can be added or subtracted. In case of multiplication x*y, the number of columns of x MUST be equal to the number of rows of y. To simply multiply or divide each element of a matrix by the corresponding element of another matrix. x .* y x ./ y x .^ y Element-by-element operations are executed when the operator is preceded by a dot . multiplies each element of x by the respective element of y divides each element of x by the respective element of y raise each element of x by the respective y element

Any function applied to a matrix (sin, cos, exp, log,) will be applied to each element.

Note: All the examples are related to each other

Plotting: plot(x,y) creates a Cartesian plot of the vectors x & y Example: t=0:8; f1= sin(t); plot (t,f1) Plot(X,Y,S) where S is a character string made from one element from any or all the following 3 columns: b g r c m y k blue green red cyan magenta yellow black . o x + * s d v ^ < > p h point - solid circle : dotted x-mark -. dashdot plus -- dashed star (none) no line square diamond triangle (down) triangle (up) triangle (left) triangle (right) pentagram hexagram

Example: plot (t,f1,ro) Plot (x1,y1,s1, x2, y2,s2,..) combines more than one plot on the same graph. Example: t=0:0.02:8; f1= sin(t); f2=cos(t); plot (t,f1,t,f2);

Note: All the examples are related to each other

xlabel('text') writes 'text' beneath the x-axis of a plot ylabel('text') writes 'text' beside the y-axis of a plot title('text') places a title at top of graphics plot legend(string1, string2, string3, ...) puts a legend on the current plot using the specified strings as labels. semilogx(x,y) plots log(x) vs y semilogy(x,y) plots x vs log(y) loglog(x,y) plots log(x) vs log(y) grid creates a grid on the graphics plot.

You might also like