Ngo Van Thanh, IOP 11/2011
Phần II. Tin học ứng dụng Chương 2: Sử dụng phần mềm Mathematica (LT: 10, TH:10) Cấu trúc lệnh cơ bản Xử lý đồ họa Các phương pháp tính số Lập trình trên Mathematica Các gói chương trình chuyên dụng Website: http://www.iop.vast.ac.vn/~nvthanh/cours/comp/math/ Wolfram website http://reference.wolfram.com/mathematica/guide/mathematica.html The University of North Carolina http://facstaff.unca.edu/mcmcclur/mathematica/ Salisbury University http://facultyfp.salisbury.edu/despickler/personal/classroomresourcesmath.asp
2.1 Cấu trúc lệnh cơ bản File văn bản : được chia thành các cell Kiểu của cell : Title, subtitle,, text, Input (ngầm định) Nhiều câu lệnh trên cùng một cell; gộp các cell thành một nhóm Double-Click trên nhóm celll để mở rộng hoặc đóng nhóm.
Thực hiện các câu lệnh trong cell : SHIFT+ Enter Có thể chọn nhiều Cell để chạy đồng thời. Kết quả thực hiện cho mỗi câu lệnh được ghi ra trong cell Output : Out[n], số thứ tự n trong Out[ ] tương ứng với cell Input In[n]. Tất cả kết quả tính toán được ghi lại trên bộ nhớ của máy tính cho đến khi tắt chương trình Mathematica, hoặc sử dụng lệnh Clear[ ] Sau khi sửa câu lệnh, phải chạy lại câu lệnh đó bằng SHIFT+ Enter Quy tắc tên biến, hàm : Phân biệt chữ hoa và chữ thường, không được dùng ký tự gạch dưới : _ Tên Hàm được ghép nhiều từ với nhau, chữ cái đầu tiên của mỗi từ được viết hoa Vd: ListPlot[ ], Solve[ ], FindRoot[ ] Quy tắc móc, ngoặc ( ), [ ], { }, [[ ]] ( ) : nhóm biểu thức tương tự như các ngôn ngữ lập trình khác a*(b+c) [ ] : đối số của hàm số : Sin[x]; Plot[5 x 2,{x,1,2}] { } : tập hợp các phần tử; nhóm các câu lệnh; mảng/ma trận {1,2,3}; {a1 x+b1 y==0, a2 x+b2 y==0} [[ ]] : chỉ số mảng : a[[1]]; b[[1]][[2]]
Toán tử: Phép toán Ký hiệu TT. Quan hệ Ký hiệu TT. Logic Ký hiệu Cộng + EQ == NOT! Trừ - NEQ!= AND && Nhân * or space GT > OR Chia / GEQ >= Luỹ thừa ^ LT < LEQ >= Hằng số: Pi hoặc π ~ 3.14159 E : cơ số e ~ 2.71828 EulerGamma : hằng số Euler γ ~ 0.577216 Degree : radian của 1 độ = π/180
Hàm số cơ bản: Sin[x] Exp[x] KroneckerDelta[a,b] Cos[x] Log[x] DiracDelta[a,b] Tan[x] Log[x,b] HeavisideTheta[a,b] Cot[x] Log10[x] Gamma ArcSin[x] Abs[x] Erf ArcCos[x] Min[x] BesselJ ArcTan[x] Max[x] Prime[n] ArcCot[x] Im[z] Factorial[N] ~ N! Sinh[x] Re[z] RandomInteger[i min,i max ] Cosh[x] Conjugate[z] RandomReal[x min,x max ] Tanh[x] Arg[z] NormalDistribution [ µ,σ] Coth[x] Abs[z] Mean[list] ArcSinh[x] Plus[a,b, ] Variance[dist] ArcCosh[x] Times[a,b, ] ArcTanh[x] Power[a,b, ] ArcCoth[x] Mod[a,b]
Palettes: Vd: InputForm : {{1, 2}, {3, 4}} Palettes
2.2 Xử lý đồ họa Vẽ đồ thị theo hàm số Đồ thị hàm 1 biến : Plot[{expr1, expr1, }, {x, x min, x max }, Opt1->{Values of Opt1}] exprn : các biểu thức toán học là một hàm theo x {x, x min, x max } : khoảng giá trị của biến số x Plot[x^3 + 3 x^2 + x - 9, {x, -4, 3}] 20 10 4 3 2 1 1 2 3 10 20 30
Export đồ thị ra file Nên chọn EPS hoặc WMF Chọn hình vẽ Click chuột phải -> Save Graphics As Chọn EPS -> Save
Export đồ thị ra file
Copy trực tiếp sang Winword hoặc PowerPoint Chọn hình vẽ Vào menu Edit -> Copy As -> Chọn Metafile
Trên Winword hoặc PowerPoint nhấn Ctrl + v 2 4 6 8 10 1.0 0.5 0.5 1.0
Options : AxesLabel -> { x_label, y_label } Plot[x^3 + 3 x^2 + x - 9, {x, -4, 3}, AxesLabel -> { x, y }] y 20 10 4 3 2 1 1 2 3 x 10 20 30
PlotLabel -> { plot label } Plot[x^3 + 3 x^2 + x - 9, {x, -4, 3}, PlotLabel -> f[x]]
LabelStyle -> {FontFamily -> "Times", FontSize -> 18} Plot[x^3 + 3 x^2 + x - 9, {x, -4, 3}, PlotLabel -> f[x], AxesLabel -> {Style[ x,italic], y } LabelStyle -> {FontFamily -> "Times", FontSize -> 16}]
AxesOrigin -> {x O, y O } Plot[x^3 + 3 x^2 + x - 9, {x, -4, 3}, AxesOrigin -> {-4, -30}] 20 10 4 3 2 1 1 2 3 10 20 20 10 0 10 30 20 3 2 1 0 1 2 3
Mesh -> 30; MeshStyle -> {Red,PointSize[Medium]} Plot[x^3 + 3 x^2 + x - 9, {x, -4, 3}, Mesh -> 20, MeshStyle -> {Red, PointSize[Medium]] 20 10 4 3 2 1 1 2 3 10 20 30
PlotRange -> {Full, Automatic} hoặc {{x min, x max },{y min, y max }} Plot[Exp[x^2] + x -4, {x, 0, 2}, PlotRange -> {0, 50}] 50 25 40 20 15 30 10 5 20 0.5 1.0 1.5 2.0 10 0.0 0.5 1.0 1.5 2.0
Ticks -> None / {t 1, t 2, t 3, } Plot[Sin[x], {x, 0, 10},Ticks -> None] Plot[Sin[x], {x,0,10}, Ticks -> {{0,Pi,2 Pi,3 Pi},{-1,1}}] 1 2 3 1
AspectRatio -> Automatic hoặc y/x 1.0 Plot[Sin[x], {x, 0, 10}, AspectRatio -> 1/2] 1.0 0.5 0.5 2 4 6 8 10 0.5 2 4 6 8 10 1.0 Plot[Sin[x], {x, 0, 10}, AspectRatio -> 2/1] 0.5 1.0
Epilog -> {obj1, obj2, } : phía trước hình Prolog -> {obj1, obj2, } : phía sau hình Plot[Sin[x],{x, 0, 10}, Epilog -> { {Dashed, Blue, Line[{{Pi/2,Sin[Pi/2]}, {Pi/2,0}}]}, Text[Sin[x], {Pi,0.5}]}]
Drawing Tools
Drawing Tools : chỉ dùng cho đồ thị 2 chiều
Vẽ nhiều hàm trên một đồ thị Plot[{Sin[x], Sin[2 x]},{x, 0, 10}, PlotStyle -> {Red,{Blue, Dashed}}] 1.0 0.5 2 4 6 8 10 0.5 1.0
Show[graph1,graph2,, Options -> Values] vd. Hàm 1.0 0.5 1 2 3 4 5 6 0.5 1.0
Đồ thị 3 chiều Plot3D[{exprs.}, {x, x min, x max }, {y, y min, y max }, Opt1 -> {Values of Opt1}] Plot3D[Sin[2 x + y^2], {x, -3, 3}, {y, -2, 2}]
BoxRatios -> {x, y, z} Plot3D[Sin[2 x + y^2], {x, -3, 3}, {y, -2, 2}, BoxRatios -> {1, 1, 1/2}]
ColorFunction -> (White &) Plot3D[Sin[2 x + y^2], {x, -3, 3}, {y, -2, 2}, ColorFunction -> (White)]
MeshShading -> {{None, None}} MeshStyle -> Thick Plot3D[Sin[x + y], {x, -3, 3}, {y, -2, 2}, MeshShading -> {{None, None}}, MeshStyle -> Thick] 1.0 0.5 0.0 0.5 1.0 2 0 1 2 0 1 2 2
Vẽ đồ thị các điểm từ bảng dữ liệu ListPlot[{list1, list2}, Opts -> Options] List : {{x 1, y 1 }, {x 2, y 2 }, } ListPlot[Table[{n, Sin[n]},{n, 0, 10, 0.5}]] Table[{n, Sin[n]},{n, 0, 10, 0.5}] 1.0 {{0.,0.}, {0.5, 0.479426}, {1., 0.841471}, {1.5, 0.997495}, {2., 0.909297}, {2.5, 0.598472}, {3., 0.14112}, {3.5, -0.350783}, {4., -0.756802}, {4.5, -0.97753}, {5., -0.958924}, {5.5, -0.70554}, {6., -0.279415}, {6.5, 0.21512}, {7., 0.656987}, {7.5, 0.938}, {8.,0.989358}, {8.5, 0.798487}, {9., 0.412118}, {9.5, -0.0751511}, {10., -0.544021}} 0.5 0.5 1.0 2 4 6 8 10
Joined -> True ; Mesh -> Full; ListPlot[Table[{n, Sin[n]},{n, 0, 10, 0.5}], Joined -> True, Mesh -> Full] 1.0 0.5 2 4 6 8 10 0.5 1.0
MeshStyle -> {PointSize[Large], Red} PlotStyle -> {Thick, Green} ListPlot[Table[{n, Sin[n]},{n, 0, 10, 0.5}], Joined -> True, Mesh -> Full, PlotStyle -> {Thick, Green}, MeshStyle -> {PointSize[Large], Red}] 1.0 0.5 2 4 6 8 10 0.5 1.0
InterpolationOrder -> 3 ListPlot[Table[{n, Sin[n]},{n, 0, 10, 0.5}], Joined -> True, Mesh -> Full, InterpolationOrder -> 3] 1.0 0.5 2 4 6 8 10 0.5 1.0
Đọc từ file dữ liệu Import[ file","table"] li1 = Import[ D:/math/data.dat","Table"] {{0.,0.},{0.5,0.479426},{1.,0.841471},{1.5,0.997495}, {2.,0.909297},{2.5,0.598472},{3.,0.14112}, {3.5,-0.350783},{4.,-0.756802},{4.5,-0.97753}, {5.,-0.958924},{5.5,-0.70554},{6.,-0.279415}, {6.5,0.21512},{7.,0.656987},{7.5,0.938},{8.,0.989358}, {8.5,0.798487},{9.,0.412118},{9.5,-0.0751511}, {10.,-0.544021}}
Vẽ đồ thị các đoạn thẳng từ bảng dữ liệu ListLinePlot[{list1, list2}, Opts -> Options] ListLinePlot[{l1,l2}, Mesh -> Full, PlotMarkers -> Automatic, InterpolationOrder -> 2] 1.0 0.5 0.5 1.0 2 4 6 8 10 ListPlot[{l1,l2}, Mesh -> Full, PlotMarkers -> Automatic, Joined -> True, InterpolationOrder -> 2] 1.0 0.5 0.5 1.0 2 4 6 8 10
Các loại đồ thị khác: ListPlot3D[{list1, list2}, Opts -> Options] List : {{x 1, y 1, z 1 }, {x 2, y 2, z 2 }, } data = Table[Sin[j^2+i],{i,0,Pi,Pi/5},{j,0,Pi,Pi/5}]; ListPlot3D[data, InterpolationOrder -> 3]
ListPointPlot3D[{list1, list2}, Opts -> Options] List : {{x 1, y 1, z 1 }, {x 2, y 2, z 2 }, } data = Table[Sin[j^2+i],{i,0,Pi,0.1},{j,0,Pi,0.1}]; ListPointPlot3D[data] 1.0 0.5 0.0 0.5 1.0 20 30 10 20 10 30
DensityPlot[f, {x, x min, x max },{y, y min, y max }] Giá trị càng lớn thì có màu càng sáng ListDensityPlot[{{x 1, y 1, f 1 },{x 2, y 2, f 2 }, }] DensityPlot3D[ ] và ListDensityPlot3D[ ] DensityPlot[Sin[x]Sin[y],{x,-4,4},{y,-3,3}] Plot3D[Sin[x]Sin[y],{x,-4,4},{y,-3,3}]
ContourPlot[f, {x, x min, x max },{y, y min, y max }] ContourPlot[{{f 1 == g 1 },{f 2 == g 2 }},{x,x min,x max },{y,y min,y max }] Giá trị càng lớn thì có màu càng sáng ListContourPlot[{{x 1, y 1, f 1 },{x 2, y 2, f 2 }, }] ContourPlot[Sin[x]Sin[y],{x,-4,4},{y,-3,3}]
ContourPlot[Sin[x]Sin[y]==0.5,{x,-4,4},{y,-3,3}] 3 2 1 0 1 2 3 4 2 0 2 4 Plot3D[Sin[x]Sin[y],{x,-4,4},{y,-3,3},PlotRange->{0,0.5}, ClippingStyle->None, BoundaryStyle->{Thick,Blue}]
VectorPlot[{v x, v y }, {x, x min, x max },{y, y min, y max }] VectorPlot3D[{v x,v y,v z },{x,x min,x max },{y,y min,y max },{z,z min,z max }] ListVectorPlot[{{{x 1, y 1 },{v x1, v y1 }}, }] ListVectorPlot3D[{{{x 1, y 1, z 1 },{v x1, v y1, v z1 }}, }] 3 VectorPlot[{x,-y},{x,-3,3}, {y,-3,3}] 2 1 0 1 2 3 3 2 1 0 1 2 3
StreamPlot[{v x, v y }, {x, x min, x max },{y, y min, y max }] ListStreamPlot[{{{x 1, y 1 },{v x1, v y1 }}, }] StreamPlot[{x,-y},{x,-3,3},{y,-3,3}] 3 2 1 0 1 2 3 3 2 1 0 1 2 3
2.3 Các phương pháp tính số Tính toán trên các biểu thức Phép gán f = a x^2 + b x + c hoặc f = a x 2 + b x + c f (x + d) --> kết quả : (d + x) (c + b x + a x 2 ) Tính số (1/2) Sin[20] x --> kết quả : (1/2) Sin[20] x//n --> kết quả : 0.456473 x N[(1/2) Sin[20] x, 8] --> kết quả : 0.45647263 x Các số hạng trong biểu thức (x+y+z)[[2]] --> kết quả : y (x+y+z)[[-1]] --> kết quả : z f[g[a],g[b]][[1]] --> kết quả : g[a] f[g[a],g[b]][[1,1]] --> kết quả : a ReplacePart[a+b+c+d, 3 -> x^2] --> kết quả : a+b+x 2 +d
Khai triển biểu thức : Expand/ExpandAll f = (1+x)^3 + Sin[(1+x)^3] Expand[f] ExpandAll[f] Biểu thức lượng giác: TrigExpand[Sin[x+y]] --> Sin[x] Cos[y] + Cos[x] Sin[y] Số phức : ComplexExpand[Sin[x],x] --> Cosh[Im[x]] Sin[Re[x]] + i Cos[Re[x]] Sinh[Im[x]]
Đơn giản hoá : Simplify/FullSimplify f = 1 + 3 x + 3 x^2 + x^3 + Sin[1 + 3 x + 3 x^2 + x^3] Simplify[f] --> Phân tích thừa số : Factor[] Factor[1 + 2x + x^2] --> (1 + x) 2 Extension Factor[2+2Sqrt[2]x+x^2] --> Factor[2+2Sqrt[2]x+x^2, Extension -> Sqrt[2]] Biểu thức lượng giác: Factor[Sin[x]^2+Tan[x]^2, Trig -> True] TrigFactor[Sin[x]^2+Tan[x]^2]
Đạo hàm riêng D[f, {x, n}] : n là bậc của đạo hàm D[f, {x, n x },{y, n y }, ] : đạo hàm nhiều biến D[Sin[x]/(a + x^2),x] D[Sin[x]/(a + x^2),{x,2}] NonConstants D[ a x^2, x, NonConstants -> {a}] --> 2 a x + x 2 D[a, x, NonConstants -> {a}]
Đạo hàm toàn phần Dt[f, {x, n}] : n là bậc của đạo hàm Dt[f, {x, n x },{y, n y }, ] : đạo hàm nhiều biến Dt[ a x + b,x] --> a + x Dt[a,x] + Dt[b,x] Dt[x^2 y, x, y] --> 2 x + 2y Dt[x,y] + 2x Dt[x,y] Dt[y,x] Constants Dt[ a x + b, x, Constants -> {b}] --> a + x Dt[a, x, Constants ->{b}]
Derivative Sin [x] : --> Cos[x] Derivative[1][Sin][x] Sin [x] : --> -Sin[x] Derivative[2][Sin][x] Derivative[n 1, n 2, ][f] f[x_, y_] := Sin[x] Cos[y]; Derivative[1, 2][f][x, y] --> -Cos[x] Cos[y]
Giới hạn (Limit) Limit[expr., x -> x 0 ] Limit[expr., x -> x 0, Direction -> 1] Limit[expr., x -> x 0, Direction -> -1] Limit[(1 + x/n)^n, n -> Infinity] --> e x Direction Limit[Tan[x], x -> Pi/2, Direction -> 1] Limit[Tan[x], x -> Pi/2, Direction -> -1] --> --> Assumptions Limit[ x^a,x ->Infinity, Assumptions -> a < 0] --> 0 Limit[ x^a,x ->Infinity, Assumptions -> a > 0] -->
Tích Phân (Integrate) Integrate[f, x] Integrate[f, {x, x min, x max },{y, y min, y max }, ] Integrate[1/(x^3+1),x] --> Integrate[1/(x^3+1),{x,0,1}] PrincipalValue Phương pháp Riemann Integrate[Tan[x],{x,0,Pi}] --> Không hội tụ Phương pháp Cauchy Integrate[Tan[x],{x,0,Pi}, PrincipalValue -> True]
Tích Phân số (NIntegrate) NIntegrate[f, {x, x min, x max }] NIntegrate[f, {x, x min, x max },{y, y min, y max }, ] NIntegrate[1/(x^3+1), {x, 0, 1}] --> 0.835649 Integrate[1/(x^3 + 1), {x, 0, 1}] //Timing -->??s NIntegrate[1/(x^3 + 1),{x, 0, 1}] //Timing -->??s Cận tích phân không phải là hằng số NIntegrate[Sin[5x y+y^2] + 1,{x, -1, 1}, {y,-sqrt[1-x^2], Sqrt[1-x^2]}] NIntegrate[Sin[5x y+y^2] + 1,{y,-Sqrt[1-x^2], Sqrt[1-x^2]},{x, -1, 1}]
Exclusions NIntegrate[1/Sqrt[x 2 +y],{x,-2,4},{y,-2,4}] NIntegrate[1/Sqrt[x 2 +y],{x,-2,4},{y,-2,4}, Exclusions -> (x 2 + y == 0)] Số vòng lặp đệ quy (Recursive) MinRecursion -> 0 MaxRecursion -> Automatic NIntegrate[Exp[-100(x2+y2)],{x,-50,60},{y,-50,60}] --> 0.0 NIntegrate[Exp[-100(x2+y2)],{x,-50,60},{y,-50,60}, MinRecursion -> 4] --> 0.0314159 Method NIntegrate[1/Sqrt[x 2 +y],{x,-2,4},{y,-2,4}, Method -> "MonteCarlo"]
Tổng (Tích) Sum/NSum (Product/NProduct) Sum[f, {i, i max }] Sum[f, {i, i min, i max },{j, j min, j max,dj}, ] Sum[i^2,{i,1,n}] --> Sum[i^3,i] --> NSum[f, {i, i max, i max }] NSum[f, {i, i min, i max,di}] NSum[1/((k-20)^2+1),{k,0, }] --> 3.09286 NSumTerms (NProductFactors) -> 15 NSum[1/((k-20)^2+1),{k,0, }, NSumTerms -> 30] --> 3.10462
Khai triển chuỗi luỹ thừa Series[f, {x, x 0, n}] Series[f, {x, x 0, n x },{y, y 0, n y }, ] Series[ Cos[x]/x,{x,0,10}] --> Normal[%] --> Assumptions Series[ArcCos[x],{x,1,1},Assumptions->(x > 1)] Series[ArcCos[x],{x,1,1}]
Hệ số của số hạng bậc n trong chuỗi luỹ thừa SeriesCoefficient[f, {x, x 0, n}] SeriesCoefficient[f, {x, x 0, n x },{y, y 0, n y }, ] Series[ Cos[x]/x,{x,0,10}] SeriesCoefficient[ Cos[x]/x,{x,0,9}] --> SeriesCoefficient[Cos[x]/x,{x,0,8}] -->??? SeriesCoefficient[ Exp[-x],{x, 0, n}] --> Assumptions SeriesCoefficient[ Exp[-x],{x, 0, n}, Assumptions -> (n 0)]
Khai triển chuỗi Fourier FourierSeries[f, x, n}] FourierSeries[f, {x 1, x 2, },{n 1, n 2, }] với FourierSeries[t/2, t, 3] FourierParameters {1,1} {1,-2π} {a, b}
Hệ số của số hạng bậc n trong chuỗi Fourier FourierCoefficient[f, x, n}] FourierCoefficient[f, {x 1, x 2, },{n 1, n 2, }] FourierSeries[t/2, t, 3] FourierCoefficient[t/2, t, 2] --> FourierCoefficient[t/2, t, 3] -->??? FourierParameters {1,1} --> {1,-2π} --> {a, b} -->
Giải phương trình /hệ phương trình - (bất phương trình) Solve[expr., vars, domain] domain : Reals, Integers, Complexes sol = Solve[a x 2 + b x + c == 0, x] sol[[2]] --> x/.sol[[2]] --> Hệ phương trình Solve[{expr1., expr1., }, {var1, var2, }] Solve[{3x + 8y == 5, 5x + 2y == 7},{x,y}]
sol2 = Solve[{x 2 + y 2 == 1, x + y == a}, {x, y}] sol2[[1]] -->??? x/.sol2[[2]] -->??? x/.sol2[[2, 1]] -->??? Cách viết khác khi expr. có nhiều biểu thức logic Solve[x 2 + y 2 == 1 && x + y == a, {x, y}] sol3 = Solve[{2x+3y == 4,3x-4y 5,x-2y > -21},{x,y},Integers] x/.sol3 -->???
Giải số các phương trình NSolve[expr., vars, domain] domain : Reals sol = NSolve[x^3-2x + 3 == 0,x] sol = NSolve[x^3-2x + 3 == 0,x,Reals] -->??? NSolve[x^2+y^2==1 && x^4+y^4==2 && Element[x,Reals],{x,y}] --> x : Reals ; y : Complex FindRoot[{f 1, f 2 }, {{x,x 0 },{y,y 0 }}] FindRoot[f, {x, x 0 }] --> Phương pháp Newton FindRoot[f, {x, x 0, x 1 }] --> Phương pháp Secant FindRoot[f, {x, x 0, x min, x max }] Kết thúc tính toán khi giá trị x vượt ra ngoài khoảng
Plot[Sin[x]+Exp[x],{x,-10,1}] 3 2 1 10 8 6 4 2 1 FindRoot[Sin[x]+Exp[x]==0, {x,10}] --> {x -> -0.588533} FindRoot[Sin[x]+Exp[x]==0, {x,-10}] --> {x -> -9.4247} FindRoot[Sin[x]+Exp[x]==0, {x,-3,-4,-2}] --> {x -> -3.09636}
ContourPlot[{x == 2y + 3Cos[x],y == x-4}, {x,0,15},{y,-5,15}, Frame -> False, Axes -> True, LabelStyle -> Large, ContourStyle -> Thick] 15 10 5 2 4 6 8 10 12 14 5 FindRoot[{x == 2y + 3 Cos[x],y == x-4},{x,6},{y,2}] --> {x -> 5.6256, y -> 1.6256}
Giải phương trình (hệ pt) vi phân DSolve[eqn., y, x] DSolve[{eqn1.,eqn2, }, {y1,y2, }, x] DSolve[eqn., y, {x1,x2, }] DSolve[y'[x] + y[x] == a Sin[x], y[x], x] Phương trình vi phân có điều kiện biên DSolve[{y'[x] + y[x] == a Sin[x], y[0]==0}, y[x], x] Phương trình vi phân bậc 2 DSolve[y''[x] + 4y'[x] + 5y[x] == 0, y[x],x]
Phương trình vi phân phi tuyến DSolve[ y'[x] + x y'[x]^2 == 1, y[x], x] Hệ phương trình vi phân DSolve[{y'[x]== x^2y[x],z'[x]==5z[x]}, {y[x],z[x]},x] Hệ phương trình vi phân có điều kiện biên DSolve[{y'[x]-4z[x]==Cos[x], y[x]+z[x]==1/2, y[pi/2]==1/2},{y[x],z[x]},x]
Đạo hàm riêng DSolve[3D[y[x1,x2],x1]+5D[y[x1,x2],x2]==x1, y[x1,x2],{x1,x2}] Đạo hàm riêng bậc 2 DSolve[3D[y[x1,x2],{x1,2}]-12D[y[x1,x2],{x2,2}]==1, y[x1,x2],{x1,x2}] Đạo hàm riêng có điều kiện đầu DSolve[{2D[y[x1,x2],x1] + 7D[y[x1,x2],x2]==3, y[x1,0]==x1},y[x1,x2],{x1, x2}]
Giải số phương trình (hệ pt) vi phân NDSolve[eqns., y, {x,x min,x max }] NDSolve[eqns., y, {x,x min,x max },{t,t min,t max }] NDSolve[eqns., {y1,y2}, {x,x min,x max }] Bắt buộc phải có điều kiện đầu hoặc điều kiện biên nsol = NDSolve[{y'[x]-y[x]Cos[x+y[x]]==0,y[0]==1},y,{x,0,30}] y -> InterpolatingFunction[{{0.,30.}},<>] Plot[y[x]/.nsol,{x,0,30}, PlotRange->All, LabelStyle->Large, PlotStyle->Thick] 0.2 Plot[y[x]/.nsol,{x,-10,10}] -->??? 5 10 15 20 25 30 1.0 0.8 0.6 0.4
Phương trình vi phân bậc 2 NDSolve[{y''[x]+y[x]==0,y[0]==1,y'[0]==0},y,{x,10}] Hệ phương trình vi phân NDSolve[{x'[t]==y[t], y'[t]==-x[t], x[0]==1,y[0]==0}, {x, y},{t,0,10}] x -> InterpolatingFunction[{{0.,10.}},<>] y -> InterpolatingFunction[{{0.,10.}},<>] Plot[???] Plot[???]
Phương trình đạo hàm riêng điều kiện biên: dsol = NDSolve[{D[u[t,x], t] == 0.5 D[u[t,x],x,x]+ u[t,x]d[u[t,x],x], u[t,-pi] == u[t, Pi]==0, u[0,x] == Sin[x]}, u, {t,0,2},{x,-pi, Pi}] u -> InterpolatingFunction[{{0.,2.},{-3.14,3.14}},<>] Plot3D[???]
Vector Ma trận Vector/mảng 1 chiều : v = {x, y, z} v[[2]] =??? ; v + a =??? v1={x1,y1,z1}; v2={x2,y2,z2}; v1*v2 --> {x1 x2,y1 y2,z1 z2} Mảng 2 chiều/ma trận : m = {{x1, y1},{x2,y2}} MatrixForm m//matrixform m[[2,1]] =??? Tích vô hướng a*b --> Tích vector a.b -->
Table Table[expr., {i,i max }] Table[expr., {i,i min,i max }] Table[expr., {i,i min,i max, di}] Table[expr., {i,i min,i max },{j,j min,j max }] Table[i^2, {i,10}] --> {1,4,9,16,25,36,49,64,81,100} Table[i+2 j,{i,4},{j,3}]//matrixform Table[a i,j,{i,2},{j,2}]//matrixform
Array Array[f, n] --> f[i] Array[f, {n 1,n 2 }] --> f[i,j] Array[f,{3,2}] {{f[1,1],f[1,2]},{f[2,1],f[2,2]},{f[3,1],f[3,2]}} Table[???] Array[b ## &, {3,3}] Table[???]
Các hàm liên quan đến ma trận Dimensions[] --> kích thước của một ma trận Dimensions[A] --> {3,3} ; Dimensions[B] --> {?,?} Inverse --> Nghịch đảo ma trận Inverse[A] --> A -1 Det --> Định thức Det[A] --> 21
Tr[list] --> Vết của một ma trận Tr[A] --> 13 Tr[list, f] --> Vết của ma trận tác dụng bởi hàm f Tr[A,Times] -->??? Tr[A,List] --> {1,5,7} MatrixPower[m, n] --> Luỹ thừa bậc n của ma trận m MatrixPower[A, 2] --> Transpose[m] --> Ma trận chuyển vị Transpose[A] -->
Eigenvalues[m] --> liệt kê trị riêng của ma trận Eigenvalues[m] {0.656382, 0.0181093, 0.000509145} Eigenvalues[m, k] --> chỉ in ra k trị riêng của ma trận Eigenvalues[m, 2] --> {0.656382, 0.0181093} Eigenvalues[m, -2] -->??? Eigenvectors[m] --> liệt kê vector riêng của ma trận Eigenvectors[m, k] --> chỉ in ra k vector riêng của ma trận Eigenvectors[m, 2] -->
Eigensystem[m] --> liệt kê trị riêng và vector riêng của ma trận Eigensystem[m] Trị riêng : Eigensystem[m][[1]] {0.656382, 0.0181093, 0.000509145} Vector riêng : Eigensystem[m][[2]] Eigensystem[m, k] --> liệt kê k trị riêng và k vector riêng của ma trận Eigensystem[m, 1] {{0.656382},{{-0.703526,-0.549364,-0.450832}}}
DiagonalMatrix[list] Tạo ma trận với các phần tử của list sắp xếp trên đường chéo DiagonalMatrix[{a,b,c}] --> DiagonalMatrix[list,k] Tạo ma trận với các phần tử của list sắp xếp trên đường chéo thứ k DiagonalMatrix[{a,b,c},2] IdentityMatrix[n] --> Ma trận đơn vị bậc n IdentityMatrix[4] -->
2.4 Lập trình trên Mathematica Định nghĩa hàm f[x_,y_, ]:=(exprs.) x, y : đối số hình thức exprs. : liệt kê các biểu thức, các câu lệnh của Mathematica. f[x_]:= Sin[x] + Cos[x] f[a] --> f[pi] -->??? f[x]*sin[x] --> f[a_,b_]:= x/.solve[a x^4+ b x == 0,x] f[1,1] --> {-1., 0., 0.5+ i 0.866025, 0.5- i 0.866025} f[a_,b_,c_]:= x/.solve[a x^2 + b x + c == 0,x] f[0] -->??? f[0,1] -->??? f[0,1,1] -->???
Biến cục bộ u, v : biến cục bộ f[x_,y_, ]:=Module[{u,v, },expr1;expr2; ] exprs. : liệt kê các biểu thức, các câu lệnh của Mathematica. g[a_]:= Module[{b}, b = Sin[a]*Cos[a]; a 2 b] g[x] --> g[b] -->??? SetOptions SetOptions[function, option->value, ] Plot[Sin[x],{x,0,30}, PlotRange->All, LabelStyle->Large, PlotStyle->Thick] SetOptions[Plot,PlotRange->All, LabelStyle->Large, PlotStyle->Thick] Plot[Sin[x],{x,0,30}]; Plot[Cos[x],{x,0,30}]
Cấu trúc lặp Do[expr, {i,i max ] Do[expr, {i,i min,i max,di] t = 2; Do[t = t^2; Print[t],{3}] --> {4, 16, 256} t = 2; Do[Print[t]; t = t^2,{3}] -->??? For[start, test, incr, body] t = 2; For[i=1, i<4, i++, t = t^2; Print[t]] --> {4, 16, 256} While[test, body] t = 2; i = 1; While[i<4, t = t^2; Print[t];i++] Nest[f, expr, n] Nest[(#^2)&, 2, 3] --> 256 Module[{x = 2}, Nest[(#^2)&, x, 3]]
Cấu trúc rẽ nhánh If[condition, T, F] If[condition, Then, Else] f[x_]:= If[x > 0, 1, -1] Điều kiện (Condition) f[x_/;x > 0]:= 1 <==> f[x_]:= 1/;x > 0 f[x_/;x <= 0]:= -1 <==> f[x_]:= -1/;x <= 0 Switch[expr.,form1, val1, form2, val2, ] f[x_]:= Switch[Mod[x,3], 0, a, 1, b, 2, c] f[4] --> b Which[test1, val1, test2, val2, ] f[x_]:= Which[x > 0, 1, x <= 0, -1] Piecewise[val1, cond1, val2, cond2, ] f[x_]:= Piecewise[{{1, x > 0},{-1, x < 0}}, -1] f[x_]:= Piecewise[{1, x > 0}, -1]
Các quy tắc thay thế Quy tắc thay thế -> /.{var1 -> value1, var1 -> value2, } f[x_]:= a Sin[x] + b Cos[a x] f[t] --> f[t]/.a -> 1 --> Quy tắc thay thế :> /.{var1 :> value1, var1 :> value2, } list = {t,t,t,t}/.t :> RandomReal[] {0.262002,0.18452,0.695246,0.302918} Bước 1 : thay thế {t,t,t,t} --> {RandomReal[],RandomReal[],RandomReal[],RandomReal[]} Bước 2 : tính các giá trị list[[1]] = 0.262002 list[[2]] = 0.18452 So sánh hai quy tắc {t,t,t,t}/.t -> RandomReal[] {0.640703, 0.640703, 0.640703, 0.640703}
2.5 Các gói chương trình chuyên dụng Gọi package để sử dụng << PackageName` hoặc Get[PackageName`] Needs[ PackageName`, file ] Package : VectorAnalysis: <<VectorAnalysis` SetCoordinates[system[names]] system/names : Cartesian : Xx, Yy, Zz Cylindrical: Rr, Ttheta, Zz Spherical : Rr, Ttheta, Pphi Grad[f, coordsys] --> gradient Div[f, coordsys] --> divergence Curl[f, coordsys] --> curl/rot -> x, y, z -> r, θ, z -> r, θ, ϕ Các toán tử dùng trong các hệ toạ độ trụ/cầu http://en.wikipedia.org/wiki/nabla_in_cylindrical_and_spherical_coordinates
Phương pháp biến thiên hằng số: << VariationalMethods` Phương trình Euler Lagrange : EulerEquations[f, u[x],x] ele = EulerEquations[Sqrt[1+y'[x]^2],y[x],x] DSolve[ele,y[x],x] --> VariationalD[f, u[x],x] VariationalD[Sqrt[1+y'[x]^2],y[x],x]
Thư viện: http://library.wolfram.com/ http://library.wolfram.com/infocenter/mathsource/ Physics : http://library.wolfram.com/infocenter/mathsource/science/physics/