Professional Documents
Culture Documents
http://math.ntut.edu.tw
...........................................................................................................1
.............................................................................................1
.............................................................................................2
.............................................................................................3
.............................................................................................................4
.....................................................................................................5
.....................................................................................................8
.....................................................................................................9
...........................................................................................12
...........................................................................................14
...............................................................................................16
...................................................................................................20
...........................................................................................23
...................................................................................................29
...............................................................................................29
Matlab
Matlab
Matlab
Matlab
Matlab
Matlab Matlab
">>" "?" Enter
1>>(7*3+5.5)/10
ans = 2.6500
Matlab
";"
>>(7*3+5.5)/10;
>> "ans"
ans = 2.6500
>>n = (7*3+5.5)/10
n =2.6500
MATLAB
help
clc
clf
clear
lookfor
display
input
display(Hello)
a=Input(a)
name=Input(,s)
load
rand
fix
0~1
round
fix(4.56)
ans=4
round(4.56)
ans=5
"," ";"
3>>n = (7*3+5.5)/10; p = n ^ 5
p =130.6861
"..."
4>>(7*3+5.5)/10*...
50^5
ans = 828125000
"%" Matlab
5>>m = (7*3+5.5)/ 10
m = 2.6500
% m
ij
eps
inf
nan NaN
pi
Not A Number
pi = 3.1415926...
Matlab
1>> y=sin(50)*exp(-0.9*2^3)
y = -1.9589e-004
2
abs(x)
x x
angle(z)
sqrt(x)
real(z)
imag(z)
conj(z)
round(x)
fix(x)
floor(x)
ceil(x)
rat(x)
rats(x)
sign(x)
(Signum function)
x<0sign(x) = -1
x=0sign(x) = 0
x>0sign(x) = 1
rem(x,y)
x y
gcd(x,y)
x y
lcm(x,y)
x y
exp(x)
pow2(x)
log(x)
log2(x)
log10(x)
10
sin(x)
cos(x)
tan(x)
asin(x)
acos(x)
atan(x)
sinh(x)
cosh(x)
tanh(x)
asinh(x)
acosh(x)
atanh(x)
mvnrnd(mu,sigma,nobs)
factor
: factor(x^2-1)
ans = (x-1)*(x+1)
Matlab
plot plot
y=sin(x) y=cos(x)
>> x=linspace(0, 2*pi, 50);
>> plot(x, sin(x), x, cos(x));
plot
--
plot
plot
>> x=linspace(0, 2*pi, 50);
>> plot(x, sin(x),'r.',x, cos(x),'k*');
>> legend('y = sin(x)','y = cos(x)');
>> text(4,0.6,'sin cos ')
%
%text
xlabelylabeltitle
legendgrid ontext
% x
% y
%
%
Matlab
polar ( )
>> theta=linspace(0, 2*pi);
>> r=1+cos(theta);
>> polar(theta, r);
z = x 3 + y 3 3 xy
>> [x,y]=meshgrid(-2:0.1:2);
>>z=x.^3+y.^3-3*x.*y;
>>mesh(x,y,z)
z = x 3 + y 3 3 xy
>> [x,y]=meshgrid(-2:0.1:2);
>>z=x.^3+y.^3-3*x.*y;
>>plot3(x,y,z)
surf mesh :
z = x 3 + y 3 3 xy
>> [x,y]=meshgrid(-2:0.1:2);
>>z=x.^3+y.^3-3*x.*y;
>>surf(x,y,z)
[x,y]=meshgrid(-2:0.1:2);
z=x.^2-y.^2;
mesh(x,y,z)
10
%contour(),contour3()
[x,y]=meshgrid(-2:0.1:2);
z=x.^2-y.^2;
contour3(x, y, z, 30);
11
1 2 3
4 5 6
7 8 9
Matlab >>
";"
6>>A = [1 2 3; 4 5 6; 7 8 9];
>>A
:
A=
1 2 3
4 5 6
7 8 9
7
>> B=[1 3
5 7]
:
B=
1 3
5 7
12
ones(3,4) 1 3 4
eye(3)3
A(2,3) 2 3
A(2,:) 2
A(:,3) 3
Matlab
+
-
*
/
\
^
'
8
>>B'
ans =
15
37
>>B*B
ans =
16 24
40 64
>>B^3
ans =
856576 1353216
2255360 3563008
9
>>C=[7 9;11 13];
>>B/C
ans =
2.5000
1.5000
-1.5000
-0.5000
>>B\C
ans =
-2 -3
3 4
13
Matlab "i""j"
y=2+3i
Matlab rand
10 rand(n) rand(m,n) nn mn
0 1
>>rand(3)
ans =
0.9501 0.4860 0.4565
0.2311 0.8913 0.0185
0.6068 0.7621 0.8214
A=[1 3;2 4]
A= 1
2
3
4
A'
ans =1
3
2
4
det(A)
ans =-2
inv(A)
ans =-2.0000
1.0000
1.5000
-0.5000
A^2-2*A+eye(2)
ans =6
6
9
15
eig(A)
ans =-0.3723
5.3723
[P,D]=eig(A)
P =-0.9094 -0.5658
0.4160 -0.8246
D = -0.3723
0
0
5.3723
A.^2
A^2
14
inv(A)
A inverse
det(A)
log(A)
log
eig(A)
sum(A)
sum(sum(A))
std(A)
excel
MATLAB toolbox exlink
EXCEL putmatrix,
MATLAB ()
MATLAB
MATLAB b=a+1( b 1)
EXCEL getmatrix,
15
Matlab
syms x y a t h;
%syms
limit(sin(x)/x)
ans =1
limit((x-2)/(x^2-4),2)
ans =1/4
limit((1+2*t/x)^(3*x),x,inf)
ans =exp(6*t)
16
limit(1/x,x,0,'right')
ans =inf
limit(1/x,x,0,'left')
ans =-inf
limit((sin(x+h)-sin(x))/h,h,0)
ans =cos(x)
v = [(1 + a/x)^x, exp(-x)];
limit(v,x,inf,'left')
ans =[exp(a), 0]
syms x y; %syms
4
3
% y=3x +2x -x+7
diff('3*x^4+2*x^3-x+7')
ans=12*x^3+6*x^2-1
%y=(x3+x2+2)(2x3-x2+3)
diff('(x^3+x^2+2)*(2*x^3-x^2+3)')
ans=(3*x^2+2*x)*(2*x^3-x^2+3)+(x^3+x^2+2)*(6*x^2-2*x)
% y= x / x1/2
diff('x/sqrt(x)')
ans=1/2/x^(1/2)
% y= sec4(x)
diff('sec(x)^4')
ans=4*sec(x)^4*tan(x)
% z=3*(1-x)^2*exp(-(x^2)-(y+1)^2)-10*(x/5-x^3-y^5)*exp(-x^2-y^2)
-1/3*exp(-(x+1)^2-y^2)
>> [x,y]=meshgrid(-4:0.1:4);
>> z=3*(1-x).^2.*exp(-(x.^2)-(y+1).^2)-10.*(x./5-x.^3-y.^5).*exp(-x.^2-y.^2)
-1/3.*exp(-(x+1).^2-y.^2);
>> mesh(x,y,z)
17
>> syms x y
>> z=3*(1-x).^2.*exp(-(x.^2)-(y+1).^2)-10.*(x./5-x.^3-y.^5).*exp(-x.^2-y.^2)
-1/3.*exp(-(x+1).^2-y.^2);
z
>> diff(z,x) ( )
x
ans = -6*(1-x)*exp(-x^2-(y+1)^2)-6*(1-x)^2*x*exp(-x^2-(y+1)^2)
-(2-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-2)^2*exp(-x^2-(y+1)^2)
+200*y^3*exp(-x^2-y^2)-200*y^5*exp(-x^2-y^2)
+2*(2*x-10*x^3-10*y^5)*exp(-x^2-y^2)
-4*(2*x-10*x^3-10*y^5)*y^2*exp(-x^2-y^2)
+2/3*exp(-(x+1)^2-y^2)-4/3*y^2*exp(-(x+1)^2-y^2)
>> diff(diff(z,y),x)
Matlab int
syms x y; %syms
int('x^2+1','x',0,1)
ans =4/3
int('1/sqrt(4-x^2)','x',0,1)
ans =1/12*4^(1/2)*pi
syms x y; %syms
int(x)
ans=1/2*x^2
int(sin(x))
ans=-cos(x)
syms x y; %syms
taylor(1/(1-x))
ans= 1+x+x^2+x^3+x^4+x^5
taylor(exp(x))
ans= 1+x+1/2*x^2+1/6*x^3+1/24*x^4+1/120*x^5
taylor(sin(x))
ans= x-1/6*x^3+1/120*x^5
taylor(cos(x))
ans= 1-1/2*x^2+1/24*x^4
taylor(cos(x,10))
19
ans= 1-1/2*x^2+1/24*x^4-1/720*x^6+1/40320*x^8
taylor(log(1+x))
ans= x-1/2*x^2+1/3*x^3-1/4*x^4+1/5*x^5
taylor(exp(-x))
ans=1-x+1/2*x^2-1/6*x^3+1/24*x^4-1/120*x^5
taylor(log(x),6,1)
ans=x-1-1/2*(x-1)^2+1/3*(x-1)^3-1/4*(x-1)^4+1/5*(x-1)^5
taylor(sin(x),pi/2,6)
ans=1-1/2*(x-1/2*pi)^2+1/24*(x-1/2*pi)^4
Matlab
DOS
MATLAB mm
MATLAB
function
MATLAB M
M-File
Editor
M
1. MATLAB
2.
3.
M sample1.m
%()
clear all
hold
a=[0 0 0.5 0 0.27 0;-0.139 0.263 0.57 0.246 0.224 -0.036;0.17 -0.215 0.408 0.222
0.176 0.0893;0.781 0.034 0.1075 -0.032 0.739 0.27];
v=0;
w=0;
for l=1:10000
r=rand;
if r<0.1
k=1;
elseif r<0.2
k=2;
elseif r<0.3
k=3;
else
k=4;
end
20
z=v*a(k,1)+w*a(k,2)+a(k,3);
u=v*a(k,4)+w*a(k,5)+a(k,6);
plot(z,u)
v=z;
w=u;
end
MATLAB M MATLAB
>>sample1
sample1.m M MATLAB
sample1.m M M
%() sample2.m
clear all
hold
a=[.5 0 0 0 .5 0;.5 0 .5 0 .5 0;.5 0 0 0 .5 .5];
v=0;
w=0;
for l=1:10000
r=rand;
if r<0.3
k=1;
elseif r<0.6
k=2;
else
21
k=3;
end
z=v*a(k,1)+w*a(k,2)+a(k,3);
u=v*a(k,4)+w*a(k,5)+a(k,6);
plot(z,u)
v=z;
w=u;
end
MATLAB M MATLAB
>>sample2
%() sample3.m
clear all
hold
a=[1/3 0 0 0 1/3 0;1/3 0 2/3 0 1/3 0;1/3 0 1/3 0 1/3 1/3;1/3 0 0 0 1/3 2/3;1/3 0 2/3 0
1/3 2/3];
v=0;
w=0;
for l=1:10000
r=rand;
if r<0.2
k=1;
elseif r<0.4
k=2;
elseif r<0.6
k=3;
elseif r<0.8
22
k=4;
else
k=5;
end
z=v*a(k,1)+w*a(k,2)+a(k,3);
u=v*a(k,4)+w*a(k,5)+a(k,6);
plot(z,u)
v=z;
w=u;
end
MATLAB M MATLAB
>>sample3
MATLAB
a(i,j)=i+j;
end
end
sample1.m for
for
for r end for
function [v,w]=f(x,y)
v=x^2+y^2;
w=x+y;
[v,w]=f(4,-1)
v =17
w =3
f.m
function [value]=f(x)
value=sqrt(x)*sin(x);
()
m.m ()
a=0;
b=4;
n=8;
h=(b-a)/n;
s1=0;
sum=0;
for i=0 :n-1;
s1=f(a+(i+1/2)*h);
sum=sum+s1;
end
24
sum*h
ans =1.7874
t.m ()
a=0;
b=4;
n=8;
h=(b-a)/n;
s1=0;
sum=0;
for i=0 :n-1;
s1=f(a+i*h)+f(a+(i+1)*h);
sum=sum+s1;
end
sum*h/2
ans =1.7329
s.m (Simpson )
25
a=0;
b=4;
n=8;
h=(b-a)/n;
s1=0;
sum=0;
for i=0 : 2:n-2;
s1=f(a+i*h)+4*f(a+(i+1)*h)+f(a+(i+2)*h);
sum=sum+s1;
end
sum*h/3
ans =1.7721
while
while
while expression
statements
end
n=1;
while n < 98765
n=n+1;
end
while
cos(x)-x^(1/2)=0
a=1
while abs(cos(a)-a^(1/2))>0.0001
b=a-(cos(a)-a^(1/2))/(-sin(a)-1/2*a^(-1/2))
a=b
end
a
a=
0.6417
26
e^x-3x^2=0
y=exp(x)-3*x.^2
xl=3; xr=4 %
format long
while abs(xr-xl)>10^(-6)
yold=exp(xl)-3*xl.^2
xtmp=(xl+xr)/2; ynew=exp(xtmp)-3*xtmp.^2
if yold*ynew<0
xr=xtmp
else
xl=xtmp
end
end
(xl+xr)/2
ans =
3.73307943344116
MATLAB if-else switch case
if
if
if
end
if True if
if else elseif
if
A;
else
B;
end
if A
A;
elseif B
B;
elseif C
C;
else
D;
end
27
a=input('score')
if a>=80
disp('');
else if a>=70
disp('');
else if a>=60
disp('');
else
disp('');
end
end
end
sample1.m if
switch case
switch case
switch
case A
A;
case B
B;
.
.
.
end
a=input('1-5')
switch a
case 1
disp('')
case 2
disp('')
case 3
disp('')
case 4
disp('')
case 5
disp('')
otherwise
disp('')
end
28
Laplace
syms t
laplace(t)
ans =1/s^2
syms s
ilaplace(1/s^2)
ans =t
Fourier
syms x
fourier(exp(-x^2))
ans =pi^(1/2)*exp(-1/4*w^2)
syms w
ifourier(pi^(1/2)*exp(-1/4*w^2))
ans=3991211251234741/4503599627370496*4^(1/2)/pi^(1/2)*exp(-x^2)
(3991211251234741/4503599627370496*4^(1/2) /pi^(1/2)=1)
f ( x, y ) = 20 + x 2 + y 2 10(cos(2x) + cos(2y ))
2
1 1
x 5 x 3
1 2 2 ,
y
5
y
4
2 1 3
function y = gatest(x)
y = 20 + x(1)^2 + x(2)^2 - 10 * ( cos(2*pi*x(1))+cos(2*pi*x(2)) );
>> A=[1 1;-1 2;2 1];
>> b=[2;2;3];
>> Lb=[-5;-5];
>> Ub=[3;4];
>> [x,fval,reason]=ga(@gatest,2,A,b,[],[],Lb,Ub)
x =0.0059
0.0023
fval =0.0080
reason =Optimization terminated: average change in the fitness value less
than options.TolFun.
29
function v = f(x)
v = x(1).^2 + x(2).^2;
>> [x,fval,reason]=ga(@f,2)
gatool
30
25
31
x 2 + ( y 3) 2 .