matlab 科学工程计算

Download matlab 科学工程计算

If you can't read please download the document

Upload: avalon

Post on 10-Jan-2016

250 views

Category:

Documents


9 download

DESCRIPTION

matlab 科学工程计算. 同济大学数学系 陈雄达 [email protected] http://math.tongji.edu.cn/model/docs/matlabc.pps. teapotdemo. 目录. matlab 入门 matlab 基础编程 matlab 作图 matlab 工具箱 matlab 上机操作 matlab 综合训练 matlab 命令速查. matlab 入门. matlab 是什么 matlab 不是什么 matlab 集成环境 matlab ABC. matlab 入门. - PowerPoint PPT Presentation

TRANSCRIPT

  • matlab [email protected]://math.tongji.edu.cn/model/docs/matlabc.pps

  • *teapotdemo

  • *matlabmatlabmatlabmatlabmatlabmatlabmatlab

  • *matlabmatlabmatlabmatlabmatlab ABC

  • *matlab

    matlab

    M.N. Nachtigal S.C. Reddy L.N. Trefethen Copper Mountain 1990

  • *matlab, , 25(), , , , , , (matrix laboratory)

  • *matlab, --

  • *View > Desktop Layout > Five Panel / / File / Edit / View / Web / Window / Help

  • *matlab ABC >> % ...123 + 45 - 67 + 8 9x = 3 * sin(pi/4) ^ 2 () v =[1 3 5 2 4 6];() A =[1 3; 5,2 4 6];

  • *matlab(:)(elfun)(exp)fix,round,ceil,floor

    matlab

  • *-- [ ] (cell) (struct)

  • *(:) a:s:b a, s, b 1:2:10 [1 3 5 7 9] 7:-2:0 [7 5 3 1] 1:6 [1 2 3 4 5 6] 6:2 [ ] ()

  • *19matlab A, B, C u, v, w, x, y, z f, g, h, fun, f1, f2 alpha, beta, a, b, c

  • *pi =3.1415926i,j inf eps 2.2204460e-16NaN (Not a Number)

    inf NaN

  • format (short) 4format long (e/g) 15format + format bank format rat format loose format compact *

  • *>> A = magic(3); >> disp(A);>> s = hello world;>> disp(s);>> v = 1:100;>> disp(v);>> fprintf(%2d%2d%2d\n,A);>> fprintf(pi^2 = %12.6f\n,pi^2);>> fid = fopen(h.txt,w);>> fprintf(fid,s = %s\n,hello world);>> fclose(fid);

  • *()>> a = [1 2 3 4]; b = [0 1 2 3];>> a + bans = 1 3 5 7>> b./aans = 0 0.5000 0.6667 0.7500>> b.*aans = 0 2 6 12>> a.^bans = 1 2 9 64

  • * A = [ 8 1 6; 3 5 7; 4 9 2 ]; A(2,3) 7 A(2, [3 2 1]) [7 5 3] A([3 2],2:-1:1) [9 4; 5 3] A(2,:) [3 5 7] A(:,end) [6; 7; 2]

    816357492

  • *A(2)=A(n-1,:)=A(2+3, 2*3)=x=A(sqrt(4),x-3)=A(:)=v(end)=A(2) + 2 = x^2 + x = A(sqrt(4),x)-3 =A =inv(A) = (x>=0) = :

  • *(elfun)(sin,sinh,asin,asinh)(exp,log,log10,sqrt)(abs,conj,real,imag)(floor,ceil,round,mod)

    : ,

  • *(exp)>> x = 0:0.2:1x = 0 0.2 0.4 0.6 0.8 1.0>> y = exp(x)y = 1.00 1.22 1.49 1.82 2.22 2.71

  • *fix,round,ceil,floor

    fixroundceilfloor3.141533430.61800110-2.7181-2-3-2-3-1.4142-1-1-1-2

  • *>> [a,ind] = sort([41 33 16 10 13 54]a = 10 13 16 33 41 54 ind = 4 5 3 2 1 6>> a = find([41 33 16 10 13 54]>=35)a = 1 6>> [a,ind] = max([sqrt(10) 22/7 pi])a = 3.1623ind = 1

  • *rand(n), eye(n), zeros(n) n >> rand(2)ans = 0.9501 0.6068 0.2311 0.4860>> rand(3,2)ans = 0.8913 0.0185 0.7621 0.8214 0.4564 0.4447>> eye(2,3)ans = 1 0 0 0 1 0>> zeros(3)ans = 0 0 0 0 0 0 0 0 0

  • *diag>> diag([1 2 3; 4 5 6 7 8 9])ans = 1 5 9>> diag([1 2 3; 4 5 6 7 8 9],1)ans = 2 6>> diag([1 5 9])ans = 1 0 0 0 5 0 0 0 9>> diag([4 8],-1)ans = 0 0 0 4 0 0 0 8 0

  • *triu/tril>> A = magic(3)A = 8 1 6 3 5 7 4 9 2>> U = triu(A,-1)U = 8 1 6 3 5 7 0 9 2>> L1 = tril(A,-1) L1 = 0 0 0 3 0 0 4 9 0>> L2 = tril(A)L2 = 8 0 0 3 5 0 4 9 2

  • *>> char([4,1,8,8,11]+100)ans = hello>> abs(matlab)ans = 109 97 116 108 97 98 >> eval(exp(-pi*i))ans = -1 + 0i>> feval(sin,pi/2) ans = 1>> fun = cos;>> feval(fun,pi/2)ans = 0

  • *for k = 1:9, st = [fun ... num2str(k)]; x(k) = ... feval(st, x(k));end

    function y = fun1(x)function y = fun2(x)str2num(2*pi)ans = 6.2832str2num(cos(2*pi))ans = 1eval(cos(2*pi))ans = 1

  • * (true,1)(false,0) >, =, > pi >= sqrt(10) ans = 0>> [1:5] >= pians = 0 0 0 1 1

  • *(and)and(a,b)a & b(or)or(a,b)a | b(not)not(a)~a(xor)xor(a,b)

    a0011b0101a&b0001a|b0111~a1100xor(a,b)0110

  • *(any,all) v (), all(v)10 v (), any(v)01>> any([0 1 2 3]) ans = 1>> all([0 1 2 3]) ans = 0

  • *

  • *matlabhelphelp /help helpdocdoc eigwhatmatlabwho/whoswhichmatlablookfor lookfor curve / help curve

  • *(if,switch)forwhile

  • *(if)if 1 1elseif 2 2elseif 3 3else n+1end

    if temp < 10, disp(cold!);elseif temp < 20, disp(cool);elseif temp < 27, disp(warm);elseif temp < 31, disp(hot);else disp(too hot);end

  • (switch)switch month case {1,3,5,7,8,10,12} day = 31; case {4,6,9,11} day = 30; otherwise day = 28; % end

    *

  • *forfor = ends = 0;for k = 1:100, s = s + 1/k^2;endfor k = hello world, fprintf(%c,k); pause(0.1);endfor k = magic(3), k+1end

  • *whilewhile end

    while 1, if , break; end end

    n = input(n= );while n~=1, if mod(n,2)==1, n = n*3 + 1; else n = n / 2; endend

    Collatz

  • *for k = 1:9, for j = 1:9, fprintf(%d*%d=%3d , k,j,k*j); end fprintf(\n);end

  • *n = input(n>=0: );flag = 0;while ~flag, nn = n; rn = 0; while nn~=0, d = mod(nn,10); rn = rn * 10 + d; nn = floor(nn/10); end disp(rn) if rn==n, flag = 1; else n = rn + n; endend n 0 (=0), n nrn rn n
  • *(matlab)(5%--30%)()

    : Matlab

  • *n = input(years: );for k = n, if mod(k,400)==0, disp([num2str(k) is a leap year]); elseif mod(k,100)==0, disp([num2str(k) isnt a leap year]); elseif mod(k,4)==0, disp([num2str(k) is a leap year]); else disp([num2str(k) isnt a leap year]); endendn = input(years: );for k = n, if mod(k,400)==0 | ... (mod(k,4)==0&mod(k,100)~=0), disp([num2str(k) is a leap year]); else disp([num2str(k) isnt a leap year]); endend...

  • *function [p,t] = fun(n)% n: input integer% p: sum of factors% t: number of factors p = 0; t = 0; for k = 1:n, if mod(n,k)==0, p = p + k; t = t + 1; end endfunction [p,t]= fun(n)% n: input integer% p: sum of factors% t: number of factors k = 1:n; z = mod(n,k); t = sum(z==0); p = sum(k(z==0));fun.m

  • *>> [z,p] = fun(15)z = 24p = 4>> x = fun(6)x = 12>> fun(28)ans = 56>> [p,z] = funInput argument n is undefined.>> [p,z] = fun(20,18)Too many input arguments.>> [p,t,b] = fun(15)Too many output arguments.function [p,t] = fun(n)

  • *, , function X = amic(n) s = 1; for k = 2:n, if k==fun(fun(k)), X(s,[1 2]) = [k, fun(k)]; s = s + 1; end endfunction p = fun(n) k = 1:n-1; z = mod(n,k); p = sum(k(z==0));

  • *Frayer, [0,1]n, ,

    01

    14

    13

    12

    23

    45

    34

    11

    15

    25

    35

  • *function [p,q] = frayer(n) if n>1, [p,q] = frayer(n-1); ind = find(q(1:end-1)+q(2:end)==n); for k = ind(end:-1:1), q = [q(1:k) n q(k+1:end)]; p = [p(1:k) p(k)+p(k+1) p(k+1:end)]; end elseif n==1, p = [0,1]; q = [1,1]; end

  • *function v = fact(n) if n==0|n==1, v = 1; else v = n * fact(n-1); end>> v = prod(1:n);

  • *s = 0;for k = 1:n, p = 1; for j = 1:k, p = p * j; end s = s + 1/(p+1);endop = 0;for k = 1:n, op = op + 1; for j = 1:k, op = op + 1; end op = op + 3;end

    op=4n+n(n+1)/2

  • *s = 0; p = 1;for k = 1:n, p = p*k; s = s + 1/(p+1);endop = 2;for k = 1:n, op = op + 1; op = op + 3;end

    op=4n+2

  • *matlab

  • *(plot)plot(x,y,s) x = linspace(0,2*pi); y = sin(x); plot(x,y,r-);

  • *(plot)plot(x,y,s) -- s

    colorpoint styleline styleRed. point- solidGreenx x-mark: dottedBlueo circle-. dashdotCyan+ plus-- dashedMagenta* starYellows,p,h 456blacKd diamondv,^,

  • *(plot)xlabel(x);ylabel(y);title(y=sin(x));

  • *(polar)>> theta = linspace(0,6*pi);>> rho = theta;>> polar(theta,rho,r-);

  • *(plot3) plot3(x,y,z,s) >> t = 0:pi/50:10*pi; >> plot3(sin(t),cos(t),t);

  • *(comet3)>> t = 0:pi/500:20*pi;>> comet3(sin(t),cos(t),t);

  • *(meshgrid)[X,Y] = meshgrid(x,y) [XY] = meshgrid(1:3, 4:7) X =[1 2 3 Y = [4 4 4 1 2 3 5 5 5 1 2 3 6 6 6 1 2 3] 7 7 7]

  • *(mesh)x = linspace(-5,5);[X,Y] = meshgrid(x);Z = X.^2 Y.^2;mesh(X,Y,Z);view(-60,28);X,Ymeshgrid(x,y) mesh(x,y,Z)surf

  • theta = linspace(0,2*pi);psi = linspace(-pi/2+0.5,pi/2-0.5);[Theta,Psi] = meshgrid(theta,psi);X = 4 * sec(Psi) .* cos(Theta);Y = 4 * sec(Psi) .* sin(Theta);Z = 3 * tan(Psi);surf(X,Y,Z)view(-41,22);shading interp;colormap spring*

  • *(bar3)>> A = [82 82 63 81 82 81 84 61 84 85 80 79 65 84 86];>> bar3(A,'grouped');

  • *(pie3)pie3([2 4 3 5],[0 1 1 0],... {'North','South','East','West'})

  • *surf, contour: -- 1200
  • *surf, contour>> A = [ 1130 1250 1280 1230 1040 900 500 700 1320 1450 1420 1400 1300 700 900 850 1390 1500 1500 1400 900 1100 1060 950 1500 1200 1100 1350 1450 1200 1150 1010 1500 1200 1100 1550 1600 1550 1380 1070 1500 1550 1600 1550 1600 1600 1600 1550 1480 1500 1550 1510 1430 1300 1200 980];>> x = 1200:400:4000;>> y = 1200:400:3600;>> surf(x,y,A);>> contour(x,y,A,100);

  • *matlab(toolbox)(help matfun)(help optim)(help ode45,help pde)(help polyfun)(help stats)

  • *(inv)inv(A) A >> A = magic(3) A = 8 1 6 3 5 7 4 9 2 >> inv(A) ans = 0.1472 -0.1444 0.0639 -0.0611 0.0222 0.1056 -0.0194 0.1889 -0.1028

  • *(\)>> A = hilb(3)A = 1.0000 0.5000 0.3333 0.5000 0.3333 0.2500 0.3333 0.2500 0.2000

    >> b = ones(3,1);% 311

    >> x = inv(A) * bx = 3.0000 -24.0000 30.0000>> A = hilb(3)A = 1.0000 0.5000 0.3333 0.5000 0.3333 0.2500 0.3333 0.2500 0.2000>> b = ones(3,1);% 311>> x = A \ bx = 3.0000 -24.0000 30.0000

  • *(rank)>> A = magic(4)A = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1>> rank(A)ans = 3

  • *(det)>> A = pascal(4)A = 1 1 1 1 1 2 3 4 1 3 6 10 1 4 10 20>> det(A)ans = 1

  • *(trace)>> A = [1:4]' * [1:4]A = 1 2 3 4 2 4 6 8 3 6 9 12 4 8 12 16>> trace(A)ans = 30

  • *Cholesky(chol)>> A = pascal(4)A = 1 1 1 1 1 2 3 4 1 3 6 10 1 4 10 20>> B = chol(A)B = 1 1 1 1 0 1 2 3 0 0 1 3 0 0 0 1

  • *(eig)>> A = gallery(3)A = -149 -50 -154 537 180 546 -27 -9 -25>> eig(A)ans = 1.00000000001122 1.99999999999162 2.99999999999700>> [V,D] = eig(A)

    V = 0.3162 -0.4041 -0.1391-0.9487 0.9091 0.9740-0.0000 0.1010 -0.1789

    D = 1.0000 0 0 0 2.0000 0 0 0 3.0000

  • *(linprog)>> f = [1 1]';>> A = [-2 -1; -5 -8];>> b = [-10 -40]';>> Aeq = [ ];>> beq = [ ];>> lb = [0 4];>> ub = [7 inf];>> linprog(f,A,b,Aeq,beq,lb,ub) Optimization terminated successfully.ans = 3.0000 4.0000

  • *(fminu)[x,opts]=fminu(f,x0,options,g,p1,p2,)

    helpfoptions

  • *(fminu)function v=f(x) v = 100*( x(2)-x(1)^2 )^2 + ... (x(1)-1)^2;

    >> x = fminu('f',[0 0]')x = 1.0000 1.0000

  • *(fzero)[x,fv]=fzero(f,x0,options,p1,p2,)

    helpfoptions

  • *(fzero)>> f = inline('x+exp(x)-3');>> [x,fv]=fzero(f,2)x = 0.79205996843068fv = 0

  • *(fmincon)x = fmincon(fun,x0,... [1 1 1],8,... [1 2 0],6,... [0 0 0],[],... cons... );function v = fun(x) v =-3*x(1)-x(2)^2+x(3);function [c,ceq] = cons(x) c(1)= x*x-16; c(2)= x(2)*(x(1)+x(3))-12; ceq = [];[6 0 0]

  • *(ode45)[t,y] = ode45(odefun,tspan,y0,options,p1,p2...)helpodesetplot(x,y)

  • *(ode45)function dydx =f(x,y) dydx = y x + 1;

    >> [x,y] = ... ode45(f,[0,2],1);>> plot(x,y,r-);

  • *

    polyval(p,1:5)

    r = roots(p)

    p = poly(r) p = [1 0 2]; q = [2 1]; s = conv(p,q);

    [q,r] = deconv(s,p);

    d = polyder(p)

  • *(polyfit)>> x = linspace(0,pi,8)x = 0 0.4488 0.8976 1.3464 1.7952 2.2440 2.6928 3.1416>> y = sin(x)y = 0 0.4339 0.7818 0.9749 0.9749 0.7818 0.4339 0.0000>> p = polyfit(x,y,2)p = -0.4011 1.2601 -0.0179>> xx = linspace(0,pi,200);>> yy = polyval(p,xx);>> plot(xx,yy,'r-',x,y,'b-','linewidth',2)

  • *(interp1)yi=interp1(xyxi'method')nearest: linear: spline: cubic: :

  • *(interp2)z=interp2(x0,y0,z0,x,y,method)nearest linear cubic ,

  • *betafit, binofit, mle, betapdf, binopdf, hygepdf, betastat, binostat, chi2stat, anova1, anova2, polyfit, regress, lsqnonneg, nlinfit, nlintool,

  • *matlabmatlabCtrl-R

  • *matlab

  • *matlabA,B, A*B, B*A, A.*B, B.*A, A/B, A\B, A./B, A.\B

  • *matlabA,B, (1) (2) (3) (4) (5)

  • *matlabA,

  • *matlab: A, A=LDL, LD Cholesky: XOY:

  • *matlab : ... .* ./ .^ \ > >> >= == ~= % abs all and any asin asinh bar3 ceil cell char chol comet3 conj contour conv deconv det diag disp doc eig elfun else elseif end eps eval exp eye false feval find fix floor fmincon fminu for format fprintf function fzero help hilb i if imag inf input interp1 interp2 inv linprog linspace log log10 lookfor j magic matfun max mesh meshgrid mod NaN not num2str ode45 ones optim or pascal pi pie3 plot plot3 polar poly polyfun polyder polyfit polyval prod rand rank real roots round sin sinh sort sqrt stats str2num struct surf switch title trace tril triu true view what which while who whos xlabel xor ylabel zeros

  • *: (x,y)z575200*-50150

    x129.0140.0103.588.0185.5195.0105.5y7.5141.523.0147.022.5137.585.5z4868688x157.5107.577.081.0162.0162.0117.5y-6.5-81.03.056.5-66.584.0-33.5z9988949

  • *:

  • *THANK YOU

    ***