230 likes | 250 Views
MATLAB DENC 2533 ECADD. LAB 9. OUTLINE. INTRO MATLAB SYSTEM MATLAB WINDOW MATLAB IN MATHEMATICAL OPERATION GRAPHIC ON MATLAB MATLAB EDITOR FILE (M-file) PROGRAMMING IN MATLAB. Introduction. MATLAB stand for MATrix LABoratory . High performance language for technical computing.
E N D
MATLABDENC 2533 ECADD LAB 9
OUTLINE • INTRO • MATLAB SYSTEM • MATLAB WINDOW • MATLAB IN MATHEMATICAL OPERATION • GRAPHIC ON MATLAB • MATLAB EDITOR FILE (M-file) • PROGRAMMING IN MATLAB
Introduction • MATLAB stand for MATrixLABoratory. • High performance language for technical computing. • It integrates computation, visualization and programming. • Typical uses in: • Math and computation • Algorithm development • Modeling, simulation and prototyping • Data analysis, exploration and visualization • Scientific and engineering graphics • Application development
MATLAB System • Development environment – command window, command history, editor, debugger, browsers for help, workspace file. • MATLAB mathematical function library – function like sum, sine, cosine and complex arithmetic, matrix. • MATLAB language – high level matrix/array language with control flow statements, functions, data structure, I/O. • Graphics – presentation graphics, image processing, data visualization. • MATLAB application program interface (API) – a library that allows to write C and FORTRAN programs that interact with MATLAB.
Starting MATLAB • When you start MATLAB, 4 windows appear by default: • MATLAB Command Window • Current Directory Window • Workspace Window • Command History Window
1) MATLAB Command Window • To communicate with MATLAB program, use MATLAB command window. • MATLAB display the prompt (>> ) to indicate that it is ready to receive instructions.
ii) Current Directory Window • It is much like a file manager window. • Used to access files • For example: file.m – it will open file in Matlab editor.
iii) Workspace Window • Display the variables created in the command window • Workspace refers to the names and values of any variable in use in the current work session.
Command History Window • All the previous instruction/programming entered in MATLAB command window showed here • You can double click and drag the command into the MATLAB command window or Matlab editor.
Entering Command and Expressions • Example of command s in Matlab Command Window: • >> x=[0:002:8]; • >> y=5*sin(x); • >> plot(x,y): • When you put semicolon (;) at the end of your command line, Matlab will not shows the answer of your commands/expressions. • Example of mathematical expressions in Matlab prompt without semicolon(;) : • >> 8/10 • ans = 0.8000 • >> 5*anw • ans = 4
Variables • A variable in Matlab is a symbol used to contain a value. • When we do not specify a variable name for a result, Matlab uses the symbol ans as a temporary variable containing the most recent answer. • Scalar variable: • >> r=8/10 r = 0.8000 • >> s=5*r s = 4 • >> 3 + 9 ans = 12
Variables (cont.) • Variable names must begin with a letter and must contain less than 32 characters; contain letter, digits, and underscore. • Matlab is case-sensitive, the following names represent different variables: speed, Speed, SPEED. • Special variables and built-in constants in matlab: • ans, i , j, inf, NaN, pi
Computing with MATLAB • Matlab able to handle collection of numbers, called array. • Example of array variable: • >> x= [0 1 3 6]; % 4-sequence of array • >> u= [0 1; 2 1]; % 2x2 matrix • >> y= [0:0.01:2]; • >> length (y); % check total number of sequence-y • >> y(7); % check the value of y in array-7 • Define polynomial x2 + 2x + 1, to find roots of the polynomial: • >> a= [1, 2, 1]; • >> roots (a) • Answer: roots of a: x= -1 , x= -1 • So, the polynomial a is (x+1)(x+1)
Computing with MATLAB (cont.) • Built-in and User Defined Functions
Matlab Scripting files & the Editor/Debugger • Matlab can perform operations in 2 ways: • Using Matlab Command Window • Using Editor/Debugger (.m file)
MATLAB Editor M-file (.m) • All program and function files are saved using Matlab editor. • Writing M-files will enhance your problem solving productivity since many MATLAB commands can be run from one file without having to enter each command one-by-one at the MATLAB prompt. This way, corrections and changes can be made in the M-file and re-run easily. • Using the MATLAB Script Editor (click FileNewM-file)
Plotting with Matlab/Graphics • Matlab contains many powerful functions for easily creating plots of several different types. • Try this session: >> x=[0:0.001:10]; >> y=sin(2*x); >> figure(1); >> plot(x,y); >> xlabel(‘paksi-x’), ylabel(‘paksi-y’); >> title(‘plot y=sin 2x’);
PROGRAMMING IN MATLAB • Flow control: • if statement • if statements evaluates a logical expression and executes a group of statements when the expression is true. • Eg: If A > B ‘greater’ elseif A < B ‘less’ elseif A == B ‘equal’ else ‘unknown error’ end
PROGRAMMING IN MATLAB (cont.) • for statement - for loop repeats a group of statements a fixed, predetermined number of times. • Eg: m = 0 for n = 3:32 m = m + n end m
PROGRAMMING IN MATLAB (cont.) • while statement • while loop repeats a group of statements an indefinite number of times under control of a logical condition. • Eg: a = 0 b = 10 while b > a b = b – 1; end a b