You are on page 1of 42

Mathworks

Matlab

Matlab
Matrix Laboratory

    


LINPACK EISPACK FORTRAN

Matlab
(>>) M-files Simulink   

Matlab
Matlab Workspace Variables/Current Directory
( Matlab )

Command History
( Matlab )

Command Window
(all Matlab versions)

Matlab
       Operators Matrices Plotting M-files Flow control Simulink Misc commands      Simulink  

a+b a-b a*b a/b a\b (bza) a^b

Operators

+ * / \ ^

( (

) )

     

L R L R L R
Operators

() ^ */ +-

/ /

    

>> 5/2*3+4 ans= 11. 11.5

>> 6*5 ans = 30 >> 17/5 ans = 3.4000 >> ans ans = 3.4000

 Matlab ans ans >>ans 

Operators

>> x=6*5 x= 30 >> y=17/5; >> y y= 3.4000 >> z=x+y z= 33.4000 >> who Your variables are: x y z

 (;)   

Operators

: >> x=6*5;y=17/5;z=x+y  >>who

:
>>case_34=6.45; >>case34=6.45;
Operators

:
34case, ge204!, Name Here case34, ge204, Name_Here 

Matlab
8 :
>> p = 3; >> p = 3.0;

  :   : 

Operators

>>number_123= >>number_123=7; >>text04='This >>text04='This is GE204'; GE204';

:
>> a = 4; >> A = 10; 10; >> clear a

A{ a

Matlab
        exp(x) sin(x) asin(x) log(x) log10(x) sqrt(x) abs(x) sum(x) ex sin x sin-1x ln x log x     pi i,j NaN Inf T imaginary unit Not-a-Number g

Operators

x
|x|

Matlab
: :
>>y=x^0.5; >>y=x^(1/2); >>y=sqrt(x);

Operators

y! x

Matlab Help
>>help

Operators

Matlab

Matlab Help
>> help sqrt

Operators

square root function help

Matlab Help

help
Operators

Matlab

>> A=[6 A= 6 3 >> B=[1 B= 1 2 >> C=[4

5; 3 4] 5 4 2 3 4] 3 4 8 7 5]; ; Enter Matlab   

[]

Arrays

(
>> A=[6 5; 3 4] A= 6 5 3 4 >> A(1,1)=10 A= 10 5 3 4 >> 1:2:7 ans= 1 3 5 7 >> 1:4 ans= 1 2 3 4

)
   : ( initial:step:final initial:step: : : (1 ) 

Arrays

* .* / ./ ^ .^ '(or transpose()) ^-1 (or inv()) length() size()

         

Arrays

4 5 1 2 3 2 3 4 A! B ! 5 6 7 C ! 6 7 4 5 6 8 9 >>D=A+B; >>D=A-B; >>D=A*C; >>D=C*A; >>D=A.*B; >>D=A+C; >>D=A*B; >>D=B*A; >>D=A.*C;

Arrays

)
Matlab

>> C=A\B; >> C=B/A;


Arrays

C!A B

1

C ! BA

1

Matlab >>C=A;
1 2 3 A! 4 5 6
Arrays

 C=AT

>>B=A; 1 4 B ! 2 5 3 6

1 2 3 A! 4 5 6

2 3 4 B! 5 6 7

>> D=[A B];


Arrays

1 2 3 2 3 4 D! 4 5 6 5 6 7

Matlab
>> >> >> >> >> >> x=0:.1:20; y=exp(0.1*x).*sin(x); plot(x,y) xlabel('Time (t) in Seconds') ylabel('Response in mm') title('A Simple 2-D Plot')

Matlab

 

Plotting

plot
>> n=0:11; >> y=sin((pi/6)*n);
! , y1, x1

>> plot(n,y)
Plotting

: >> help plot


plot(x1,y1[,x2,y2,x3,y3 plot(x1,y1[,x2,y2,x3,y3.....])

>> plot(n,y,'--')

subplot
subplot(m subplot(m,n,p)

Plotting

    
Plotting

title xlabel ylabel grid axis

       

 legend  figure  plot3

Matlab
Matlab Matlab :
Script files ( Function files (
M-Files

   

) )

.m

(M-files) M
:M-file
>>edit filename New > M-file

M-Files

Script Files
:script
) (test 

: >> test y = 9.0

>> x=3.0; >> y=x^2; >> y y = 9.0 >>


!Matlab

M-Files

script

function [list of outputs] = aaa(list of inputs) % function help . . . program . . . return ) Matlab !(

M-Files

cmb function [x,y]=cmb(a,b) %Test function Command x=a+b; Window y=a/b; return >> [j,k]=cmb(5,2) j=7 k=2.5 >>help cmb Test function

M-Files

a=2; b=3; c=a+b; d=sqrt(b);

Flow control

: ( )
if if-else while for ...

 

while
while expression statements end

if
if expression statements1 else statements2 end

if expression statements end

Flow control

Matlab  (expression)  Matlab (expression) (false) (true) Matlab (statements) 

(statements)

a=4; b=5; c=5; if a+b true 0 if b-c false 0 =


Flow control

== < > <= >= ~=

! if sin(0) false if sin(pi) true sin(pi) = 1.22e-16 & and | or while(3<=a)&(a<=5)

(if/if-else) if/if>> a=6; >> if a<10 b=a/2; end >> b b = 3


Flow control

>> a=50; >> if a<10 b=a/2; end >> b


??? Undefined function or variable 'b'.

>> a=6; >> if a<10 b=a/2; else b=a/5; end >> b b = 3

>> a=50; >> if a<10 b=a/2; else b=a/5; end >> b b = 10

(while)
>> a=1; >> while a<5 a=a+1 end a = 2 a = 3 a = 4 a = 5 >> a=10; >> while a<5 a=a+1 end >> a a = 10 >> a=1; >> while a<5 b=a+1 end b = 2 b = 3 b = 4 b = 5 b = 6 b = 7 . . . g loop! .

Flow control

for
!
for index = start : [increment :] end statements end

1
Flow control

(increment)

   

>

<

Flow control

>> for a=1:5 a end a = 1 a = 2 a = 3 a = 4 a = 5 >> for a=1:5 f(a)=a; end >> f 1 2 3 4 5 >>size(f) ans = 1 5

>> for a=10:5 a end >> a [] >> size(a) ans= 0 0

 sym/syms       diff int solve expand poly2sym pretty

sym) (

      

Misc. commands

       

clc clf format save/load Workspace pwd cd <drive:\folder> fprintf diary

       

Misc. commands

You might also like