Beruflich Dokumente
Kultur Dokumente
Haris Javed
The Purpose of this Guide is to inform the fellow students how to use mathematica for a Linear Algebra and
Differential Equation Coarse:
H* We Can Write Comments in mathematica by using H* Text*L command. Many of the Functions can
H* Shift + enter should be used to Obtain a Function, just hitting enter will just take you to
H* We can Assign Variables as a Function, For example typing n=2 will give n the value of 2, Also
H* You Can Also Use In@xD or Out@xD To bring or use old commands, just put the number in x, for
H* Pallettes Can Also be used instead of typing commands, you can use pallettes by clicking Pallettes
H* Pi can be Written as pi without spaces or Π*L
H* infinity can be Written as inf or ¥ *L
2 test mathematica.nb
1 Solving
Solving an Equation With Mathematica
In[2]:= SolveA4x-x2 6,xE H*note that x2 Requires Ctrl+6, and there are two equal signs between the
::x ®
1 32 - 60 Sin
Out[22]= - +
6 13
2 3
12 223 299 + 45 Sin + 3 3 3387 + 570 Sin + 875 Sin - 500 Sin
13
299 + 45 Sin + 3 3 3387 + 570 Sin + 875 Sin2 - 500 Sin3
>,
6 213
J1 + ä 3 N H32 - 60 SinL
:x ®
1
+ -
6 13
2 3
24 223 299 + 45 Sin + 3 3 3387 + 570 Sin + 875 Sin - 500 Sin
J1 - ä 3 N 299 + 45 Sin + 3
13
3 3387 + 570 Sin + 875 Sin2 - 500 Sin3
>,
12 213
J1 - ä 3 N H32 - 60 SinL
:x ®
1
+ -
6 13
2 3
24 223 299 + 45 Sin + 3 3 3387 + 570 Sin + 875 Sin - 500 Sin
J1 + ä 3 N 299 + 45 Sin + 3
13
3 3387 + 570 Sin + 875 Sin2 - 500 Sin3
>>
12 213
test mathematica.nb 3
In[23]:= H*by Adding A N to the Function in input@26D We can change all the exact Value to Approximate
SolveA-6+3x-2x2 +4x3 5 SinHxL,xEN
>,
13
0.132283 299. + 45. Sin + 5.19615 3387. + 570. Sin + 875. Sin2 - 500. Sin3
In[24]:= H* we Can Also Add a . in the Function itself to Create decimalApproximate Values. Note the .
SolveA-6.+3x-2x2 +4x3 5 SinHxL,xE
H0.0330709 + 0.0572804 äL
>,
13
2392. + 360. Sin + 929.516 2.92889 - 1. Sin 2.31282 + 1.17889 Sin + Sin2
H0.0330709 - 0.0572804 äL
>,
13
2392. + 360. Sin + 929.516 2.92889 - 1. Sin 2.31282 + 1.17889 Sin + Sin2
>>
13
0.0661417 2392. + 360. Sin + 929.516 2.92889 - 1. Sin 2.31282 + 1.17889 Sin + Sin2
In[34]:= Solve@87x-4y-8,-1x+3y18<D
::x ® >>
48 118
Out[34]= ,y®
17 17
4 test mathematica.nb
::x ® - >>
43 93
Out[96]= ,y®
67 67
Out[97]= 88x ® - 0.641791, y ® 1.38806<<
In[102]:= Clear@equD;equ=82x-1y-3z-4,-5x+2y-18z2,4x+y+2z-3<;Solve@equD
Solve@N@equDD
NSolve@equD
::x ® - >>
162 127 43
Out[102]= ,y® ,z®
145 145 145
Out[103]= 88x ® - 1.11724, y ® 0.875862, z ® 0.296552<<
In[118]:= N@Out@117DD
In[131]:= Clear@equD;equ=83 x-2 y-7 z==-4,-3 x+2 y-8 z==1,2 x+11 y+6 z==-3<;Solve@equD
Solve@N@equDD
NSolve@equD
test mathematica.nb 5
In[134]:= ContourPlot3D@83 x-2 y-7 z==-4,-3 x+2 y-8 z==1,2 x+11 y+6 z==-3<,8x,-5,5<,8y,-5,5<,8z,-5,5<D
5
-5
5
Out[134]=
0
-5
-5
5
6 test mathematica.nb
2 Roots
Out[25]= 8x ® 1.16058<
H* above is the Excat root, it does not include all the roots that are found by using the Solve *L
Out[27]= 8x ® 1.03301<
y
10
Out[28]=
x
-10 -5 5 10
-5
-10
Out[135]= 8x ® 2.81381<
test mathematica.nb 7
3 Graphing
Out[3]=
x
-4 -2 2 4
-5
H*Also Lets Plot This function and see how it looks like*L
5 5
x
-3 -2 -1 1 2 3
-5
H* We will Plot another function just to get the hang of the method*L
PlotA92x4 +x3 -7x2 +3x-8=,8x,-20,20<, AxesLabel® 8x,y<,PlotRange ® 8-20,20<E
In[5]:=
H* The image Below can be Shrinked, or Expanded with the use of the Mouse cursors. Click on the
y
20
10
Out[5]=
x
-20 -10 10 20
-10
-20
test mathematica.nb 9
sin2 HxL
1.0
0.8
0.6
Out[30]=
0.4
0.2
1 2 3 4 5 6
H* the Original Function Was x- ==1- x*L PlotB: x- ,1- x>,8x,-.5,1.5<,PlotRange® 8-.5,1.2<
2 1 3
6 2 1
3 3 2
3 3 2
1.0
0.5
Out[6]=
-0.5
H5-6xL
>,8x,-2,4<F H* Original function 4-x=
H5-6xL
In[7]:= PlotB:4-x, *L
2
2
Out[7]= -2 -1 1 2 3 4
-5
10-10
-5
5 0
5
0
10
-5 10
-10
5
Out[8]=
-5
-10
test mathematica.nb 11
1 4
Out[151]=
0
2
-1
-4
0
-2
2 -2
12 test mathematica.nb
3-D Plotting
H*Mathematica will plot 3d Graphs With ease look at the Following Example*L
Plot3D@Sin@x+y^2D,8x,-3,3<,8y,-2,2<,Mesh® AllDH*This Plot can be moved Scaled by clicking on
1.0
0.5
Out[18]=
0.0 2
-0.5
-1.0 1
-2
-1
-2
1.0
0.5
0.0
-0.5
-1.0
-2
2
-1
1
0
0
1
-1
2 -2
14 test mathematica.nb
In[20]:= H*Note that contour plot will plot a 3d function if we have more than 2 variables, if we only
ContourPlot@88x+2y-3,-8x+3y11<,8x,-6,3<,8y,-2,4<,Axes® True,Frame® False,Mesh® AllD
Out[20]= 1
-6 -4 -2 2
-1
-2
test mathematica.nb 15
In[138]:= GraphPlot3D@Table@i -> Mod@i^2, 75D, 8i, 75<D, SelfLoopStyle -> None,
MultiedgeStyle -> None,
EdgeRenderingFunction -> H8Green, Cylinder@ð, 0.1D< &L,
VertexRenderingFunction -> H8Black, Sphere@ð, 0.2D< &LD
Out[138]=
16 test mathematica.nb
H5xL J N
1
In[174]:=
2 + 2x2
1.4
1.2
1.0
0.8
Out[174]=
0.6
0.4
0.2
0.0
0 5 10 15
test mathematica.nb 17
4 Matrix
2 -5 -7 -4
In[32]:= RowReduceB -3 2 -15 1 FMatrixForm
2 1 6 -3
Out[32]//MatrixForm=
352
1 0 0 -
163
79
0 1 0 -
163
49
0 0 1
163
H* We have defined n As the Matrix above, be careful about lower and uppercase. A lot of Commands
In[61]:= H* A Equation can Also be put as a linear system and be solved, 89x+2y-3,-2x+7y11< can be put
::x ® - >>
43 93
Out[61]= ,y®
67 67
18 test mathematica.nb
F
9 2 -3
In[62]:= RowReduceB
-2 7 11
In[105]:= Clear@equD
2 -5 -7
In[109]:= Clear@m,u,v,wD;m= -1 8 -5 ;
9 4 6
u=8-8,1,-2<;v=812,4,4<;
w=83,5,-8<;f=LinearSolve@mD
Out[112]//MatrixForm=
- 704
863
1148
863
- 434
863
353
863
- 100
863
122
863
533 1080
863
- 863 - 581
863
In[113]:= 8u,v,w<MatrixForm
Out[113]//MatrixForm=
-8 1 -2
12 4 4
3 5 -8
In[114]:= Join@m,8u,v,w<DMatrixForm
Out[114]//MatrixForm=
2 -5 -7
-1 8 -5
9 4 6
-8 1 -2
12 4 4
3 5 -8
In[115]:= n=Transpose@Append@Append@Append@Transpose@mD,uD,vD,wDDMatrixForm
Out[115]//MatrixForm=
2 - 5 - 7 - 8 12 3
-1 8 -5 1 4 5
9 4 6 -2 4 -8
In[116]:= RowReduce@Out@115DDMatrixForm
Out[116]//MatrixForm=
704 1148 434
1 0 0 - -
863 863 863
353 100 122
0 1 0 -
863 863 863
533 1080 581
0 0 1 - -
863 863 863
test mathematica.nb 19
In[119]:= Clear@A,A1,b,cD;A=889,7,3<,88,5,1<,87,5,5<<;b=8-2,5,3<;c=81,-1,0<;A1=MatrixForm@889,7,3<,88,5
3 + A2
889, 8, 7<, 87, 5, 5<, 83, 1, 5<<
TransposeB 8 5 1 F
9 7 3
7 5 5
999T , 7T , 3T =, 98T , 5T , 1=, 97T , 5T , 5T ==
8881, 49, 9<, 864, 25, 1<, 849, 25, 25<<
2
9 7 3
8 5 1
7 5 5
In[123]:= Clear@BD;MatrixForm@B=88-3,5,19<,80,1,5<,86,-18,27<<D
Out[123]//MatrixForm=
- 3 5 19
0 1 5
6 - 18 27
In[124]:= H* Determinant*L
Det@Out@123DD
Out[124]= - 315
In[126]:= B+1MatrixForm
Out[126]//MatrixForm=
- 2 6 20
1 2 6
7 - 17 28
In[128]:= BT MatrixForm
Out[128]//MatrixForm=
H- 3LT 5T 19T
0T 1 5T
6T H- 18LT 27T
20 test mathematica.nb
In[130]:= Transpose@BDMatrixForm
Out[130]//MatrixForm=
-3 0 6
5 1 - 18
19 5 27
In[216]:= Inverse@BDMatrixForm
Out[216]//MatrixForm=
13 53 2
- -
35 35 105
2 13 1
- -
21 21 21
2 8 1
105 105 105
In[218]:= LinearSolve@BD
In[219]:= NullSpace@BD
Out[219]= 8<
In[222]:= Eigenvalues@N@BDDMatrixForm
Out[222]//MatrixForm=
27.5269
- 4.87447
2.34761
In[226]:= Eigenvectors@N@BDDSimplifyMatrixForm
Out[226]//MatrixForm=
0.54023 0.155871 0.826956
0.986349 0.10673 - 0.125397
- 0.877241 - 0.46351 - 0.124926
test mathematica.nb 21
Graphics3D@8
AstronomicalData@ð, "OrbitPath"D & AstronomicalData@"Planet"D,
In[147]:=
8Orange,
Opacity@.5D, HAstronomicalData@ð, "OrbitPath"D &
AstronomicalData@"ApolloAsteroid"DL<
<, PlotRange -> 88-6, 6<, 8-6, 6<, 8-4, 4<<, Boxed -> False,
ViewAngle -> Π11D
Out[147]=
22 test mathematica.nb
5
derivatives
and integrals
Differentiation
H*Remember to Always clear your Variables*L
Clear@xD;Clear@nD
Out[161]= n x-1+n
Out[162]= H- 3 + nL H- 2 + nL H- 1 + nL n x-4+n
x3
In[163]:= DB ,xF
3Tan@xD
1
Out[163]= x2 Cot@xD - x3 Csc@xD2
3
Manipulate@D@2xn ,xD,8n,1,20,1<DH* This makes a nice Differential Calculator, which can Differentiate
n
Out[167]=
Integration
In[164]:= IntegrateAx2 ,xE
x3
Out[164]=
3
à x âx
2
In[165]:=
x3
Out[165]=
3
test mathematica.nb 23
Out[166]=
2 x3
3
1
In[168]:= IntegrateB ,xF
1-x3
F
1+2 x
ArcTanB
LogA1 + x + x2 E
3 1 1
Out[168]= - Log@- 1 + xD +
3 3 6
1
In[170]:= ManipulateBIntegrateB ,xF,8n,1,20,1<FTiming
1-xn
2 J5 + 5 N ArcTanB F+
1 1- 5 +4x
2
2 J5 + 5 N
20
>
F - 4 Log@- 1 + xD -
1+ 5 +4x
2 10 - 2 5 ArcTanB
10 - 2 5
J- 1 + 5 N LogB1 - J- 1 + 5 N x + x2 F + J1 + 5 N LogB1 + J1 + 5 N x + x2 F
1 1
2 2
à
1
In[171]:= âx
x3 -1
F
1+2 x
ArcTanB
LogA1 + x + x2 E
3 1 1
Out[171]= - + Log@- 1 + xD -
3 3 6
24 test mathematica.nb
In[172]:= Plot@%,8x,0,2<D
-1.2
-1.4
Out[172]=
-1.6
6 vector
fields and
differential
equations
In[183]:= VectorPlotA9ã-12x Sin@2yD, 2y Cos @5xD=, 8x,-Π, Π<, 8y, -Π, Π<, Axes ® True, AxesLabel® 8x,y<,
Out[183]=
x
-3 -2 -1 1 2 3
-1
-2
-3
26 test mathematica.nb
In[187]:= VectorPlot@8Sin@6yD, -y Cos @6xD<, 8x,-Π, Π<, 8y, -Π, Π<, Axes ® True, AxesLabel® 8x,y<, Frame D
Out[187]=
x
-3 -2 -1 1 2 3
-1
-2
-3
Out[192]= 0 x
-1
-2
-3
-3 -2 -1 0 1 2 3
H* Lets Solve some differential Equations now we will use DSolve Command which Solves 2 Simultaneous
DSolve@8y@xD -z'@xD,z@xD -y'@xD<,8y,z<,xDH* This equation involves no boundary conditions*L
In[193]:= DSolve@2y@tD+2y'@tD+y''@tD==2t,y@tD,tD@@1DD
In[199]:= Clear@fD
c1 Cos@5tD c2 Sin@5tD
In[203]:= f@t_D:=t+ + -9
ã-t ã-5t
f@tD.8c1® 1,c2® 1<
Out[205]= :5.641,
20
10
-3 -2 -1 1 2 3
>
-10
-20
-30
-40
28 test mathematica.nb
In[206]:= Plot@Evaluate@Table@f@xD,8c2,-2,2<,8c1,-2,2<DD,8x,-2,3<DTiming
200
100
-100
-200
-10 -5 0 5 10
40
20
10
10
0
0
-10
In[207]:= y@tD.DSolve@2y@tD+2y'@tD+y''@tD==2Sin@tD,y@tD,tD@@1DDSimplify
4 2
Out[208]= - + ã-t C@2D Cos@tD + + ã-t C@1D Sin@tD
5 5
30 test mathematica.nb
In[209]:= y@tD.DSolve@2y@tD+2y'@tD+y''@tD==2Sin@tD,y@tD,tD@@1DDN
H*Initial Conditions*L
DSolveA9y''@xD-4y'@xD+3y@xD ãH3xL -5Cos@3xD,y@0D1,y'@0D1 =,y@xD,xESimplify
In[212]:=
::y@xD ®
1 1
Out[214]= x C@1D CosB 3 Log@xDF + x C@2D SinB 3 Log@xDF>>
2 2