You are on page 1of 4

Tm tt mt s lnh c bn ca Mathematica

ng Nguyn Phng

Ngy 29 thng 11 nm 2013


Cc chi tit c th v v d c th c xem ti
http://reference.wolfram.com/mathematica/guide/Mathematica.html
1 C bn
Cc php tnh: +, , (hay khong trng), /,(hm m),...
In [ 1] : = 2^2
Out [ 1] = 4
Php so snh: >, <, ==, >=, <=, ! = (ss khng bng),...
Php gn: =, := (gn tr)
Cp ngoc vung [] c s dng bao cc tham s ca hm; cp ngoc trn () c s dng nhm cc
biu thc ton hc; v cp ngoc nhn {} c s dng bao cc phn t ca mt tp hp.
Cc hm ton hc: Sin[], ArcCos[], Sqrt[],...
In [ 1] : = Sqrt [ 2 ]
Out [ 1] =

2
Nhn kt qu s: % // N (% c dng i din cho kt qu nhn c trc )
In [ 1] : = 1/2
Out [ 1] =
1
2
In [ 2] : = % // N
Out [ 2] = 0. 5
hay N[expression,precision]
In [ 1] : = N[ 1 / 2 , 3 ]
Out [ 1] = 0. 500
i s
Khai trin: Expand[expression]
In [ 1] : = Expand [ ( x + 1) ^2]
Out [ 1] = 1 + 2 x + x^2
Rt gn: Factor[expression]
In [ 1] : = Factor [ x^2 2x + 1]
Out [ 1] = (1 + x) ^2
Vi tch phn
o hm: D[expression,variable]
In [ 1] : = D[ x^5 3xy 2 , x ]
Out [ 1] = 5 x^4 3 y
Tch phn: Integrate[expression,variable] hay Integrate[expression,{variable,min,max}]

dnphuong1984@gmail.com
1
ng Nguyn Phng Ti liu ni b NMTP
In [ 1] : = I nt e gr at e [ 28 x + 10y , x ]
Out [ 1] = 14 x^2 + 10 x y
In [ 2] : = I nt e gr at e [ 28 x + 10y , {x , 0 , 10}]
Out [ 2] = 1400 + 100 y
Tng: Sum[expression,variable] hay Sum[expression,{variable,min,max}]
In [ 1] : = Sum[ x , x ]
Out [ 1] =
1
2
(1 + x) x
In [ 2] : = Sum[ x^2 , {x , 0 , 10}]
Out [ 2] = 385
2 List, table
List: List[item
1
,item
2
,...]
In [ 1] : = Li s t [ a , b , c , d ]
Out [ 1] = {a , b , c , d}
Table: Table[expression,{variable,min,max,step}] or Table[expression,{list}]
In [ 1] : = Tabl e [ i^2 , {i , 10}]
Out [ 1] = {1 , 4 , 9 , 16 , 25 , 36 , 49 , 64 , 81 , 100}
V list v table: ListPlot[list_or_table]
In [ 1] : = Li s t Pl ot [ Tabl e [ { Si n [ n ] , Si n [ 2 n ] } , {n , 5 0 } ] ]
3 Hm, phng trnh
nh ngha hm: function_name[variable_]:= definition
In [ 1] : = f [ x_ ] := x^2 Si n [ x ]
In [ 2] : = f [ 1 ]
Out [ 2] = 1 Si n [ 1 ]
In [ 3] : = % // N
Out [ 3] = 0. 158529
Trong trng hp hm l biu thc i s, ta c th s dng ton t thay th (/.) thay v khai bo mt hm
In [ 1] : = x^2 Si n [ x ]
Out [ 1] = x^2 Si n [ x ]
In [ 2] : = % /. x > 1
Out [ 2] = 1 Si n [ 1 ]
In [ 3] : = % // N
Out [ 3] = 0. 158529
Gii phng trnh: Solve[equation,variable]
In [ 1] : = f = x^4 3x^2 + 1
Sol ve [ f == 0 , x ]
Out [ 1] = 1 3 x^2 + x^4
Out [ 2] = {{x >
1
2
(1

5) } , {x >
1
2
(1

5) } ,
{x >
1
2
(1 +

5) } , {x >
1
2
(1 +

5) }}
2
ng Nguyn Phng Ti liu ni b NMTP
Gii s phng trnh: NSolve[equation,variable]
In [ 1] : = NSolve [ x^4 3x^2 + 1 == 0 , x ]
Out [ 1] = {{x > 1. 61803} , {x > 0. 618034} , {x > 0. 618034} , {x > 1. 61803}}
Gii phng trnh vi phn: DSolve[equation,dependent_variable,independent_variable]
In [ 1] : = DSolve [ y [ x ] + y [ x ] == a Si n [ x ] , y [ x ] , x ]
Out [ 1] = {{y [ x ] > E
x
C[ 1 ] +
1
2
a (Cos [ x ] + Si n [ x ] ) }}
4 th
th hm 1 chiu: Plot[function,{variable,min,max}]
In [ 1] : = Pl ot [ Si n [ x ] , {x , 0 , 6Pi }]
V cng lc nhiu th: Plot[{f
1
,f
2
,...},{variable,min,max}]
In [ 1] : = Pl ot [ { Si n [ x ] , Si n [ 2 x ] , Si n [ 3 x ] } , {x , 0 , 2Pi } , PlotLegends > " Expr es s i ons " ]
th hm 2 chiu dng contour: ContourPlot[f,{variable
1
,min
1
,max
1
},{variable
2
,min
2
,max
2
}]
In [ 1] : = ContourPl ot [ Cos [ x ] + Cos [ y ] , {x , 0 , 4 Pi } , {y , 0 , 4 Pi }]
th hm 2 chiu dng 3D: Plot3D[f,{variable
1
,min
1
,max
1
},{variable
2
,min
2
,max
2
}]
In [ 1] : = Plot3D [ Cos [ x ] + Cos [ y ] , {x , 0 , 4 Pi } , {y , 0 , 4 Pi }]
3
ng Nguyn Phng Ti liu ni b NMTP
5 Lnh iu kin
iu kin If: If[condition,t,f,u] (cho t nu condition c tha, f nu condition khng tha, v u nu
khng nm trong 2 trng hp trn).
In [ 1] : = abs [ x_ ] := I f [ x < 0 , x , x ]
abs [ 1. 5]
Out [ 2] = 1. 5
iu kin Switch: Switch[expression,form
1
,value
1
,form
2
,value
2
,...] (so snh expression vi form
1
,
form
2
,... v tr v gi tr value
i
tng ng vi form
i
u tin trng).
In [ 1] : = r [ x_ ] := Swi tch [ Mod[ x , 2] , 0 , Pr i nt [ "Even number" ] , 1 ,
Pr i nt [ "Odd number" ] ]
In [ 2] : = r [ 3 ]
Odd number
In [ 3] : = r [ 1 0 ]
Even number
6 Vng lp
Vng lp For: For[start,condition,increase,body] (khi u vi start, lp li body v tng theo increase
cho n khi condition khng cn tha)
In [ 1] : = For [ i = 1; t = x , i^2 < 10 , i++, t = t^2 + i ; Pr i nt [ t ] ]
1+x
2
2+(1+x
2
)
2
3+(2+(1+x
2
)
2
)
2
Vng lp Do: Do[expression,{variable,min,max,step}] (nh gi biu thc expression vi bin variable
tng t min n max)
In [ 1] : = Do[ Pr i nt [ { i , j }] , {i , 4} , {j , i 1}]
{2 , 1}
{3 , 1}
{3 , 2}
{4 , 1}
{4 , 2}
{4 , 3}
hay Do[expression,{variable,list}] (s dng gi tr t trong list)
In [ 1] : = list = {2 , 3 , 5}
Out [ 1] = {2 , 3 , 5}
In [ 2] : = Do[ Pr i nt [ { i , j }] , {i , list } , {j , i 1}]
{2 , 1}
{3 , 1}
{3 , 2}
{5 , 1}
{5 , 2}
{5 , 3}
{5 , 4}
Vng lp While: While[condition,body] (nh gi condition v sau lp body cho n khi condition
khng cn tha)
In [ 1] : = x = 3
Out [ 1] = 3
In [ 2] : = While [ x > 0 , Pr i nt [ x ] ; x = x 1]
3
2
1
4

You might also like