Presentation is loading. Please wait.

Presentation is loading. Please wait.

MATLAB DENC 2533 ECADD LAB 9.

Similar presentations


Presentation on theme: "MATLAB DENC 2533 ECADD LAB 9."— Presentation transcript:

1 MATLAB DENC 2533 ECADD LAB 9

2 OUTLINE INTRO MATLAB SYSTEM MATLAB WINDOW
MATLAB IN MATHEMATICAL OPERATION GRAPHIC ON MATLAB MATLAB EDITOR FILE (M-file) PROGRAMMING IN MATLAB

3 Introduction MATLAB stand for MATrix LABoratory.
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

4 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.

5 Starting MATLAB When you start MATLAB, 4 windows appear by default:
MATLAB Command Window Current Directory Window Workspace Window Command History Window

6 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.

7 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.

8 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.

9 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.

10 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 = >> 5*anw ans = 4

11 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 = >> s=5*r s = 4 >> 3 + 9 ans = 12

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

13 Scalar Arithmetic Operations:
SYMBOL OPERATION MATLAB FORM + Addition: a + b a + b - Subtraction: a – b a – b * Multiplication: a b a*b / Right division: a/b a/b \ Left division: b\a b\a ^ Exponentiation: ab a^b

14 Commands for managing the MATLAB
Operation clc Clear the command window clear Removes all variables from memory clear var1 Remove the var1 quit Stop matlab who List the variables currently in memory whos List current variable and its size in memory ; (semicolon) Indicate the ends of the rows of a matrix or use after statement to suppress printing : (colon) used to pick out selected rows, columns and elements of vectors, matrices, and arrays , separator % comment

15 Computing with MATLAB Matlab able to handle collection of numbers, called array. Example of array variable: >> x= [ ]; % 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)

16 Computing with MATLAB (cont.)
Built-in and User Defined Functions Function MATLAB Syntax ex exp (x) √x sqrt (x) ln x log (x) log10 x log10 (x) cos x cos (x) sin x sin (x) tan x Tan (x) cos-1 x acos (x) sin-1 x asin (x) tan-1 x atan (x)

17 Matlab Scripting files & the Editor/Debugger
Matlab can perform operations in 2 ways: Using Matlab Command Window Using Editor/Debugger (.m file)

18 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 FileNewM-file)

19 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’);

20 Plotting with Matlab/Graphics (cont.)
Command MATLAB Operation figure Create a new graph/figure plot( x, y) Draw the graph title('text') Define a title for a graph xlabel('text') Define a x-axis for a graph ylabel('text') Define a y-axis for a graph grid Setting grid lines at graph stem (x,y) plot a graph in a discrete sequence format mesh (x,y,z) Plot graph in three dimensional plots

21 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

22 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

23 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


Download ppt "MATLAB DENC 2533 ECADD LAB 9."

Similar presentations


Ads by Google