Introduction To Mathematica: By: Dr. Rajai Alassar
Introduction To Mathematica: By: Dr. Rajai Alassar
Introduction To Mathematica: By: Dr. Rajai Alassar
nb 1
Introduction To Mathematica
By: Dr. Rajai Alassar
E-mail:alassar@kfupm.edu.sa Homepage:
http://faculty.kfupm.edu.sa/math/alassar
Numerical Calculations
Addition, Subtraction, Multiplication, Division, and Powers
In[1]:= 1+2
Out[1]= 3
In[2]:= 3−9
Out[2]= −6
Out[3]= −12
In[4]:= −2 6
Out[4]= −12
In[5]:= 8ê2
Out[5]= 4
In[6]:= 7ê3
7
Out[6]=
3
In[7]:= 2^3
Out[7]= 8
In[8]:= 2 ^ H1 ê 3L
Out[8]= 21ê3
5 ^ H1 ê 2L
è!!!
In[9]:=
Out[9]= 5
Approximating Numbers
"####
To approximate 21ê3 or 5 , we can use N as
In[10]:= N@2 ^ H1 ê 3LD
Out[10]= 1.25992
Introduction to Mathematica by Dr Rajai Alassar.nb 3
Out[11]= 1.25992104989487316476721060727822835057025146470150798008197511215529967651
395948372939656243625509415431025603561566525939902404061373722845911030426
935524696064261662500097747452656548030686718540551868924587251676419937370
969509838278316139915512931369536618394746344857657030311909589598474110598
116290705359081647801147352132548477129788024220858205325797252666220266900
566560819947156281764050606648267735726704194862076214429656942050793191724
41480920448232840127470321964282081201905714188996
In[12]:= 5 ^ H1 ê 2L êê N
Out[12]= 2.23607
In[13]:= 2 + 3ê4
11
Out[13]=
4
In[14]:= 2. + 3 ê 4
Out[14]= 2.75
In[15]:= 2 ^ 100
Out[15]= 1267650600228229401496703205376
In[16]:= 2. ^ 100
Order of Operations
Brackets -> Powers -> Multiplication/Division -> Addition/Subtrac-
tion
In[17]:= 4 + 8ê2
Out[17]= 8
In[18]:= H4 + 8L ê 2
Out[18]= 6
In[19]:= −3 ^ 2 ê 4
9
Out[19]= −
4
In[20]:= H−3L ^ 2 ê 4
9
Out[20]=
4
−3 ^ H2 ê 4L
è!!!
In[21]:=
Out[21]= − 3
Introduction to Mathematica by Dr Rajai Alassar.nb 4
H−3L ^ H2 ê 4L
è!!!
In[22]:=
Out[22]= 3
"#######
Note : ‰ = -1
Built-In Constants
In[24]:= Pi + π
Out[24]= 2π
In[25]:= N@PiD
Out[25]= 3.14159
In[26]:= E^2 + 2
2
Out[26]= 2
Out[27]= 2.7182818284590452354
In[28]:= I∗
Out[28]= −1
H − 1L4
In[29]:=
1+
Out[29]= −2 + 2
In[30]:= Infinity + ∞
Out[30]= ∞
Built-In Functions
The names of built-in Mathematica functions begin with capital
letters
The arguments of Mathematica functions are enclosed in square
brackets
Introduction to Mathematica by Dr Rajai Alassar.nb 5
In[31]:= Exp@−2D
1
Out[31]= 2
E
1
In[32]:= NA
2
Out[32]= 0.135335
In[33]:= Abs@−2D
Out[33]= 2
è!!!
In[34]:= Sqrt@2D
Out[34]= 2
In[35]:= Factorial@4D
Out[35]= 24
In[36]:= 4!
Out[36]= 24
Out[37]= Log@2D
In[38]:= Log@ED
Out[38]= 1
Out[39]= 2
Log@10D
Out[40]=
Log@2D
Out[41]= 3.32193
E + SecA E
π π
In[42]:= CosA
è!!!
4 4
è!!! + 2
1
Out[42]=
2
Introduction to Mathematica by Dr Rajai Alassar.nb 6
cos J N
π
In[43]:=
4
General::spell1 :
Possible spelling error: new symbol name "cos" is similar to existing symbol "Cos". More…
cos π
Out[43]=
4
E + CotA E
π π
In[44]:= TanA
è!!!
3 3
è!!! + 3
1
Out[44]=
3
E
π
In[45]:= Sin@30D + CscA
6
Out[45]= 2 + Sin@30D
In[46]:= Sin@30D êê N
Out[46]= −0.988032
Out[47]= 0.5
In[48]:= Sin@π ê 4D ^ 2
1
Out[48]=
2
Sin2@ 4 D
π
Out[49]=
E
5π
In[50]:= SinA
4
− è!!!
1
Out[50]=
2
ArcSinA− è!!!! E
1
In[51]:=
2
π
Out[51]= −
4
In[52]:= Sinh@−5D êê N
Out[52]= −74.2032
In[53]:= ArcSinh@−74.2D
Out[53]= −4.99996
Introduction to Mathematica by Dr Rajai Alassar.nb 7
Out[54]= 3
In[55]:= Mod@11, 3D
Out[55]= 2
In[56]:= Random@D
Out[56]= 0.13218
è!!!!!!
In[57]:= MaxA1, −3, 4.01, 2, 17 , 6.2 ∗ Abs@Sin@7DDE
è!!!!!!
Out[57]= 17
In[58]:= FactorInteger@1627956652000D
Out[58]= 882, 5<, 85, 3<, 87, 1<, 817, 2<, 823, 1<, 88747, 1<<
The functions:
Re: returns the real part of a complex number
Im: returns the imaginary part of a complex number
Others are self-explanatory
In[59]:= Re@1 + 2 D
Out[59]= 1
In[60]:= Im@1 + 2 D
Out[60]= 2
Abs@1 + 2 D
è!!!
In[61]:=
Out[61]= 5
In[62]:= Conjugate@1 + 2 D
Out[62]= 1−2
In[63]:= Arg@1 + 2 D
Out[63]= ArcTan@2D
Introduction to Mathematica by Dr Rajai Alassar.nb 8
In[64]:= Arg@1 + 2 D êê N
Out[64]= 1.10715
0.5
2 4 6 8 10 12
-0.5
-1
Out[65]= Graphics
Out[66]= 4
Out[67]= 5
In[68]:= 2∗% + 7
Out[68]= 17
Out[69]= 29
Defining Variables
x = value assign a value to x
x = y = value assign a value to both x and y
In[70]:= x=4
Out[70]= 4
Introduction to Mathematica by Dr Rajai Alassar.nb 9
è!!!!
In[71]:= z= x
Out[71]= 2
In[72]:= x=y=5
Out[72]= 5
In[73]:= x
Out[73]= 5
In[74]:= y^3
Out[74]= 125
In[75]:= y=7
Out[75]= 7
In[76]:= x+y+z
Out[76]= 14
In[78]:= x
Out[78]= x
In[79]:= y
Out[79]= 7
In[80]:= y =.
In[81]:= y
Out[81]= y
In[82]:= z =.
Note:
Values assigned are permanent until you clear them or start a new
session
No limit on the length of names
Names can not start with numbers (x2 is a name but 2x means
2*x )
x y means x*y
xy with no space means the variable with name xy
Introduction to Mathematica by Dr Rajai Alassar.nb 10
Out[83]= 3
In[84]:= v = 7;
In[85]:= u = 2; v = 3; w = 5
Out[85]= 5
In[86]:= v
Out[86]= 3
In[87]:= u = 1; v = 2; w = 3;
In[88]:= Clear@u, v, wD
In[89]:= u
Out[89]= u
−2 − x + 2 x2 + x3
Out[91]=
−4 − 4 x + x2 + x3
Introduction to Mathematica by Dr Rajai Alassar.nb 11
Out[92]= 16
In[93]:= ahmad ê. x → 4
3
Out[93]=
2
When you first define a function, you must enclose the argument
by square brackets and place an underscore "_ " after the argu-
ment on the left hand side of the equal sign. When evaluating the
function, do not use the underscore.
In[94]:= f@x_D = x2
Out[94]= x2
In[95]:= f@2D
Out[95]= 4
è!!!!
g@x_D =
è!!!
In[96]:= x
Out[96]= x
In[97]:= g@xD ê. x → 9
Out[97]= 3
f@x + hD − f@xD
In[98]:=
h
−x2 + Hh + xL2
Out[98]=
h
In[99]:= Simplify@%D
Out[99]= h+2x
Out[100]=
x
In[101]:=
Composition@f, gD@xD
Out[101]=
x
In[102]:=
Clear@f, gD
f@x_D := x2 + 1 ê; x ≥ 0
In[103]:=
f@x_D := −x − 1 ê; x < 0
-3 -2 -1 1 2 3
Out[105]=
Graphics
In[106]:=
Clear@fD
1 − Sin@x2 + y2 D
Out[107]=
Introduction to Mathematica by Dr Rajai Alassar.nb 13
è!!!!
In[108]:=
è!!!! 3
E
π
fA2 π,
2
Out[108]=
1 − è!!!
1
2
In[109]:=
f@0, aD
1 − Sin@a2 D
Out[109]=
8t2 , 1 − t2 <
Out[111]=
In[112]:=
f@1D
81, 0<
Out[112]=
In[113]:=
f@Sin@cDD
In[115]:=
g@1, 2D
8Cos@3D, −Sin@3D<
Out[115]=
In[116]:=
g@π, −πD
81, 0<
Out[116]=
g@s, tD ê. 8s → 1, t → 2<
In[117]:=
8Cos@3D, −Sin@3D<
Out[117]=
Introduction to Mathematica by Dr Rajai Alassar.nb 14
Expand@H1 + xL2 D
In[118]:=
Out[118]=
1 + 2 x + x2
H−1 + xL2 H2 + xL
In[119]:=
E
H−3 + xL2 H1 + xL
ExpandA
Out[119]=
Factor@12 x2 + 27 x y − 84 y2 D
In[120]:=
3 H4 x − 7 yL Hx + 4 yL
Out[120]=
In[121]:=
Factor@1 + 4 x + 6 x2 + 4 x3 + x4 + 12 y + 36 x y + 36 x2 y +
12 x3 y + 54 y2 + 108 x y2 + 54 x2 y2 + 108 y3 + 108 x y3 + 81 y4 D
H1 + x + 3 yL4
Out[121]=
In[122]:=
E
4 H−1 + xL 4 H1 + xL 2 H1 + x2 L
1 1 1
SimplifyA − −
Out[122]=
1
−1 + x4
In[123]:=
E
−7 − 6 x + 5 x2
H−3 + xL2 H1 + xL
ApartA
Out[123]=
E
−7 − 6 x + 5 x2
H−3 + xL2 H1 + xL
ExpandA
Out[124]=
In[125]:=
E
2 x2
TogetherA −
x2 2
Out[125]=
4 − x4
2 x2
In[126]:=
saleh = Expand@H3 + 2 x + yL ^ 3D
Out[126]=
27 + 54 x + 36 x2 + 8 x3 + 27 y + 36 x y + 12 x2 y + 9 y2 + 6 x y2 + y3
In[127]:=
saleh
Out[127]=
27 + 54 x + 36 x2 + 8 x3 + 27 y + 36 x y + 12 x2 y + 9 y2 + 6 x y2 + y3
Introduction to Mathematica by Dr Rajai Alassar.nb 16
In[128]:=
Collect@saleh, yD
27 + 54 x + 36 x2 + 8 x3 + H27 + 36 x + 12 x2 L y + H9 + 6 xL y2 + y3
Out[128]=
In[129]:=
Collect@saleh, xD
27 + 8 x3 + 27 y + 9 y2 + y3 + x2 H36 + 12 yL + x H54 + 36 y + 6 y2 L
Out[129]=
Coefficient@saleh, x2 D
In[130]:=
Out[130]=
36 + 12 y
In[131]:=
Exponent@saleh, yD
Out[131]=
3
One can also use the Palettes to evaluate expressions. First type
the expression. Then, highlight it or select it. Finally, move the
cursor on the palette and chose your command. Now try factoring
the following expression in place by using the 'AlgebraicManipula-
tion' Palette from the file menu
In[132]:=
1 + x6
Out[132]=
1 + x6
Equations
We use the 'double equals' = = in Mathematica to specify an
equation
In[133]:=
Solve@x2 + x − 2 0D
In[134]:=
eqn1 = x3 + x2 + x + 1 0
Out[134]=
1 + x + x2 + x3 0
Introduction to Mathematica by Dr Rajai Alassar.nb 17
In[135]:=
eqn1
Out[135]=
1 + x + x2 + x3 0
In[136]:=
Solve@eqn1D
In[137]:=
Solve@x2 + 2 x − 7 0D
è!!! è!!!
99x → −1 − 2 2 =, 9x → −1 + 2 2 ==
Out[137]=
In[138]:=
N@%D
In[139]:=
Solve@x6 − 1 0D
88x → −1<, 8x → 1<, 8x → −H−1L1ê3 <, 8x → H−1L1ê3 <, 8x → −H−1L2ê3 <, 8x → H−1L2ê3 <<
Out[139]=
In[140]:=
N@%D
In[141]:=
Factor@x6 − 1D
H−1 + xL H1 + xL H1 − x + x2 L H1 + x + x2 L
Out[141]=
In[142]:=
eqn2 = Sin@xD2 − 2 Sin@xD − 3 0
Out[142]=
−3 − 2 Sin@xD + Sin@xD2 0
In[143]:=
Solve@eqn2D
In[144]:=
Solve@eqn2, xD
99x → − =, 8x → ArcSin@3D<=
Out[144]=
π
2
In[145]:=
N@%D
Systems of Equations
99x → ==
Out[146]=
3 1
,y→
2 2
sys1 = 82 x − 3 y + 4 z
In[147]:=
2, 3 x − 2 y + z 0, x + y − z 1<
82 x − 3 y + 4 z
Out[147]=
2, 3 x − 2 y + z 0, x + y − z 1<
99x → ==
Out[148]=
7 9 3
,y→ ,z→
10 5 2
sys1 ê. solsys1
In[149]:=
sys2 = 82 x − 2 y − 2 z
In[150]:=
−2, −x + y + 3 z 0, −3 x + 3 y − 2 z 1<
82 x − 2 y − 2 z
Out[150]=
−2, −x + y + 3 z 0, −3 x + 3 y − 2 z 1<
8<
Out[151]=
Introduction to Mathematica by Dr Rajai Alassar.nb 19
sys3 = 8−2 x + 2 y − 2 z
In[152]:=
−2, 3 x − 2 y + 2 z 2, x + 3 y − 3 z −3<
8−2 x + 2 y − 2 z
Out[152]=
−2, 3 x − 2 y + 2 z 2, x + 3 y − 3 z −3<
Solve::svars : Equations may not give solutions for all "solve" variables. More…
88x → 0, y → −1 + z<<
Out[153]=
sys4 = 8a x + y 0, 2 x + H1 − aL y
In[154]:=
1<
8a x + y 0, 2 x + H1 − aL y
Out[154]=
1<
In[155]:=
Eliminate@sys4, yD
H2 − a + a2 L x
Out[155]=
1
è!!!!!
To determine the initial guess, you may want to plot the graph
Example: Find a root of f(x)=lnx+sin(x+1)- x -1
Introduction to Mathematica by Dr Rajai Alassar.nb 20
è!!!!
x − 1, 8x, 0, 20<E
In[157]:=
PlotALog@10 x + 1D + Sin@x + 1D −
1.5
0.5
5 10 15 20
-0.5
-1
Out[157]=
Graphics
è!!!!
0, 8x, 7<E
In[158]:=
FindRootALog@10 x + 1D + Sin@x + 1D − x −1
8x → 14.8616<
Out[158]=
Calculus
Limits
Limit[exp,x->x0] finds the limiting value of expr when x
approaches x0
In[159]:=
Limit@x2 − 3 x + 2, x → 3D
Out[159]=
2
Introduction to Mathematica by Dr Rajai Alassar.nb 21
In[160]:=
0.8
0.6
0.4
0.2
-4 -2 2 4
-0.2
Out[160]=
Graphics
In[161]:=
Sin@xD
LimitA , x → 0E
x
Out[161]=
1
In[162]:=
x3 − 1
w=
x−1
Out[162]=
−1 + x3
−1 + x
-2 -1 1 2
Out[163]=
Graphics
Introduction to Mathematica by Dr Rajai Alassar.nb 22
w ê. x → 1
In[164]:=
1
Power::infy : Infinite expression
0
encountered. More…
In[165]:=
Factor@Numerator@wDD
H−1 + xL H1 + x + x2 L
Out[165]=
In[166]:=
Cancel@wD
Out[166]=
1 + x + x2
% ê. x → 1
In[167]:=
Out[167]=
3
In[168]:=
x3 − 1
LimitA , x → 1E
x−1
Out[168]=
3
In[169]:=
1
LimitAH1 + 2 xL x , x → 0E
Out[169]=
2
In[170]:=
LimitA$%%%%%%%%%%%%%%%%
3 x2 + 5%%%%%%%%
x +%%%%%%
2
3
, x → ∞E
1−x 2
H−3L1ê3
Out[170]=
In[171]:=
75
50
25
-1 -0.5 0.5 1
-25
-50
-75
-100
Out[171]=
Graphics
In[172]:=
1
LimitA , x → 0, Direction → 1E
x
Out[172]=
−∞
In[173]:=
1
LimitA , x → 0, Direction → −1E
x
Out[173]=
∞
Some limits are hard to evaluate. The limit can, however, be evalu-
ated numerically using the command NLimit. You need to load the
proper package
In[174]:=
<< NumericalMath`NLimit`
In[175]:=
2x
NLimitA , x → ∞E
x!
Out[175]=
0.
Derivatives
f' or ∂x f represents the derivative of a function f of one argu-
ment
D[f, x] gives the partial derivative ∂ f ê ∂ x
Introduction to Mathematica by Dr Rajai Alassar.nb 24
In[177]:=
h@x_D = ArcTan@xD
Out[177]=
ArcTan@xD
In[178]:=
dh@x_D = h '@xD
Out[178]=
1
1 + x2
Out[179]=
Graphics
In[181]:=
pnt =
Show@Graphics@8PointSize@0.02D, Point@82, h@2D<D<D, DisplayFunction → IdentityD
Out[181]=
Graphics
In[182]:=
Show@peq, g1, pnt, DisplayFunction → $DisplayFunctionD
0.5
-4 -2 2 4
-0.5
-1
Out[182]=
Graphics
Introduction to Mathematica by Dr Rajai Alassar.nb 25
In[183]:=
h ''@xD
Out[183]=
H1 + x2 L2
2x
−
In[184]:=
h ''@−2D
Out[184]=
4
25
In[185]:=
D@x2 , xD
Out[185]=
2x
In[186]:=
∂x x4
Out[186]=
4 x3
Out[187]=
24 x
In[188]:=
∂8x,3< x4
Out[188]=
24 x
In[189]:=
D@x2 y + Sin@x + yD, x, yD
Out[189]=
2 x − Sin@x + yD
Out[190]=
2 x − Sin@x + yD
Out[191]=
−Cos@x + yD
Implicit Differentiation
Introduction to Mathematica by Dr Rajai Alassar.nb 26
DtAHx2 + y2 L == 4 Hx2 − y2 L, xE
In[192]:=
2
In[193]:=
Solve@%, Dt@y, xDD
Out[193]=
99Dt@y, xD → ==
y H2 + x2 + y2 L
2 x − x3 − x y2
In[195]:=
Solve@%, y '@xDD
Out[195]=
99y @xD → ==
y@xD H2 + x2 + y@xD2 L
2 x − x3 − x y@xD2
Integration
In[196]:=
Integrate@x, xD
Out[196]=
x2
2
è!!!!
SinA x E
In[197]:=
In[198]:=
‡ Sin@Log@xDD x
x HCos@Log@xDD − Sin@Log@xDDL
Out[198]=
1
−
2
In[199]:=
‡
π
4
Sin@xD20 x
0
Out[199]=
44623 46189 π
− +
322560 1048576
Introduction to Mathematica by Dr Rajai Alassar.nb 27
In[200]:=
‡ Tan@xD Sec@xD
2 4
x
In[201]:=
Simplify@%D
1
Ä Tan@x D5 + ÄÄÄÄ
The textbook by Anton gives the answer ÄÄÄÄ 1
Ä Tan@x D3
5 3
In[203]:=
‡ è!!!!!!!!
!!!!!
1
x
x2 4 − x2
è!!!!!!!!!!!!2!
Out[203]=
4−x
−
4x
In[204]:=
‡
x2 + x − 2
x
3 x3 − x2 + 3 x − 1
Log@1 + x2 D
Out[204]=
3 ArcTan@xD 7 2
− Log@−1 + 3 xD +
5 15 5
In[205]:=
E
x2 + x − 2
ApartA
3 x3 − x2 + 3 x − 1
Out[205]=
5 H−1 + 3 xL 5 H1 + x2 L
7 3+4x
− +
In[206]:=
‡% x
Log@1 + x2 D
Out[206]=
3 ArcTan@xD 7 2
− Log@−1 + 3 xD +
5 15 5
è!!!!
In[207]:=
‡ è!!! !
x
x
3
1+ x
è!!! 6 x5ê6
Out[207]=
+ 6 ArcTan@x1ê6 D
6 x7ê6
−6 x1ê6 + 2 x − +
5 7
Introduction to Mathematica by Dr Rajai Alassar.nb 28
In[208]:=
‡
1
x
1 − Sin@xD + Cos@xD
EE − LogACosA E − SinA EE
Out[208]=
x x x
LogACosA
2 2 2
In[209]:=
‡
1
−x2
x
0
1 è!!!
Out[209]=
π Erf@1D
2
% êê N
In[210]:=
Out[210]=
0.746824
In[211]:=
‡ Sin@x D x
1
2
0
Out[211]=
$%%%%%% FresnelSA$%%%%%% E
π 2
2 π
% êê N
In[212]:=
Out[212]=
0.310268
‡ x
1
x
x
0
Out[213]=
‡ x
1
x
x
0
Out[214]=
0.783431
Introduction to Mathematica by Dr Rajai Alassar.nb 29
0.9
0.85
0.8
0.75
0.7
Out[215]=
Graphics
In[216]:=
Limit@xx , x → 0D
Out[216]=
1
Out[217]=
2870
n H1 + nL2
Out[218]=
1 2
4
In[219]:=
‚
∞
1
n=1
n4
Out[219]=
π4
90
Introduction to Mathematica by Dr Rajai Alassar.nb 30
Out[220]=
21
In[221]:=
‚ ‚ Hi ∗ jL
4 3
i=1 j=1
Out[221]=
60
In[222]:=
‚
∞
Sin@kD
k=1
k
HLog@1 − D − Log@1 − DL
Out[222]=
1 −
−
2
In[223]:=
ComplexExpand@%D
E
Out[223]=
Sin@1D
ArcTanA
1 − Cos@1D
In[224]:=
N@%, 20D
Out[224]=
1.0707963267948966192
In[225]:=
100
Sin@kD
NA‚ , 20E
k=1
k
Out[225]=
1.0604289384010621281
Introduction to Mathematica by Dr Rajai Alassar.nb 31
In[226]:=
E;
n
Sin@kD
v@n_D := NA‚
k
pt = Table@8j, v@jD<, 8j, 1, 2000<D;
k=1
ListPlot@ptD
1.074
1.073
1.072
1.071
Out[228]=
Graphics
Out[229]=
x2 x3 x4 x5 x6 x7
x− + − + − + + O@xD8
2 3 4 5 6 7
In[230]:=
Clear@fD
v = 83, 2, −1<
In[232]:=
83, 2, −1<
Out[232]=
w = 81, 0, 3<
In[233]:=
81, 0, 3<
Out[233]=
In[234]:=
v+w
84, 2, 2<
Out[234]=
In[235]:=
Dot@v, wD
Out[235]=
0
In[236]:=
v.w
Out[236]=
0
In[237]:=
Cross@v, wD
In[238]:=
v w
c êê MatrixForm
In[240]:=
i
j
0 y
z
Out[240]//MatrixForm=
j
j 5 1 −2 z
z
j
j z
z
2 0
j z
k 3 −2 1 {
In[241]:=
c@@2DD
85, 1, −2<
Out[241]=
In[242]:=
c@@3, 2DD
Out[242]=
−2
In[243]:=
d = 3∗c
In[244]:=
MatrixForm@dD
i
j
0 y
z
Out[244]//MatrixForm=
j
j z
z
j
j z
z
6 0
j z
k {
15 3 −6
9 −6 3
In[245]:=
Det@dD
Out[245]=
−162
i 0 y
In[246]:=
j
j 0 1 −2 z
z
a=j
j z
z
2 0
j
j z
z
k 0 −2 1 {
In[247]:=
a.c
b = 82, 3, −1<
In[248]:=
82, 3, −1<
Out[248]=
Introduction to Mathematica by Dr Rajai Alassar.nb 34
In[249]:=
RowReduce@aD
In[250]:=
MatrixForm@t = Eigenvectors@aDD
i 0 −1 1 z
j y
Out[250]//MatrixForm=
j
j 1 0 0z z
j
j z
z
j z
k0 1 1{
In[251]:=
Eigenvalues@aD
83, 2, −1<
Out[251]=
In[252]:=
LinearSolve@a, bD
91, − ,− =
Out[252]=
1 5
3 3
In[253]:=
MatrixForm@p = Transpose@tDD
i 0 1 0z
j y
Out[253]//MatrixForm=
j
j −1 0 1 z
z
j
j z
z
j z
k 1 0 1{
In[254]:=
MatrixForm@pinv = Inverse@pDD
i
j y
z
Out[254]//MatrixForm=
j
j z
z
j
j z
0 z
0 − 12 1
j
j z
z
j z
2
j 1 z
k0 2 {
1 0
1
2
pinv.a.p êê MatrixForm
In[255]:=
i
j y
z
Out[255]//MatrixForm=
j
j z
z
j
j z
z
3 0 0
j z
k 0 0 −1 {
0 2 0
Graphics
Introduction to Mathematica by Dr Rajai Alassar.nb 35
40
30
20
10
-3 -2 -1 1 2
-10
-20
Out[256]=
Graphics
Some options and directives are available. You can find these
options by writing Options[Plot]
In[257]:=
Options@PlotD
9AspectRatio →
Out[257]=
1
, Axes → Automatic, AxesLabel → None,
GoldenRatio
AxesOrigin → Automatic, AxesStyle → Automatic, Background → Automatic,
ColorOutput → Automatic, Compiled → True, DefaultColor → Automatic,
Clear @g, hD
In[258]:=
In[259]:=
g@x_D = 12 x2 + 12 x − 9;
h@x_D = 24 x + 12;
Introduction to Mathematica by Dr Rajai Alassar.nb 36
60
50
40
30
20
10
-3 -2 -1 1 2
-10
Out[261]=
Graphics
60
50
40
30
20
10
-3 -2 -1 1 2
-10
Out[262]=
Graphics
60
40
20
-3 -2 -1 1 2
-20
-40
-60
Out[263]=
Graphics
In[264]:=
Show@g1, h1, PlotLabel → "Test of Combined Graphs"D
40
20
-3 -2 -1 1 2
-20
-40
-60
Out[264]=
Graphics
y−axis
60
50
40
30
20
10
0 x−axis
-10
-3 -2 -1 0 1 2
Out[265]=
Graphics
0.5
-3 -2 -1 1 2 3
-0.8
-1
Out[266]=
Graphics
Introduction to Mathematica by Dr Rajai Alassar.nb 38
0.5
-3 -2 -1 1 2 3 4
-0.5
-1
-1.5
-2
Out[267]=
Graphics
In[268]:=
E=, 8x, 0, 4 π<, PlotStyle →
x
PlotA9Sin@xD, Sin@2 xD, SinA
0.5
2 4 6 8 10 12
-0.5
-1
Out[268]=
Graphics
"#########################
# "##########################
In[269]:=
-1 1 2 3 4 5
-1
-2
Out[269]=
Graphics
Introduction to Mathematica by Dr Rajai Alassar.nb 39
In[270]:=
Show@circ, AspectRatio → 1D
-1 1 2 3 4 5
-1
-2
Out[270]=
Graphics
You can plot the complete circle by using ImplicitPlot. You, how-
ever, need to load the package `ImplicitPlot`
In[271]:=
<< Graphics`ImplicitPlot`
-1 1 2 3 4 5
-1
-2
Out[272]=
Graphics
Introduction to Mathematica by Dr Rajai Alassar.nb 40
Piecewise Functions
In[273]:=
Clear@f, tD
In[274]:=
f@x_D := Sin@xD ê; 0 ≤ x <
π
2
f@x_D := 1 ê;
π 3π
≤x<
2 2
f@x_D := −Sin@xD ê;
3π
≤x<2π
f@x_D := f@x − 2 πD ê; x ≥ 2 π
2
1.4
1.2
0.8
0.6
0.4
0.2
Parametric Functions
Introduction to Mathematica by Dr Rajai Alassar.nb 41
-1 1 2 3 4 5
-1
-2
Out[279]=
Graphics
In[280]:=
Clear@x, yD
In[281]:=
x@t_D = t − 2 Sin@tD;
In[282]:=
y@t_D = 1 − 2 Cos@tD;
5 10 15 20 25
-1
Out[283]=
Graphics
In[284]:=
Clear@x, yD
In[285]:=
3t
x@t_D = ;
1 + t3
Introduction to Mathematica by Dr Rajai Alassar.nb 42
In[286]:=
3 t2
y@t_D = ;
1 + t3
-2 -1 1 2 3 4
-2
Out[287]=
Graphics
In[288]:=
Clear@x, yD
Polar Graphs
To sketch polar curves, you need to load the package `Graphics`
In[289]:=
<< Graphics`Graphics`
Out[290]=
Graphics
Out[291]=
Graphics
Introduction to Mathematica by Dr Rajai Alassar.nb 43
In[292]:=
In[293]:=
In[294]:=
E ,
5
t
p5 = PolarPlotA Cos@tD
− 2 Cos@4 tD + SinA
Out[294]=
Graphics
In[295]:=
3 1
2.5
2 0.5
1.5
1 -1-0.5 0.5 1
0.5 -0.5
-1.5
--
10.5 0.511.5 -1
1
0.5 15
10
5
-1-0.5 0.5 1 -20-10
-5 10 20
-0.5 -10
-1
3 1
2
1 0.5
-2-1
-1 1 2 3 4 0.5 1 1.5 2
-0.5
-2
-3 -1
Out[296]=
GraphicsArray
Introduction to Mathematica by Dr Rajai Alassar.nb 44
3-D Graphics
The command Plot3D[f,{x,xmin,xmax},{y,ymin,ymax}] generates a
3-d plot.
The command Plot3D[{f,s},{x,xmin,xmax},{y,ymin,ymax}] generates
a 3-d plot with shading specified by s
In[297]:=
Clear@fD
In[298]:=
E;
−J x4 + y2 N x2 4 y2
2 2
f@x_, y_D = CosA +
2 4
0.05
0 2
-0.05
0
-5
-2.5
0
-2
2.5
5
Out[299]=
SurfaceGraphics
Introduction to Mathematica by Dr Rajai Alassar.nb 45
Plot3D@f@x, yD, 8x, −6, 6<, 8y, −3, 3<, PlotPoints → 30D
In[300]:=
0.05
0 2
-0.05
0
-5
-2.5
0
-2
2.5
5
Out[300]=
SurfaceGraphics
0.5
2
0
0
-5
-2.5
0
-2
2.5
5
Out[301]=
SurfaceGraphics
Introduction to Mathematica by Dr Rajai Alassar.nb 46
Plot3D@f@x, yD, 8x, −6, 6<, 8y, −3, 3<, PlotPoints → 30, PlotRange → All,
In[302]:=
ViewPoint −> 8−2.428, −1.870, 1.995<, Boxed → False, BoxRatios → 86, 3, 1<D
1 2.5
0.5
0
0
2 -2.5
0
-2 -5
Out[302]=
SurfaceGraphics
Out[308]=
GraphicsArray
Level Curves
The command ContourPlot[f,{x,xmin,xmax},{y,ymin,ymax}] gener-
ates a contour plot.
In[309]:=
Clear@fD
-1
-2
-2 -1 0 1 2
Out[311]=
ContourGraphics
g2 = ContourPlot@f@x, yD, 8x, −2, 2<, 8y, −2, 2<, PlotPoints → 50,
In[312]:=
-2 -1 1 2
-1
-2
Out[312]=
ContourGraphics
Introduction to Mathematica by Dr Rajai Alassar.nb 49
g3 = Plot3D@f@x, yD, 8x, −2, 2<, 8y, −2, 2<, PlotPoints → 50, Shading → FalseD
In[313]:=
1
0.5 2
0
-0.5 1
-1
-2 0
-1
0 -1
1
2 -2
Out[313]=
SurfaceGraphics
In[314]:=
Show@GraphicsArray@8g2, g3<DD
1
1
0.5 2
0
-0.5 1
-2 -1 1 2 -1
-2
2 0
-1 -1
-1 0
1
2 -2
-2
Out[314]=
GraphicsArray
In[315]:=
Options@ContourPlotD
In[316]:=
<< Graphics`Graphics3D`
Out[317]=
Graphics3D
g1 = ContourPlot3D@x ^ 2 ê 16 + y ^ 2 + z ^ 2 ê 4 − 1,
In[319]:=
8x, −4, 4<, 8y, −1, 1<, 8z, −2, 2<, Contours −> 80, 1<D
Out[319]=
Graphics3D
è!!!! è!!!!
In[320]:=
g2 = ContourPlot3DAx ^ 2 + y ^ 2 − z ^ 2 ê 4 − 1, 9x, − 5 , 5 =,
è!!!! è!!!!
9y, − 5 , 5 =, 8z, −2, 2<, Contours −> 80<, PlotPoints −> 85, 5, 5<,
ViewPoint −> 83.131, 0.993, 1.424<, Axes −> True, Boxed −> False,
AxesStyle −> 8RGBColor@1, 0, 0D, Thickness@.02D<, ColorOutput −> RGBColor,
LightSources −> 888.4, 0, 1<, RGBColor@.5, .7, .3D<<E
-1
1
-1
0
-2
-1 1
0
1
Out[320]=
Graphics3D
In[322]:=
Clear@x, y, z, u, vD
In[323]:=
x@u_, v_D = u;
y@u_, v_D = Cos@vD ê u;
z@u_, v_D = Sin@vD ê u;
ParametricPlot3D@8x@u, vD, y@u, vD, z@u, vD<, 8u, 0.7, 5<, 8v, 0, 2 π<,
Shading → False, ViewPoint −> 82.617, 1.877, 1.311<, PlotPoints → 30D
0
2
-1
1
-1
0
1
Out[326]=
Graphics3D
Introduction to Mathematica by Dr Rajai Alassar.nb 53
In[327]:=
x1@u_, v_D = u v;
y1@u_, v_D = u;
z1@u_, v_D = v2 ;
ParametricPlot3D@8x1@u, vD, y1@u, vD, z1@u, vD<, 8u, −2, 2<,
8v, −2, 2<, ViewPoint −> 82.086, 2.606, 0.935<, PlotPoints −> 30D
-2 -1
0 1 2
1
-4
0 -2
0
2
4
Out[330]=
Graphics3D
1
0.5 1
0
-0.5 0.5
-1
-1 0
-0.5
0 -0.5
0.5
1 -1
Out[332]=
SurfaceGraphics
Introduction to Mathematica by Dr Rajai Alassar.nb 54
In[333]:=
SetCoordinates@Cartesian@x, y, zDD
Out[333]=
Cartesian@x, y, zD
In[334]:=
gw@x_, y_D = Grad@z − Cos@4 x ^ 2 + 9 y ^ 2D, CartesianD
è!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!
norm@x_, y_D = gw@x, yD ë
In[335]:=
gw@x, yD.gw@x, yD
General::spell1 :
Possible spelling error: new symbol name "norm" is similar to existing symbol "Norm". More…
8 x Sin@4 x2 + 9 y2 D
Out[335]=
9 è!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!! ,
1 + 64 x Sin@4 x2 + 9 y2 D2 + 324 y2 Sin@4 x2 + 9 y2 D2
18 y Sin@4 x2 + 9 y2 D
2
è!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!!!!!!!!!!!!!!!! ,
1 + 64 x2 Sin@4 x2 + 9 y2 D2 + 324 y2 Sin@4 x2 + 9 y2 D2
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
è!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!! =
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1 + 64 x2 Sin@4 x2 + 9 y2 D2 + 324 y2 Sin@4 x2 + 9 y2 D2
1
In[336]:=
<< Graphics`PlotField3D`
In[337]:=
p2 = PlotVectorField3D@norm@x, yD,
8x, −1, 1<, 8y, −1, 1<, 8z, −1, 1<, VectorHeads −> TrueD
Out[337]=
Graphics3D
Introduction to Mathematica by Dr Rajai Alassar.nb 55
In[338]:=
Show@p1, p2D
1
0.5
0 1
-0.5
-1
0
-1
-0.5
0
0.5 -1
1
Out[338]=
Graphics3D
Out[339]=
x Sin@xD
7.5
2.5
2 4 6 8 10 12
-2.5
-5
-7.5
-10
Out[340]=
Graphics
Introduction to Mathematica by Dr Rajai Alassar.nb 56
Out[342]=
6
7.5
2.5
-1 1 2 3
-2.5
-5
-7.5
-10
7.5
2.5
2 4 6 8 10 12
-2.5
-5
-7.5
-10
2 4 6 8 10 12
-5
-10
-15
References:
1. Wolfram Research, The Mathematica Book, Fourth Edition, Wolfram Media, 1999.
2. Wolfram Research, Standard Add-On Packages, Fourth Edition, Wolfram Media, 1999.
3. Martha Abell and James Braselton, Mathematica by Example, Second Edition, 1997.