Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

MATLAB Tutorials

1 Introduction
MATLAB stands for MATrix LABoratory

1.1 Environment
Location of the program C:\Program Files\MATLAB\R2012b\bin Familiarity with desktop: o Current folder: Set the operating folder and view its contents o Command window: Used as a calculator and execute bulk commands o Workspace: Variables in use o Command history: History of commands used in command window o Editor: Type and save scripts. Useful in debugging. Difference between 2012b and before version o App like interface from 2012b. Desktop like interface before 2012b

2 Variables, Scripts and Operations


2.1 Command window and Matrices
Variables and assignment: o Rules for variable naming o No need initialization of variables o In- built variable (pi, i, j, ans, Inf, NaN) o Right hand side is evaluated and store on left hand side Vector and matrix creation
row = [1 2 5.4 -6.6]; % Vector separated by space of commas a = linspace(0,10,5); % Using function b = 0:2:10; % Vector with 2 as increment c = 1:5; % Vector with 1 as increment column = [4;2;7;4]; % Column separated by semicolons a = [1 2; 3 4]; % Matrix creation

Concatenation and complex numbers


a d e f = = = = [1 2]; b = [3 4]; c = [5; 6]; [a; b]; % Vertical [d c]; % Horizontal [[e e]; [a b a]];

Matrix and array operations ( +, -, *, /, \, (transpose), ^(exponentiation))


7/45 % Division (1+i)*(2+i) % Complex number 4^2, (3+4*j)^2 % Exponentiation(^) ((2+3)*3)^0.1 % Complicated expressions, use parentheses a =[1 2 3];b=[4;2;1]; % Vectors a.*b', a./b', a.^(b') % Element operations c = [4 5 6]; a - c, a + c % Matrix Addition and subtraction 5*a

Array indexing and picking sub matrices


A = rand(5) % shorthand for 5x5 matrix A(1:3,1:2) % specify contiguous submatrix A([1 5 3], [1 4])

2.2 Calling Function


Calling inbuilt functions (clc, clear all, close all, max(), min(), max(:), mean(), std(),sqrt(), log(), cos(), round(), floor(), ceil()) Getting multiple outputs
vec = [5 3 1 9 7]; [minVal,minInd] = min(vec); ind = find(vec == 9); ind = find(vec > 2 & vec < 6);

Get help for functions (doc, pause, help sin)

2.3 Editor
Save and Run program: Use icons Copying from command history: Select and drag to editor Comments:

o % Comments in MATLAB starts with percentage.


o o Comments are not executed and just for user understanding Increases readability and clarity

2.4 Workspace
save myFile a b c % Save variables to file clear f load myFile % Load the variables

Watch data type, maximum, minimum Edit variables

3 Visualization and Programming


3.1 Plotting and excel
Simple plot ( plot(0:0.1:2*pi, sin(0:0.1:2*pi))) Annotation in figure (labels, title, color, font, save) More 2d plots: Many variables in a single plot (plot(x,y,x,z), hold on) Importing from excel Program annotation of figures 3d and other figures (plot3(), surf(), contour(), scatter(), pie, bar, stem)
x = -pi:.1:pi; y = sin(x); p = plot(x,y) set(gca,'XTick',-pi:pi/2:pi) set(gca,'XTickLabel',{'-pi','-pi/2','0','pi/2','pi'}) xlabel('-\pi \leq \Theta \leq \pi') ylabel('sin(\Theta)') title('Plot of sin(\Theta)') set(p,'Color','red','LineWidth',2)

3.2 Conditional and looping statements


Conditional statement (ifelseend) o Relational operators (equal (==), notequal (~=), greater than (>), less than ( <), greater or equal ( >=), less or equal (<=)) o Logical operators (and (&), or (|), not (~), Xor (xor)) Looping statement (for end, whileend)

3.3 Functions
Writing own functions Path of search
% Function to find the target function Position = plotSin(Vector) Position = 0; for Index = 1:length(Vector) if Vector(Index) == Target Position = Index; break end end

4 Other Possibilities
4.1 GUI
A simple GUI construction.

4.2 Toolboxes
What are toolboxes? Toolboxes contain more functions. Some toolboxes come with GUI. Easy use multicore processors and Graphical Processing Units. Using curve fitting toolbox. Sensors: Can connect variety of sensors with MATLAB. Few come with GUI. Mathematics: ODE, PDE, Statistics functions are available Data Access: Multiple importing options are available

4.3 Simulink
What is Simulink? It is an interactive graphical environment. Block diagram based MATLAB add-on environment. Design, simulate, implement, and test control, signal processing, communications, and other time-varying systems When to use it? Dynamic systems

5 Miscellaneous
Various programming paradigms: Compatible with object oriented, imperative and functional paradigms Can be used to develop software, standalone applications and add-ons. Symbolic representation

5.1 More help


Documentation: Demos, Examples and general help Mathworks website: Central, Webinars, blogs (https://www.mathworks.in/) MIT OCW (http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-094-introduction-tomatlab-january-iap-2010/index.htm)

5.2 Why MATLAB?


Though there are thousands of programming languages, MATLAB is widely used for scientific computing in industry and academia. Its enormous number of functions, visualization, matrix operations and toolboxes are the reasons for its widespread use. MATHEMATICA is as a contender for MATLAB. For open source and free ware, Octave can be used. Octave can run most of MATLAB programs.

5.3 MATLAB is often accused of slow speed?


Because of Interpretation Less speed than well-constructed C++ program Can be made fast with vectorisation, attaching C++ programs.

You might also like