You are on page 1of 48

66.

094
094
MATLAB
1 :
Sourav Deyy
Danilo epanovi

Ankit Patel
Patrick Ho


19.00 21.00
1.
2.
3.
4.


(visualization)
(fitting)
(advance)

/

3
.doc pdf

1)
2)
3)
4)



MATLAB

https://msca.mit.edu/cgi-bin/matlab


MATLAB
Windows
Wi d
START

Athena
>> matlab
>> matlab &



File ->> Preferences
MATLAB

Courtesy of The MathWorks, Inc. Used with permission

MATLAB
MATLAB
TI-83 ?
( )


MATLAB

MATLAB
who
MATLAB (workspace)

what
MATLAB MATLAB

why
help

MATLAB

1)
2)
3)
4)


MATLAB
(weakly type)

MATLAB
3.84

64-
a

16

(double)
(char)
: , ((symbolic)
y
) , 16 8


var1 = 3.14
myString = hello world


_
- ( ) var1 Var1

(built-in)

i j
pi 3.1415926
ans ( )
Inf Inf
Inf

(infinity)
NaN

Hello world

Hello wolrd
MATLAB
MATLAB
Hello 6.094

ans= disp()
disp('Hello 6.094')

sprintf()
class=6.094;
disp(sprintf('Hello %g', class))

(scalars)


a = 10


c = 1.3*45-2*a

, ;
cooldude = 13/3;

MATLAB
2
1.
2.

( )
(object) ()

MATLAB !
!


:
,
[]
row = [1 2 5.4 -6.6];
row = [1, 2, 5.4, -6.6];

Courtesy of The MathWorks, Inc. Used with permission.


[]
: ;
column = [4;2;7;4];

Courtesy of The MathWorks, Inc. Used with permission.



(element by element)
a= [1 2;3 4];



()
()
a = [1 2];
b = [
[3 4];
];
c = [5;6];
d = [a;b];
e = [d c];
f = [[e e];[a b a]];

save/clear/load

save

save myfile a b

a b myfile.mat
myfile.mat

\MATLAB\work


MyDocuments\6.094\day1


clear


clear a b

workspace a b

load




load myfile

workspace
p a b

save myenv; clear all; load myenv;

:

5 :

r 1 4 7 10 13
c 13 10 7 4 1
varEx

r=[1 4 7 10 13];
c=[13; 10; 7; 4; 1];
save varEx r c
clear r c
load varEx

1)
2)
3)
4)


(+, -, * ,/)

7/45
(1+i)*(2+i)
(1+i)
(2+i)
1/0
0/0

(Exponentiation) (^)
4^2
(3+4*j)
(3+4*j)^2
2


((2+3)*3)^0.1
((
) )


3(1+0.7) gives an error


Clc


MATLAB
-

sqrt(2)
log(2), log10(0.23)
cos(1 2) atan(
cos(1.2),
atan(-.8)
8)
exp(2+4*i)
round(1.4), floor(3.3), ceil(4.23)
angle(i); abs(1+i);

Help / Docs


:
help sin

Help


doc sin


doc

+ Search tab

lookfor hyperbolic

:
e^(i*x) = cos(x) + i*sin(x) x

x = pi/3;
a = exp(i*x)
b = cos(x)+ i*sin(x)
a-b


:


size

length

Transpose
transpose (

)
a = [1 2 3 4]
transpose(a)

.'

a.
a '

' Hermitian-transpose

a = [1+j 2+3*j]
a'
a.'

.' '



( )

(error)
c = row + column


transpose
c = row + column
c = row + column
column


s
s=sum(row);
sum(row);
p=prod(row);



t = [1 2 3];
f = exp(t);


f = [exp(1) exp(2) exp(3)];

help

(* / ^)

a=[1 2 3];b=[4;2;1];
a.*b, a./b, a.^b

all errors

a.*b,
a *b a
a./b,
/b a
a.^(b)
^(b)

all valid


(*)

((^)) *


(/ \)

:

(inner product) [1 2 3] [3 5 4]
a=[1 2 3]*[3 5 4]


b=[1 2 3].*[3 5 4]


c=log(b)


ones, zeros, rand
o=ones(1,10)

10 1
z=zeros(23,1)


23

0
(
( ))
r=rand(1,45)

45 uniform [[0,1]
,]
n=nan(1,69)

NaN

Number of rows

var=zeros(M,N);

Number of columns


linspace
a=linspace(0,10,5)

0 10 5

:
b=0:2:10

0 2 10


c=1:5

logspace
linspace

:
10,000 f(x) = e^{-x}*cos(x), x 0
10
x = linspace(0,10,10000);
f = exp(-x).*cos(x);


MATLAB 1
0

a(n) nth



x=[12 13 5 8];
a=x(2:3);
b=x(1:end
b=x(1:end-1);
1);

a=[13 5];
b=[12 13 5];




subscripts ( )
linear indices ( )

: subscripts linear indices

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




a=[-1 10 3 -2];
b=a([1 2 4;3 4 2]);

d=c(1
d=c(1,:);
:);
e=c(:,2);
c(2,:)=[3 6];

d=[12 5];
e=[5;13];
%replaces second row of c

2
MATLAB



vec = [1 5 3 9 7]


[
[minVal,minInd]
,
] = min(vec);
(
);


[maxVal,maxInd] = max(vec);


ind = find(vec == 9);
ind
i d = find(vec
fi d(
> 2 & vec < 6);
6)

find



ind2sub
sub2ind

help

:
0 2*pi

1,000


55
100 110

:
:


-0.001 0.001

x = linspace(0,2*pi,1000);
y = sin(x);
y(55)
y(100:110)
[minVal,minInd]=
[
,
] min(y)
(y)
[maxVal,maxInd]= max(y)
inds = find(y>-0.001 & y<0.001)

:
3x100 x 100 0 10
mat=zeros(3,100);
x=linspace(0,10,100);
x=linspace(0 10 100);

cos(x)
mat(1,:)=cos(x);
mat(1 :)=cos(x);

log((x+2)^2)
mat(2,:)=log((x+2).
mat(2 :)=log((x+2) ^2);
2);


mat(3,:)=rand(1,100);
mat(3 :)=rand(1 100);

sum
rs = sum(mat
sum(mat,2);
2);
cs = sum(mat); % default dimension is 1

1)
2)
3)
4)



x=linspace(0,4*pi,10);
y=sin(x);


plot(y);

y x
plot(x,y);
plot(x y);

MATLAB

(plot) ?
plot (x,y)

x=linspace(0,4*pi,1000);
plot(x,sin(x));

x y

error
plot([1 2], [1 2 3])

MATLAB
1
0
1

10 x values:

1000 x values:

0.8

0.8

0.6

0.6

0.4

0.4

0.2

0.2

-0.2

-0.2

-0.4

-0.4

-0.6

-0.6

-0.8

-0.8

-1

-1
0 2 4 6 8 10 12 14

0 2 4 6 8 10 12 14



(marker)

plot(x,y,k.-);


plot(x,y,
plot(x y .)
)

help plot :


2

hold on;


figure;
plot(x,y);

GUI

:
f(x) = e^x*cos(x) x =[0 10]

x=0:.01:10;
plot(x,exp(x).*cos(x),r);

1
1)
2)
3)
4)

You might also like