35
Matlab 在微積分課程之應用 林晶璟 老師 臺北科大 通識中心 檔案已上網 http://math.ntut.edu.tw

matlab基礎與法介紹

Embed Size (px)

Citation preview

Matlab

http://math.ntut.edu.tw

...........................................................................................................1 .............................................................................................1 .............................................................................................2 .............................................................................................3 .............................................................................................................4 .....................................................................................................5 .....................................................................................................8 .....................................................................................................9 ...........................................................................................12 ...........................................................................................14 ...............................................................................................16 ...................................................................................................20 ...........................................................................................23 ...................................................................................................29 ...............................................................................................29

Matlab Matlab Matlab "matrix laboratory" 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 2 >>n = (7*3+5.5)/10 n =2.6500

MATLAB help clc clf clear lookfor display input 1

display(Hello) a=Input(a)

name=Input(,s) load rand fix round 0~1

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

Matlab Permanent Constants 6>>pi ans =3.1416 Matlab ij eps inf nan NaN pi

1 Not A Number pi = 3.1415926...

Matlab 1>> y=sin(50)*exp(-0.9*2^3) y = -1.9589e-0042

abs(x) angle(z) sqrt(x) real(z) imag(z) conj(z) round(x) fix(x) floor(x) ceil(x) rat(x) rats(x) x x z x z z z x x x x x x (Signum function) x0sign(x) = 1 x y x y x y e 2 e 2

sign(x)

rem(x,y) gcd(x,y) lcm(x,y) exp(x) pow2(x) log(x) log2(x)

3

log10(x) sin(x) cos(x) tan(x) asin(x) acos(x) atan(x) sinh(x) cosh(x) tanh(x) asinh(x) acosh(x) atanh(x)

10 :

mvnrnd(mu,sigma,nobs)

factor

: factor(x^2-1)

ans = (x-1)*(x+1)

Matlab

4

plot Matlab plot >> x=linspace(0, 2*pi, 50); %linspace(0, 2*pi, 50) 0 2pi 50 >> y=sin(x); >> plot(x,y);

plot plot y=sin(x) y=cos(x) >> x=linspace(0, 2*pi, 50); >> plot(x, sin(x), x, cos(x));

5

plot y m c r g b w k plot . O x + * : - X + *

6

>> x=linspace(0, 2*pi, 50); >> plot(x, sin(x),'r', x, cos(x),'k');

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

7

xlabelylabeltitle legendgrid ontext >>x=linspace(0, 2*pi, 50); >>plot(x, sin(x),'r.',x, cos(x),'k-.'); >>xlabel('x'); >>ylabel('y'); >>title('ex'); >>grid on;

% x % y % %

Matlab polar ( ) >> theta=linspace(0, 2*pi); >> r=1+cos(theta); >> polar(theta, r);

8

Matlab meshplot3 mesh plot3 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

9

>> [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

%meshc() & surfc() [x,y]=meshgrid(-2:0.1:2); z=x.^2-y.^2; surfc(x,y,z)

%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 712

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 >>B\C ans = -2 -3 3 4

-1.5000 -0.5000

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 A'

3 4

ans =1 3 det(A)

2 4

ans =-2 inv(A)

ans =-2.0000 1.0000 A^2-2*A+eye(2)

1.5000 -0.5000

ans =6 6 eig(A)

9 15

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) det(A) log(A) eig(A) sum(A) sum(sum(A)) std(A)

A inverse A log 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-30*x^2)*exp(-x^2-y^2)+2*(2*x-10*x^3-10*y^5)*x*exp(-x^2-y^2) -1/3*(-2*x-2)*exp(-(x+1)^2-y^2) z >> diff(z,y) ( ) y ans =3*(1-x)^2*(-2*y-2)*exp(-x^2-(y+1)^2)+50*y^4*exp(-x^2-y^2) +2*(2*x-10*x^3-10*y^5)*y*exp(-x^2-y^2)+2/3*y*exp(-(x+1)^2 -y^2) 2z >> diff(diff(z,x),x) ( 2 ) x ans =6*exp(-x^2-(y+1)^2)+24*(1-x)*x*exp(-x^2-(y+1)^2) -6*(1-x)^2*exp(-x^2-(y+1)^2)+12*(1-x)^2*x^2*exp(-x^2-(y+1)^2) +60*x*exp(-x^2-y^2)+4*(2-30*x^2)*x*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)*x^2*exp(-x^2-y^2) +2/3*exp(-(x+1)^2-y^2)-1/3*(-2*x-2)^2*exp(-(x+1)^2-y^2) 2z >> diff(diff(z,x),y) ( ) xy ans =-6*(1-x)*(-2*y-2)*exp(-x^2-(y+1)^2)-6*(1-x)^2*x*(-2*y-2) *exp(-x^2-(y+1)^2)+2*(2-30*x^2)*y*exp(-x^2-y^2)-100*y^4*x *exp(-x^2-y^2)-4*(2*x-10*x^3-10*y^5)*x*y*exp(-x^2-y^2) +2/3*(-2*x-2)*y*exp(-(x+1)^2-y^2)18

>> diff(diff(z,y),x)

2z ) yx ans =-6*(1-x)*(-2*y-2)*exp(-x^2-(y+1)^2)-6*(1-x)^2*x*(-2*y-2) *exp(-x^2-(y+1)^2)+2*(2-30*x^2)*y*exp(-x^2-y^2)-100*y^4*x *exp(-x^2-y^2)-4*(2*x-10*x^3-10*y^5)*x*y*exp(-x^2-y^2) +2/3*(-2*x-2)*y*exp(-(x+1)^2-y^2) 2z >> diff(diff(z,y),y) ( 2 ) y ans =-6*(1-x)^2*exp(-x^2-(y+1)^2) +3*(1-x)^2*(-2*y-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)(

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=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 1 2 x 2 , 5 x 3 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

Optimization terminated: average change in the fitness value less than options.TolFun. x =0.0044 0.0052 fval =4.6427e-005 reason =Optimization terminated: average change in the fitness value less than options.TolFun.2 2 If x + y = 1 , find the maximum value of

25

9

x 2 + ( y 3) 2 .

31