Transcript Slide 1

MATHEMATICA – AN INTRODUCTION

R.C. Verma Physics Department Punjabi University Patiala – 147 002

Part II- Algebra

Solving One, Two or more Equations Numerical Analysis: Root Finding Creating new Expressions, and Transformation Rules Creating new (user-defined) Functions Sums and Products

18. Algebraic Expressions

Mathematica understands algebraic expressions, and can do symbolic computations. In[48]:= 3x-6x+7-2+5x Out[48]= 5 + 2 x In[49] := (x+y)^3 (x+y) Out[49]= (x + y) 4

19.1. Expanding the Expressions

Expand[ expression] command is used to multiply out the expression. In[50]:= Expand[ (x+y)^4] Out[50]= 4 3 2 2 3 4 x + 4 x y + 6 x y + 4 x y + y Output of a certain command can be limited to approximately one line by suffix

// Short

In[51]:= Expand[ (x+y)^100]//Short Out[51]= 100 99 100 x + 100 x y + <<98>> + y ExpandAll[ expression] applies Expand command everywhere.

19.2. Simplifying the Expressions

Factor[ expression ] restores it to factored form. In[52]:= Factor[ x^4 + 4x^3 y + 6x^2 y^2 + 4x y^3 + y^4] Out[52]= (x + y) 4 Using Simplify command, you may express a formula as simply as possible. In[53]:= Simplify[ (x^2 - y^2)/(x+y)] Out[53]= x - y Try the following command: In[54]:= Simplify[ (x^3+y^3)(x^3-y^3) ] Out[54]= 3 3 3 3 (x - y ) (x + y ) So, sometimes, Expand command does the job better.

In[55]:= Expand[ (x^3 + y^3)(x^3 - y^3) ] Out[55]= 6 6 x - y

20. Equations vs. Assignments

Mathematica distinguishes between an equation and an assignment. For example, x == 5 is an equation, whereas x = 5 is an assignment. So in Mathematica, an equation is written using a double equal

==

sign.

X^3 - 3 x + 2 == 0

21. Solving Equation

The linear as well as polynomial equations involving one variable can easily be solved.

Solve [eqn, var] attempts to solve an equation (or set of equations) for the variables var. In[56]:= Solve[ x^3 - 3 x + 2 == 0, x] Out[56]= {{x -> -2}, {x -> 1}, {x -> 1}}

21.1 Solving Two or more Equations

Solve[ { lhs1== rhs1, lhs2 == rhs2, …. }, {x, y, z, ….}] In[57]:= Solve[ {x + y ==3, x - y == 1}, {x, y}] Out[57] = {{x -> 2, y -> 1}}

22. Numerical Solving Equations

NSolve[ lhs== rhs, x] solves a polynomial equation numerically. In[58]:= NSolve[x^6 + x^2 -1 ==0, x] Out[58]= { {x -> -0.826031}, {x -> -0.659334 - 0.880844 I}, {x -> -0.659334 + 0.880844 I}, {x -> 0.659334 - 0.880844 I}, {x -> 0.659334 + 0.880844 I}, {x -> 0.826031}} NSolve[ { lhs1== rhs1, lhs2 == rhs2, …. }, {x, y, z, ….}] solves a system of polynomial equations numerically. In[59]:= NSolve[{x^3+y^3 ==1, x+y==2}, {x, y}] Out[59]= {{x -> 1. - 0.408248 I, y -> 1. + 0.408248 I}, {x -> 1. + 0.408248 I, y -> 1. - 0.408248 I}}

23. Numerical Analysis: Root Finding

Root of an equation can be found using FindRoot[ ] giving one guess root (or two guess roots). To find an approximate solution of the equation e y choosing starting value of y = 2 . = 2 y + 3 , In[60]:= FindRoot[ Exp[y]==2 y + 3, {y, 2}] Out[60]={y -> 1.92394} FindRoot command can be used to solve two or more equations involving two or more unknowns. FindRoot[ { lhs1== rhs1, lhs2 == rhs2, …. }, {x, x0}, {y, y0}, ….}] searches for numerical solutions for a system of simultaneous polynomial equations

24. Creating new Expressions

You may even assign a name to an equation, and use Mathematica commands on it.

In[61]:= quadratic = a*x^2 + b*x +c ==0 ln[62]:= Solve[ quadratic, x] Out[62]= 2 2 -b - Sqrt[ b - 4 a c] -b + Sqrt[ b - 4 a c] {{x -> -------------------------}, {x -> --------------------------}} 2 a 2 a An expression may be defined using

:=

in place of simple

=

sign.

In[63] := a:= x^3 + 2 x – 1 Mathematica treats the two ways of defining expression differently

25. Transformation Rules

To evaluate an expression at a point, use the replacement operator expression / . Rule In[63] := a:= x^3 + 2 x – 1; In[64] := a/.x->2 Out[64]= 11 ln[65]:= (x^ 3+ 5) /. x -> 2 Out[65]=13 The replacement operator can also be used to substitute for two or more variables in an expression. In[66] := ( c/(a^4 - 6*b +3) )/. {a ->m, b ->n, c->p} Out[66]= p ----------- 4 3 + m - 6 n

26. Creating new (user-defined) Functions

Any functional form may be developed as user-defined function as assignment.

ln[67]:= f[x_] = x^4 + 5 x^2 -3 Out[67]= 2 4 -3 + 5 x + x Here,

f

is the name of a function of one variable,

x

. The underscore just after the symbol

x

The

argument

means that

x

must be enclosed in square brackets is a

[ ] dummy variable

. just after the function name. On the right hand side definition of the function is given. The function may be evaluated with either a numeric or a symbolic argument: In[68]:= f[-2] Out[68]= 33

You can also define a function using the "colon-equal" sign

:=

instead of equal sign. Mathematica never forgets such definitions unless instructed to do so. Structure of the statement is functionName[argument_]: = expression.

Example, f[x_]:= x^4 +5 x^2 -3 There is a difference between the two methods of defining new function. Without colon, the r.h.s. the definition is evaluated as soon as the definition is made. The colon-equal sign

:

tells Mathematica not to evaluate the r.h.s. until you use it.

It also suppresses the output.

26.1 Creating new (user-defined) Function using Table[ ]

Table[ ] may also be defined as function: In[69]:= f[n_]:=Table[ Prime[i], {i,n}] f[20] Out[69]= {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71}

26.2. Functions of two or more variables

In[70]:= g[x_, y_]:= Sqrt[x*x + y*y] length = g[2.3, 5.6] Out[70]= 6.05392

27. Sums and Products

Sum[ term-definition, {I, imin, imax}] finds the

sum of the term

for a given

range

. ln[71]:= Sum[ 1/i^2, {i, 1, 10}] Out[71]= 1968329 ------------ 1270080 In[72]:= Sum[Sqrt[3+Sqrt[3+Sqrt[3+Sqrt[3+n]]]],{n,0,5}]//N Out[72]= 13.8169

Similarly, In[73]:= Product[ i, {i, 2, 20, 2}] Out[73]= 3715891200 In this, the final 2 indicates that the counter i should be evaluated in increments of 2.

In[74]:= Sum[ 3^(-i), {i, 0, Infinity}] Out[74]= -i Sum[3 , {i, 0, Infinity}] To get the numerical result, use the N[ ] command, or suffix //N In[75]:= Sum[ 3^(-i), {i, 0, Infinity}]//N Out[75]= 1.5

End of Part II