Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
33 views

Matlab: BY K. Kalyan Kumar

gives a brief introduction to MATLAB and helps to learn how to write a simple programs in MATLAB for beginners

Uploaded by

kkk212
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Matlab: BY K. Kalyan Kumar

gives a brief introduction to MATLAB and helps to learn how to write a simple programs in MATLAB for beginners

Uploaded by

kkk212
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

MATLAB

BY K. KALYAN KUMAR

Strengths of MATLAB
2

MATLAB is relatively easy to learn


MATLAB code is optimized to be relatively

quick when performing matrix operations MATLAB may behave like a calculator or as a programming language MATLAB is interpreted, errors are easier to fix

MATLAB Desktop
3

MATLAB Desktop - contd


4

Command Window type commands Workspace view program variables clear to clear
clear all: removes all variables, globals, functions and MEX links clc: clear command window

double click on a variable to see it in the Array Editor

Command History view past commands

Command window
5

The MATLAB environment is command oriented

somewhat like UNIX. A prompt(>>) appears on the screen and a MATLAB statement can be entered. When the <ENTER> key is pressed, the statement is executed, and another prompt appears. If a statement is terminated with a semicolon ( ; ), no results will be displayed. Otherwise results will appear before the next prompt.

Command Window --contd


6

a=5;

b=a/2

b=

2.5000
2*5

ans= 10

All text after a percent sign (%) is taken as a comment

statement. To place multiple commands in a single line, they should be separated by commas or semicolons. Commas tell MATLAB to display results; semicolons suppress printing. Statement continuation is done by three periods in succession. Ex: pens=5,pencost=10; total_cost=pens* pencost

MATLAB Variable Names


8

Variable names ARE case sensitive

Variable names can contain up to 63 characters (as

of MATLAB 6.5 and newer) Variable names must start with a letter followed by letters, digits, and underscores. Punctuation characters are not allowed.

Reserved Words
9

Matlab has some special (reserved) words that you

may not use, for example,


for end if while function return elseif case otherwise switch continue else try catch global persistent break

MATLAB Special Variables


10

ans pi inf NaN i and j

Default variable name for results

Value of
Infinity Not a number e.g. 0/0

i = j = square root of minus one: (-1) (imaginary number) e.g. sqrt(-1) ans= 0 + 1.0000i The smallest usable positive real number
The largest usable positive real number

realmin realmax

MATLAB Math Operators


11

Power Multiplication Division or NOTE: Addition Subtraction Assignment

^ or .^ a^b or a.^b * (matrix multiply) or .* (array multiply) a*b or a.*b / or ./ a/b or a./b \ or .\ b\a or b.\a 56/8 = 8\56 + a + b a - b = a = b (assign b to a)

Script M-files:
12

Click on the page icon

The Matlab text editor window will open

13

Excecution of Script M-file:


14

choosing Save & Run button from the Debug menu

Pressing the Save & Run button on Editor toolbar


Simply pressing the function key F5 Alternatively, you can save the file example1.m on

your disk by choosing Save from the File menu; then at the MATLAB prompt typing the name of the file without .m extension and press enter key

Basic functions and their description:


15

Function
Beep disp(variablename) Input

Description
Makes computer beep displays results without

identifying variable names prompts user for input


Temporarily gives control to

keyboard

pause(n)

key board Pauses for n seconds

Arrays :
16

Array can be created in the following ways:

X=[ 1 6 8] creates a row vector


X=first:last creates row vector starting with first,

counting by 1, ending at or before last X=first:increment:last creates row vector starting with first, counting by increment, ending at or before last X=linspace(first,last,n) creates linearly spaced row vector starting with first, ending at last having n elements

Examples:
17
>> x=[1 6 8]

x=

>> x=2:5 x= 2 3 4 5 >> x=2:0.5:5 x= 2.0000 2.5000 3.0000 3.5000 4.0000 4.5000 5.0000 >> x=linspace(1,10,10) x= 1 2 3 4 5 6 7 8 9 10 >> x=linspace(0,10,10) x= Columns 1 through 9 0 1.1111 2.2222 3.3333 4.4444 5.5556 6.6667 7.7778 8.8889 Column 10 10.0000

Dot-transpose operation:
18
>> A=[1 2 3 4]; >> B=complex(A,A)

B=
1.0000 + 1.0000i 2.0000 + 2.0000i 3.0000 + 3.0000i 4.0000 + 4.0000i >> C=B' C=

1.0000 - 1.0000i 2.0000 - 2.0000i 3.0000 - 3.0000i 4.0000 - 4.0000i


>> D=B.' D= 1.0000 + 1.0000i 2.0000 + 2.0000i 3.0000 + 3.0000i 4.0000 + 4.0000i

Standard Arrays:
19

>> zeros(2,3) ans = 0 0 0 0 0 0 >> ones(2,3) ans = 1 1 1 1 1 1 >> eye(3) ans = 1 0 0 0 1 0 0 0 1

20

>>A=rand(3) A= 0.9649 0.9572 0.1419 0.1576 0.4854 0.4218 0.9706 0.8003 0.9157 >> diag(A) ans = 0.9649 0.4854 0.9157

Array operations:
21

>> A=[1 2 3; 4 5 6] A= 1 2 3 4 5 6 >> B=[4 5 6;3 2 1] B= 4 5 6 3 2 1 >> A+B ans = 5 7 9 7 7 7 >> A*B ??? Error using ==> mtimes Inner matrix dimensions must agree. >> A.*B ans = 4 10 18 12 10 6

MATLAB Relational Operators


22

MATLAB supports six relational operators.

Less Than Less Than or Equal Greater Than Greater Than or Equal Equal To Not Equal To

< <= > >= == ~=

MATLAB Logical Operators


23

MATLAB supports following logical operators.

not and or Scalar AND Scalar OR

~ & | && ||

MATLAB Flow Control


The while and if statements

while expression statements end

if expression statements end

if expression statements1 else statements2 end

Matlab evaluates expression as logical true or false false equivalent to zero true equivalent to any non-zero number

statements, any valid Matlab command

MATLAB Flow Control


The for statement for index = start : increment : end statements end index, start, increment, and end do not need to be integer valued increment is optional, if increment is not specified increment defaults to 1

index can be incremented positive (increment > 0) or negative (increment < 0)

loop stops when index > end (or index < end)

26

Function files:
27

Matlab identifies function files from script files by

using the function keyword

the name of the function file must be the same name as the function

28

>> [a,v]=rectangle123(3,4,6) a= 12 v= 72

29

30

You might also like