You are on page 1of 18

MATLAB Lecture Notes

by Omer Goralnik

2
a=[ 32 4 81 exp(2.5) 63 cos(pi/3) 14.2]; :
m=[1 2 3; 4 5 6]; :
:
;b=1:2:10:
) - :: (
b=(15:-5:-25)' ; :
" linspace
z=zeros(n,m) ; :
n
m
" z1=ones(5) :
z=zeros(3,5)+2 ; :
; z1=ones(5)*4
:

; )i=eye(4

8 : 2
])B=[linspace(1,25,9); linspace(72,24,9); linspace(0,1,9

];B1=[1:3:25;72:-6:24;0:0.125:1
)b=a([1,3],1:3
1 3 1 3
16
Q16 %%
;(A=ones(3
;B=ones(2)*5
;[A=[A zeros(3,2);zeros(2,3) B

3
1 3
;] u=[ 4 9 5
;]v=[-3 6 -7
'A=u*v
)B=dot(u,v
"
: dot
" )cross(a,b
a.*b a./b a.^3
3 3
;g=9.81
;]t=[1:10
d=0.5*g*t.*t

: ) rand(3,2 01-
)5+3*rand(3,4
=
. )(
)( randn 0 68% 1+-
DataAnalysis
" )(plot
matlab
)( max
[x,dx]=max( ): x dx
" ) (min
) ( Mean
) ( Std )=(
14
;v=250
;h=5:9
r=(3*v/pi./h).^0.5
s=pi.*r.*(r.^2+h.^2).^0.5
) ( char ASCII
-char Y=['qwd';'wdx';'ryw'] :
) Y(2,: .

4
:1
) va=A(1,: . A
) reshape(A,m,n A ) m*n (
) reshape(A(2,:)',m,n A . m*n
, 2:2:14 2- 14 . 2
"" B2(2,":)-[] : "" . B2
15 2 reshape
reshape(1:35,7,5)' : ' .
) (:4
% 3x1 +5x2 + 2x3 =3
% x
+7x3=2
%
x2
+3x3=5
];A=[3 5 2; 1 0 7 ;0 1 3
']b=[3 2 5
A\b
% check: A*x=b
A\b " ' "'/
) ( : plot ) plot(y X
;)x=linspace(0,2*pi,30
;)y=sin(x
;)plot(x,y

1
0.8
0.6
0.4



default-
Grid

)'*' plot(x,y, *

0.2
0
-0.2
-0.4

)( .

-0.6
-0.8

-1

1
0.8

;)'plot(x,y,'-*r

0.6
0.4

0.2
0
-0.2
-0.4
-0.6
-0.8

-1

;f=@tanh
;)]fplot(f,[-2,2
f matlab tanh fplot
- 2- 2

) (: fplot

0.8
0.6
0.4
0.2
0
-0.2


)fplot(f,[-2,2],'o',10
)' '(: title ) ( plot

) : legend(' ',' ', ) ') (( plot


;)x=linspace(0,2*pi,30
sine and cosine
;)y=sin(x
1
sin
;)z=cos(x
0.8
cos
;)'plot(x,y,x,z,'-Or
0.6
grid
0.4
;)'title('sine and cosine
0.2
;)'legend('sin','cos
-0.4
-0.6
-0.8

1.5

0.5

-0.5

-1

-1.5

-1
-2

0
-0.2
-0.4
-0.6
-0.8

-1

"" .
" )legend('sin','cos',2
2 .
) ( : subplot figure ) subplot(2,2,1 4 2X2
' ) (plot

5
matlab plot figure plot
figure hold
Holdoff hold
:
) (hist
y=randn(50,1) :
)hist(y
.
)'*'plot(y,
:
2
50
1.5
hist


) ( Semilog

6
4
2
0

5
4
3

50

45

40

35

30

25

20

15

10

-1.5

0.5

-1

-0.5

10

-2

2.5

1.5

0.5

-0.5

-1

-1.5

0
-2.5

-2

) ( semilogy y-
" ) (semilogx
:
;x=0:.1:10
;y=10.^x
;)semilog(x,y

-2
-4

-6
-8

250

200

150

100

50

-10


) ( Plolar
0.5

90
120

60
0.4

)x=linspace(0,2*pi
)y=sin(2*x).*cos(2*x
)polar(x,y

0.3
30

150
0.2
0.1
180

210

330

240

300
270

14 5
;V1=12; R=4; L=1.3
;t1=0:.1:0.5
;t2=0.5:.1:2

2.5

1.5

0.5

;))i1=V1/R*(1-exp((-R*t1)/L
)i2=exp((-R*t2)/L)*V1/R*(exp((0.5*R)/L)-1
)'plot(t1,i1,'-*',t2,i2,'-*b

) function[out1,out2,]=Fun_name(in1,in2, out i in i
:
)function[m,s]=stat1(x
;)n=length(x
;m=sum(x)/n
))s= sqrt(sum((x-m).^2/n
) (.

1 6
:
)function[cm,kg]=STtoSI(in,lb
;cm=in*2.54
;kg=0.45359237*lb
:
)[cm1,kg1]=STtoSI(12*5+10,175
)me_si=STtoSI(5.9,150
:

6
: *.
!!!

:
* 2e3 2000
4 :6
...
)function [x,y]=maxmin(a,b,c
;)x=-b/(2*a
;y=a*x^2+b*x+c
...
%% Q4 CH6
)[manmin_x1 maxmin_y1]=maxmin(3,-18,48
)[maxmin_x2 maxmin_y2]=maxmin(-5,10,-3
:
-
x<y true false command window 1 0
==
=<

& : )(and
| : )(or
~ not :
)( '-'0 - '1'-

"
) a(a<b a a<b
) A<(B+1 B 1 .
A&B A B A B
0

7
: if -
:
;x=5
)if(x<10
x=x+1
end
10<X
end
:
))if((0<x)&&(x<100
)'disp('0<x<100
else
)'disp('x<0 OR X>100
end

)if and(0<x,x<100
)'disp('0<x<100
else
)'disp('x<0 OR X>100
end
elseif if end
if
:
;n=4
)if (n<=0
)'disp('n is negative or zero
)elseif (rem(n,2)==0
)'disp('n is even
m=n+1
else
)'disp('n is odd
end
:rem rem(5,2)=1 : 5 2 1
) rem(n,2 1 n 0 -.
8 :7
:
)function[]=quadroots(a,b,c
;D=b^2-4*a*c
)if(D>0
)'disp('The equation has two roots
x1=(-b+D^.5)/2/a
x2=(-b-D^.5)/2/a
)elseif(D==0
)'disp('The equation has one root
x=-b/2/a
else
)'disp('The equation has no real root

end
:
quadroots(2,8,-3)
quadroots(15,10,5)
quadroots(18,12,2)
: :input
m=input('choose methos:','s')
string :lower
: , if : case -
m=input('choose method:','s')
switch lower(m)
case{'linear','bilinear'}
disp('Method is linear')
case 'cubic'
disp('Method is cubic')
case 'nearest'
disp('Method is nearest')
otherwise
disp('Unknown method')
end
. method method
: for
:
for i=1:10
disp(i)
end
:
for i=1:2:10
disp(i)
for t=2:4
disp('*')
end
end
:
for i=1:2:10
disp(i)
for t=2:4
disp('*')
end
if i==7, break, end
end
. i=7-

: : while
n=50; p=1; f=0;
while (p<n)
f=f+1
p=2*p
end

if
otherwise case switch
while for

matlab" file>import data txt


ctrl+F find&replace


) max(T .
) mean(T .
) (help
) std( T .T-
:
:

.
) ( median ) ( .
) ( cov a11
a12
) ( corrcoef
) ( diff
d=diff(T(:,2)) : m-1 T
) ( sort ) (help
:
))[ds,di]=sort(T(:,1
)'[Ts,ti]=sort(T,'descend
ds di .
) magic(n n
matlab NaN-not a number .
:
)X=magic(3
X(2,2)=NaN
) x(2,2
) ( find
:
))i=find(~isnan(X
NaN

:

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8
-1

;x=0:pi/10:2*pi
;)y=sin(x
)plot(x,y

) ( polyfit n
:
)p=polyfit(x,y,3
) ( polyval p
:
)y1=polyval(p,x
:
;x=0:pi/10:2*pi
;)y=sin(x
)'plot(x,y,'-o
)p=polyfit(x,y,3
)y1=polyval(p,x
)'plot(x,y,'-*',x,y1,'--

1
0.8
0.6
0.4
0.2
0
-0.2
-0.4
-0.6
-0.8

-1

1 : 8
;x=-2:0.1:2
]p=[1.5 -5 1 2
;)y=polyval(p,x
)plot(x,y

5
0
-5
-10
-15
-20
-25
-30

1.5

0.5

-0.5

-1

-1.5

-35
-2


) polyder(p p p .

:
;)dy=diff(y)./diff(x
;)dx=x(1:end-1
:
1
0.8
0.6
0.4
0.2
0

;x=0:pi/10:2*pi
;)y=sin(x
;)dy=diff(y)./diff(x
;)dx=x(1:end-1
)plot(x,y,dx,dy

-0.2
-0.4
-0.6
-0.8


n-1

-1

y x
dx end-1

9
:
9 6 3.5 2.5 1.4 .7 -1 -3 -8 -7 -2 2

;a=20:10:100
;)]x=single([a a*10 a*100
y=single([77 66 59 54 49 46 43 40 38 22 14
; )]7 9 11 12
;)'plot(x,y,'-o

) ( semilogx



10^x
:
(semilogx
'x,y,;)'o
;grid on

80
70
60
50
40
30
20
10
0

9000 10000

8000

7000

6000

5000

4000

3000

2000

1000

-10

0
80
70
60

)(

50
40

30

interp1(x,y,
2.5e3,'linea
) ' r
x,y


2.5*10^3

interp1(x,y,

20
10
0

10

10

10

-10
1
10

2.5*10^3 x,y 2.5e3,'cubic' )


2.5*10^3 ' ' x,y interp1(x,y,2.5e3,'spline' )
:
semilogx(x,y,'-o',2500,y1,'*',2500,y2,'*',2500,y3,'*');
grid on;

-2

-4

-6

-8

:
1000-4000

-10

3.3

3.4

10

3.5

10

10

x_inter=linspace(1000,4000,1000);
Y1=interp1(x,y,x_inter,'linear');
Y2=interp1(x,y,x_inter,'cubic');
subplot(1,2,1)
semilogx(x(:,1:18),y(:,1:18),x_inter,Y1,x(:,21:27),y(:,21:27));
legend('y','linear interpolation','y');
title('linear');
grid on
subplot(1,2,2)
semilogx(x(:,1:18),y(:,1:18),x_inter,Y2,x(:,21:27),y(:,21:27));
legend('y','cubic interpolation','y');
title('cubic');
grid on

linear

c ubic

80

80
y
linear interpolation
y

70

70

60

60

50

50

40

40

30

30

20

20

10

10

-10
1
10

10

10

y
cubic interpolation
y

10

-10
1
10

10

10

10


:
;x=0:1:3
;y=0:1:5
z=-[100 99 100 100; 90 90 96 95; 99 100 90 95;...
;]100 95 95 90;86 95 90 100; 90 95 90 92
;)mesh(x,y,z
;)']xlabel('x[km]'),ylabel('y[km]'),zlabel('depth[m

meshgrid

:
xi=0: .1
;:3
yi=0: .1
;:5
[xj,yj]=m
eshgrid(x
;)i,yi
xj

xi 31
yi
51
xj

51X31

xj

yj
51X31

yi

-85

-90
]depth[m

-95

-100
6
3

4
2
2

1
0

]x[km

]y[km

:
;xi=0: .1 :3
;yi=0: .1 :5
;)[xj,yj]=meshgrid(xi,yi
)'zj=interp2(x,y,z,xj,yj,'cubuc
;)mesh(xj,yj,zj
-85

-90

-95

-100

-105
6
3

4
2
2

1
0

) contour(x,y,z -
-

8
6
4

2
0
0
-2
-4
-6

-1

-8
4
4

-2

2
2

0
0
-2

-2
-4

-4

-1

-2

-3
-3

10
) ) ( humps (.
h1=@hamps h1 humps
) h2=@(x)(x.^2 h2 x

y=x 2

) ] fplot(function,[a b ]) [a,b (
h1 :
100
90
80
70
60
50
40
30
20

(fplot
h1,[0
)]1

10
100
90

20

25

15

10

80
70

60
50
40
30
20
10

0.9

0.8

0.7

0.6

0.5

0.4

0.3

0.2

0.1

:
;a=5; b=5
;h=@(x1,x2)a*x1.^2-b*x2.^2
;)[X,Y]=meshgrid(-1:.1:1,-1:.1:1
;)Z=h(X,Y
;)surf(X,Y,Z

1 : 9
;)t=linspace(0,20,100
;)x=(2+4*cos(t)).*cos(t
;)y=(2+4*cos(t)).*sin(t
;z=t.^2
;)plot3(x,y,z

(fminbnd
][a,b

) function,a,b

) fminsearch(fun,s0 fun s0
) fzero(@fun,x0 fun x0
1 10
h4=@(x)cos(x)-2*x.^3
)x=fzero(h4,0

y = a0 + a1 x1 + a2 x 2
a1 , a2 , a3 x y

; ']x1=[.2 .5 .6 .8 1 1.1
; ']x2=[.1 .3 .4 .9 1.1 1.4
; ']y=[.17 .26 .28 .23 .27 .24
;]X=[ones(size(x1)) x1 x2
;a=X\y

a a1 , a2 , a3

11

) cumtrapz(x,y
: .
:
;x=0:pi/100:2*pi
;y=0:pi/3:2*pi
;)'plot(x,sin(x),y,sin(y),'-o
;))w=cumtrapz(x,sin(x
;))u=cumtrapz(y,sin(y
w
x
u-"

1
0.8
0.6


201
7

0.4
0.2
0
-0.2
-0.4
-0.6
-0.8

-1

) quad(f,a,b f a,b
10 10
;))f=@(x)(1./(0.8*x.^2+0.5.*x+2
)quad(f,0,5
:
=ans
0.8774

:
) ( sym
:
) a=sym(2)/sym(5 )z=sym(2/5

syms a b c x a,b,c,x
:
;)'x=sym('x
;)'a=sym('alpha
;)'rho=sym('(1+sqrt(5))/2
) simplify(f
:
syms a b c x
g=a*x^2+b*x+c
f=2*a*x^2+2*b*x+c
)h=simplify(f-g
:
= h
a*x^2+b*x
) subs(f,a f a

:
;f1=2*x^2-3*x+1
)w1=subs(f1,2
:
= w1
3
:
syms x y
;)f2=x^2*y+5*x*sqrt(y
)w3=subs(f2,x,3
)}w4=subs(f2,{x,y},{3,4
:
= w3
)9*y+15*y^(1/2

= w4
66
:
) diff(f,a a f
:
syms x
)f=sin(5*x
)fd=diff(f
:
=fd
)5*cos(5*x

:
syms s t
)f=sin(s*t
)diff(f,s,t
= ans
cos(s*t)*s
:
) int(f,x x
:
syms n x
f=x^n
)int(f,x
= ans
)x^(n+1)/(n+1
:
;)g=sin(2*x
)int(g,x,0,pi/2
) g(x 0- /2-

You might also like