Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object...

24
Mathematica Demo #1 Arithmetic Shift-Enter --> Calculate (inputs any calculation you would like performed) "+" --> Addition "-" --> Subraction "*" or " " --> Multiplication "/" --> Division "^" --> Exponent Examples: 2.3 + 5.63 7.93 2.4 8.9^2 0.0302992 234 24 H3 + 4L ^2 - 2 H3 + 1L 41 Order of execution is always by standard convention. Exponents first, followed by multiplication and division, then lastly addition and subtraction. If two operations are of the same priority, parenthetical operations are performed first if applicable, or the operation that comes first sequentially. Scientific Notation One enters a number in scientific notation in the following way: 2.3 * 10^70 2.3 · 10 70 demo1.nb 1

Transcript of Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object...

Page 1: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

Mathematica Demo #1

‡ Arithmetic

Shift-Enter --> Calculate (inputs any calculation you would like performed)

"+" --> Addition

"-" --> Subraction

"*" or " " --> Multiplication

"/" --> Division

"^" --> Exponent

ü Examples:

2.3 + 5.63

7.93

2.4��������������������8.9^2

0.0302992

2 3 4

24

H3 + 4L^2 − 2 H3 + 1L41

Order of execution is always by standard convention. Exponents first, followed by multiplication and division, then lastly

addition and subtraction. If two operations are of the same priority, parenthetical operations are performed first if applicable,

or the operation that comes first sequentially.

‡ Scientific Notation

One enters a number in scientific notation in the following way:

2.3∗10^70

2.3´1070

demo1.nb 1

Page 2: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

‡ Exact and Approximate Results

Mathematica will generally default to an exact answer; however, this is not always the most desirable format. The "//N" or

"N[expr]" switch allows the program to approximate the solution to a fixed number of decimal places. It is specifically

defined in the following way:

?N

N@exprD gives the numerical value of expr. N@expr, nD attempts to give a result

with n-digit precision.

Note that ?Function gives the help description of any function or command.

ü Examples:

2^100

1267650600228229401496703205376

N@2^100D1.26765´1030

2^100 êê N

1.26765´1030

N@2^100, 15D1.26765060022823´1030

Similarly, this switch will transform rational fractions to decimal form, or a square root from its exact symbolic form to a

decimal form.

1�����3

+2�����7

13ÄÄÄÄÄÄÄÄÄ21

NA 1�����3

+2�����7

E0.6190476190476191

Lastly, note the differences in output depending on how the input is entered. In particular, note the effect of adding a decimal

point to change the argument from an integer to a real number.

demo1.nb 2

Page 3: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

Sqrt@2Dè++++2

N@[email protected]

[email protected]

‡ Common Mathematical Functions

Sqrt[x] --> square root

Exp[x] --> exponential

Log[x] --> natural logarithm

Log[b,x] --> logarithm to base b

Sin[x], Cos[x], Tan[x], Csc[x], Sec[x], Cot[x] --> trigonometric functions (x in radians)

ArcSin[x], ArcCos[x], ArcTan[x], ArcCsc[x], ArcSec[x], ArcCot[x] --> inverse

trigonometric functions (results in radians)

Sinh[x], Cosh[x], Tanh[x], Csch[x], Sech[x], Coth[x] --> hyperbolic functions

ArcSinh[x], ArcCosh[x], ArcTanh[x], ArcCsch[x], ArcSech[x], ArcCoth[x] --> inverse

hyperbolic functions

n! --> factorial

Abs[x] --> absolute value

Round[x] --> closest integer to x

FactorInteger[x] --> prime factors of n

ü Examples:

Sqrt@65Dè+++++++65

[email protected]

[email protected]

Log@10, 1353.D3.1313

demo1.nb 3

Page 4: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

SinA Pi���������2

E1

6!

720

FactorInteger@63D883, 2<, 87, 1<<Note that all Mathematica functions begin with capital letters, and that their arguments are enclosed in square brackets.

‡ Mathematical Constants

Pi --> pi (3.14159....)

E --> e (2.71828....)

Degree --> Pi / 180 (converts degrees to radians)

I --> Sqrt[-1] (imaginary number)

Infinity --> infinity

EulerGamma --> Euler's constant (0.577216....)

ü Examples:

Pi

p

[email protected]

N@Pi, 40D3.141592653589793238462643383279502884197

‡ Complex Numbers

As mentioned in the previous section, complex numbers may be denoted by adding the constant I.

?I

I represents the imaginary unit [email protected]@−4D2 I

demo1.nb 4

Page 5: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

N@Exp@2 + 9 IDD-6.73239 + 3.04517 I

ü Useful Commands for Complex Numbers:

Re[z] --> real part of z

Im[z] --> imaginary part of z

Conjugate[z] --> complex conjugate of z

Abs[z] --> absolute value (magnitude) of z

Arg[z] --> argument (phase) of z

ü Examples:

N@Re@Exp@2 + 9 IDDD-6.73239

N@Im@Exp@2 + 9 IDDD3.04517

N@Conjugate@Exp@2 + 9 IDDD-6.73239 - 3.04517 I

N@Abs@Exp@2 + 9 IDDD7.38906

N@Arg@Exp@2 + 9 IDDD2.71681

‡ Referencing Previous Results

Oftentimes, it is convenient to build upon previous results in performing calculations. The following tricks let one do so.

% --> the last result generated

%% --> the next-to-last result generated

%%...% (k times) --> the kth previous result

%n --> the result on output line Out[n] (careful with this one!!!)

demo1.nb 5

Page 6: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

?%

%n or Out@nD is a global object that is assigned to be the value produced on the nth

output line. % gives the last result generated. %% gives the result before last.

%% ... % Hk timesL gives the kth previous result.

ü Examples:

77^2

5929

% + 1

5930

3 % + %^2 + %%

35188619

In using the notebook format in Mathematica, you have to be extremely careful using these symbols to reference previous

results. This is because "%" references the last output generated, regardless of whether that result appears immediately above

your present position in the notebook. Thus, the ability to scroll back in the notebook makes the use of this command a risky

endeavor.

‡ Defining Variables

Since the previous section points out the dangers of using "%" to reference previous calculations, a better alternative is to

define a variable.

x = value --> assign a value to the variable x

x = y = value --> assign a value to both x and y

x = . or Clear[x] --> remove any value assigned to x

?=

lhs = rhs evaluates rhs and assigns the result to be the value of lhs. >From then

on, lhs is replaced by rhs whenever it appears. 8l1, l2, ... < = 8r1, r2, ... <evaluates the ri, and assigns the results to be the values of the corresponding li.

?=.

lhs =. removes any rules defined for lhs.

ü Examples:

x = 5

5

demo1.nb 6

Page 7: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

x^2

25

x = 7 + 4

11

x =.

x

x

The value assigned to a variable will remain for your entire Mathematica session unless it is explicitly cleared. Thus, to avoid

confusion, it is wise to remove variables once you are finished using them.

The only restriction in naming variables is that they cannot start with numbers. They can have numbers in their definition, but

the first character must be a letter. It is also recommended that the first character not be a capital letter, to avoid confusion

with existing Mathematica functions.

ü Examples:

x y --> implies x times y

xy --> implies the variable named xy

5x --> implies 5 times x (since variables cannot begin with a number)

x^2y --> implies (x^2)y, not x^(2y)

‡ Making Lists of Objects

Sometimes it is convenient to gather together several objects and treat them as a single entity. This can be done with lists,

which are formed using curly brackets "{}".

?88e1, e2, ... < is a list of elements.

ü Examples:

x = 83, 5, 1<83, 5, 1<x^2 + 1810, 26, 2<

demo1.nb 7

Page 8: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

There are some commands for manipulating portions of lists:

Part[list, i] or list[[i]] --> the ith element of a list

Part[list,-i] or list[[-i]] --> the ith element from the end of a list

Part[list, {n1, n2, ...}] or list[[{n1, n2,...}]] --> the list of elements at positions n1, n2,...

Part[list,i] = value or list[[i]] = value --> reset the ith element of a list

First[list] --> the first element in a list

Last[list] --> the last element in a list

Take[list, n] --> the first n elements in a list

Take[list, -n] --> the last n elements in a list

Take[list, {m, n}] --> elements m through n (inclusive) in a list

Rest[list] --> a list with its first element dropped

Drop[list, n] --> a list with its first n elements dropped

Drop[list, -n] --> a list with its last n elements dropped

Drop[list, {m, n}] --> a list with elements m through n (inclusive) dropped

‡ Sequences of Operations

You will typically perform a series of calculations in Mathematica by typing each on a separate line. However, this is not

always necessary, as shown below.

?;

expr1; expr2; ... evaluates the expri in turn, giving the last one as the result.

ü Examples:

x = 4; y = 6; z = y + 6

12

The use of a semi-colon has the effect of separating a sequence of calculations and only outputing the result for the final one

in this case. The semi-colon can also be used to avoid an output for a single calculation.

ü Examples:

x = 67 − 5;

x

62

demo1.nb 8

Page 9: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

x =.

‡ Symbolic Algebraic Computations

Mathematica has the ability to take algebraic expressions or equations and manipulate them symbolically. This is

demonstrated below:

ü Examples:

3 x − x + 2

2 + 2 x

There are also a variety of commands for manipulating these expressions:

Expand[expr] --> multiply out products and powers

ExpandAll[expr] --> apply Expand everywhere

Factor[expr] --> write an expression as a product of minimal factors

Together[expr] --> put all terms over a common denominator

Apart[expr] --> separate into terms with simple denominators

Cancel[expr] --> cancel common factors between numerators and denominators

Simplify[expr] --> try to find the simplest form of an expression by applying various standard

algebraic transformations

Collect[expr, x] --> group together powers of x

FactorTerms[expr, x] --> pull out factors that do not depend on x

ü Examples:

y = Hx^2 + 2 x + 1L Hx − 2L^2H-2 + xL2 H1 + 2 x + x2Lz = Expand@yD4 + 4 x - 3 x2 - 2 x3 + x4

Factor@zDH-2 + xL2 H1 + xL2

y =.; z =.;

y =Hx − 1L^2 H2 + xL��������������������������������������������������H1 + xL Hx − 3L^2H-1 + xL2 H2 + xL

ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄH-3 + xL2 H1 + xL

demo1.nb 9

Page 10: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

z = Expand@yD2

ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄH-3 + xL2 H1 + xL -3 x

ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄH-3 + xL2 H1 + xL +x3

ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄH-3 + xL2 H1 + xLz = ExpandAll@yD

2ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ9 + 3 x - 5 x2 + x3

-3 x

ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ9 + 3 x - 5 x2 + x3

+x3

ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ9 + 3 x - 5 x2 + x3

a = Together@zD2 - 3 x + x3

ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ9 + 3 x - 5 x2 + x3

Apart@aD1 +

5ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄH-3 + xL2 +

19ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ4 H-3 + xL +

1ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ4 H1 + xL

x =.; y =.; z =.; a =.;

Here are a couple more expressions:

ComplexExpand[expr] --> perform expansions assuming that all variables are real

PowerExpand[expr] --> transform (xy)^p into x^p y^p, etc.

ü Examples:

ComplexExpand@Sin@x + I yDDCosh@yD Sin@xD + I Cos@xD Sinh@yDPowerExpand@Sqrt@x yDDè++++x è++++

y

There are still other commands for manipulating algebraic expressions:

Coefficient[expr, form] --> coefficient of form in expr

Exponent[expr, form] --> maximum power of form in expr

Numerator[expr] --> numerator of expr

Denominator[expr] --> denominator of expr

ü Examples:

z =.; y =.; x =.;

z = Expand@H1 + 3 x + 4 y^2L^2D1 + 6 x + 9 x2 + 8 y2 + 24 x y2 + 16 y4

demo1.nb 10

Page 11: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

Coefficient@z, xD6 + 24 y2

Exponent@z, yD4

z =1 + x

��������������������������2 H2 − yL

1 + xÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ2 H2 - yLDenominator@zD2 H2 - yL

‡ Values for Symbols

Often when a symbolic expression is created, one wants to know what the result would be if a particular value were

substituted in for the variable. This can be done as follows:

?/.

expr ê. rules applies a rule or list of rules in an attempt to transform each subpart

of an expression expr.

?->

lhs -> rhs represents a rule that transforms lhs to rhs.

ü Examples:

1 + 2 x ê. x → 3

7

1 + x + x^2 ê. x → 2 − y

3 + H2 - yL2 - y

Hx + yL Hx − yL^2 ê. 8x → 3, y → 1 − a<H4 - aL H2 + aL2

t = 1 + x^2

1 + x2

t ê. x → 2

5

demo1.nb 11

Page 12: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

t ê. x → 5 a

1 + 25 a2

‡ Symbolic Differentiation

?D

D@f, xD gives the partial derivative of f with respect to x. D@f, 8x, n<D gives the

nth partial derivative of f with respect to x. D@f, x1, x2, ... D gives a mixed derivative.

ü Examples:

D@ArcTan@xD, xD1

ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ1 + x2

D@x^n, 8x, 3<DH-2 + nL H-1 + nL n x-3+n

‡ Integration

ü Symbolic

?Integrate

Integrate@f, xD gives the indefinite integral of f with respect to x. Integrate@f, 8x, xmin,

xmax<D gives the definite integral of f with respect to x from xmin to xmax. Integrate@f, 8x,xmin, xmax<, 8y, ymin, ymax<D gives a multiple definite integral of f with respect to x and y.

ü Examples:

Integrate@x^n, xDx1+n

ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ1 + n

IntegrateA 2 Exp@−x^2D��������������������������������������Sqrt@PiD , xE

Erf@xDIntegrate@x^2 + y^2, 8x, 0, 1<, 8y, 0, x<D1ÄÄÄÄÄ3

demo1.nb 12

Page 13: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

ü Numerical

?NIntegrate

NIntegrate@f, 8x, xmin, xmax<D gives a numerical approximation to the integral of

f with respect to x from xmin to xmax.

ü Examples:

Integrate@Sin@Sin@xDD, 8x, 1, 2<Dà1

2

Sin@Sin@xDD âx

NIntegrate@Sin@Sin@xDD, 8x, 1, 2<D0.816449955123312

‡ Sums and Products

ü Symbolic

?Sum

Sum@f, 8i, imax<D evaluates the sum of the expressions f as evaluated for each i from 1 to

imax. Sum@f, 8i, imin, imax<D starts with i = imin. Sum@f, 8i, imin, imax, di<D uses steps

di. Sum@f, 8i, imin, imax<, 8j, jmin, jmax<, ... D evaluates a sum over multiple indices.

?Product

Product@f, 8i, imax<D evaluates the product of the expressions f as evaluated

for each i from 1 to imax. Product@f, 8i, imin, imax<D starts with i = imin.

Product@f, 8i, imin, imax, di<D uses steps di. Product@f, 8i, imin, imax<, 8j,jmin, jmax<, ... D evaluates a product over multiple indices.

ü Examples:

Sum@x^i êi, 8i, 1, 7<Dx +

x2ÄÄÄÄÄÄÄÄ2

+x3ÄÄÄÄÄÄÄÄ3

+x4ÄÄÄÄÄÄÄÄ4

+x5ÄÄÄÄÄÄÄÄ5

+x6ÄÄÄÄÄÄÄÄ6

+x7ÄÄÄÄÄÄÄÄ7

Sum@x^i êi, 8i, 1, 7, 2<Dx +

x3ÄÄÄÄÄÄÄÄ3

+x5ÄÄÄÄÄÄÄÄ5

+x7ÄÄÄÄÄÄÄÄ7

demo1.nb 13

Page 14: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

Product@x + i, 8i, 1, 4<DH1 + xL H2 + xL H3 + xL H4 + xLü Numerical

?NSum

NSum@f, 8i, imin, imax<D gives a numerical approximation to the sum of the expressions

f as evaluated for each i from imin to imax. NSum@f, 8i, imin, imax, di<D uses a

step di in the sum.

?NProduct

NProduct@f, 8i, imin, imax<D gives a numerical approximation to the product of the

expressions f as evaluated for each i from imin to imax. NProduct@f, 8i, imin,

imax, di<D uses a step di in the product.

ü Examples:

Sum@1 êi^4, 8i, 1, Infinity<Dp4

ÄÄÄÄÄÄÄÄÄ90

NSum@1êi^4, 8i, 1, Infinity<D1.08232

‡ Equations

Equations are entered in Mathematica using the "==" notation.

?==

lhs == rhs returns True if lhs and rhs are identical.

ü Examples:

2 + 2 == 4

True

2 + 2 == 5

False

x2 + 2 x − 7 == 0

-7 + 2 x + x2 == 0

demo1.nb 14

Page 15: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

‡ Other Logical Operators

There are other logical operators that Mathematica uses:

!= --> not equal to

> --> greater than

>= --> greater than or equal to

< --> less than

<= --> less than or equal to

ü Examples:

3 < 5 < 6

True

‡ Solving Equations

ü Symbolic

?Solve

Solve@eqns, varsD attempts to solve an equation or set of equations for the

variables vars. Solve@eqns, vars, elimsD attempts to solve the equations for vars,

eliminating the variables elims.

ü Examples:

Solve@x^2 + 2 x − 7 == 0, xD99x ® -1 - 2 è++++2 =, 9x ® -1 + 2 è++++

2 ==Solve@x^2 + 1 == 0, xD88x ® -I<, 8x ® I<<

ü Numerical

?NSolve

NSolve@lhs==rhs, varD gives a list of numerical approximations to the roots of a

polynomial equation.

demo1.nb 15

Page 16: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

ü Examples:

Solve@x^5 + x + 1 == 0, xD98x ® -H-1L1ê3<, 8x ® H-1L2ê3<, 9x ®

1ÄÄÄÄÄ3

-1ÄÄÄÄÄ3

ikjjj 2ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ25 - 3 è+++++++

69

y{zzz1ê3-1ÄÄÄÄÄ3

J 1ÄÄÄÄÄ2

I25 - 3 è+++++++69 MN1ê3=,

9x ®1ÄÄÄÄÄ3

+1ÄÄÄÄÄ6

I1 + Iè++++3 M J 1

ÄÄÄÄÄ2

I25 - 3è+++++++69 MN1ê3

+1 - I è++++

3ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ3 22ê3 I25 - 3 è+++++++

69 M1ê3 =,9x ®

1ÄÄÄÄÄ3

+1ÄÄÄÄÄ6

I1 - I è++++3 M J 1

ÄÄÄÄÄ2

I25 - 3 è+++++++69 MN1ê3

+1 + I è++++

3ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ3 22ê3 I25 - 3 è+++++++

69 M1ê3 ==NSolve@x^5 + x + 1 == 0, xD88x ® -0.754878<, 8x ® -0.5 - 0.866025 I<, 8x ® -0.5 + 0.866025 I<,8x ® 0.877439 - 0.744862 I<, 8x ® 0.877439 + 0.744862 I<<

‡ Solving Differential Equations

ü Symbolic

?DSolve

DSolve@eqn, y, xD solves a differential equation for the function y, with independent

variable x. DSolve@8eqn1, eqn2, ... <, 8y1, y2, ... <, xD solves a list of

differential equations. DSolve@eqn, y, 8x1, x2, ... <D solves a partial differential equation.

ü Examples:

DSolve@y′@xD == a y@xD + 1, y@xD, xD99y@xD ® -

1ÄÄÄÄÄa

+ Ea x C@1D==DSolve@8y′@xD == a y@xD + 1, y@0D == 0<, y@xD, xD99y@xD ®

-1 + Ea xÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ

a==

ü Numerical

?NDSolve

NDSolve@eqns, y, 8x, xmin, xmax<D finds a numerical solution to the ordinary

differential equations eqns for the function y with the independent variable x in

the range xmin to xmax. NDSolve@eqns, y, 8x, xmin, xmax<, 8t, tmin, tmax<D finds

a numerical solution to the partial differential equations eqns. NDSolve@eqns,8y1, y2, ... <, 8x, xmin, xmax<D finds numerical solutions for the functions yi.

demo1.nb 16

Page 17: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

?Plot

Plot@f, 8x, xmin, xmax<D generates a plot of f as a function of x from xmin to xmax.

Plot@8f1, f2, ... <, 8x, xmin, xmax<D plots several functions fi.

?Evaluate

Evaluate@exprD causes expr to be evaluated even if it appears as the argument of a

function whose attributes specify that it should be held unevaluated.

ü Examples:

z = NDSolve@8y′@xD == y@xD, y@0D == 1<, y, 8x, 0, 2<D88y ® InterpolatingFunction@880., 2.<<, <>D<<[email protected] ê. z84.48171<Plot@Evaluate@y@xD ê. zD, 8x, 0, 2<D

0.5 1 1.5 2

1

2

3

4

5

6

7

Ö Graphics Ö

‡ Power Series

?Series

Series@f, 8x, x0, n<D generates a power series expansion for f about the point x =

x0 to order Hx - x0L^n. Series@f, 8x, x0, nx<, 8y, y0, ny<D successively finds

series expansions with respect to y, then x.

demo1.nb 17

Page 18: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

ü Examples:

Series@Cos@xD, 8x, 0, 10<D1 -

x2ÄÄÄÄÄÄÄÄ2

+x4ÄÄÄÄÄÄÄÄÄ24

-x6

ÄÄÄÄÄÄÄÄÄÄÄÄ720

+x8

ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ40320

-x10

ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ3628800

+ O@xD11

‡ Limits

?Limit

Limit@expr, x->x0D finds the limiting value of expr when x approaches x0.

ü Examples:

t =Sin@xD����������������������

x

Sin@xDÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ

x

Limit@t, x → 0D1

‡ Functions

Functions are defined using the ":=" symbol. To limit confusion, it is also recommended that you use a lower case character

for the first letter of a function.

?:=

lhs := rhs assigns rhs to be the delayed value of lhs. rhs is maintained in an

unevaluated form. When lhs appears, it is replaced by rhs, evaluated afresh each time.

The argument of the function is typically defined as any character followed by the underline symbol. This basically makes the

argument of the function a wild card, allowing one to substitute any number or variable into the function.

? _

_ or Blank@ D is a pattern object that can stand for any Mathematica expression.

_h or Blank@hD can stand for any expression with head h.

ü Examples:

f@x_D := x2

demo1.nb 18

Page 19: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

f@a + 1DH1 + aL2

f@4D16

Clear@fDf@x_, xmax_D :=

Hx − xmaxL2

����������������������������������xmax

2 + f@x, 3.5D2 + 0.285714 H-3.5 + xL2

?f

Global`f

f@x_, xmax_D := Hx - xmaxL^2 êxmaxClear@fD

‡ Basic Plotting

?Plot

Plot@f, 8x, xmin, xmax<D generates a plot of f as a function of x from xmin to xmax.

Plot@8f1, f2, ... <, 8x, xmin, xmax<D plots several functions fi.

ü Examples:

Plot@Sin@xD, 8x, 0, 2 Pi<D

1 2 3 4 5 6

-1

-0.5

0.5

1

Ö Graphics Ö

demo1.nb 19

Page 20: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

Plot@Tan@xD, 8x, −3, 3<D

-3 -2 -1 1 2 3

-40

-20

20

40

Ö Graphics Ö

Plot@8Sin@xD, Sin@2 xD, Sin@3 xD<, 8x, 0, 2 Pi<D

1 2 3 4 5 6

-1

-0.5

0.5

1

Ö Graphics Ö

plt1 = Plot@Sin@xD, 8x, 0, 2 Pi<D ; plt2 = Plot@Sin@2 xD, 8x, 0, 2 Pi<D ;

plt3 = Plot@Sin@3 xD, 8x, 0, 2 Pi<D ;

demo1.nb 20

Page 21: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

1 2 3 4 5 6

-1

-0.5

0.5

1

1 2 3 4 5 6

-1

-0.5

0.5

1

1 2 3 4 5 6

-1

-0.5

0.5

1

?Show

Show@graphics, optionsD displays two- and three-dimensional graphics, using the

options specified. Show@g1, g2, ... D shows several plots combined.

demo1.nb 21

Page 22: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

Show@plt1, plt2, plt3D

1 2 3 4 5 6

-1

-0.5

0.5

1

Ö Graphics Ö

There are sometimes better ways to make plots, depending on the circumstances. Listed below are the various ways plots can

be constructed:

Plot[f, {x, xmin, xmax}] --> first choose specific numerical values for x, then evaluate f for

each value of x.

Plot[Evaluate[f], {x, xmin, xmax}] --> first evaluate f, then choose specific numerical

values of x.

Plot[Evaluate[Table[f, ... ]], {x, xmin, xmax}] --> generate a list of functions, and then plot

them.

Plot[Evaluate[y[x] /. solution], {x, xmin, xmax}] --> plot a numerical solution to a

differential equation obtained from

NDSolve.

<< Graphics`PlotField`

?PlotVectorField

PlotVectorField@f, 8x, x0, x1, HxuL<, 8y, y0, y1, HyuL<, HoptionsLD produces a

vector field plot of the two-dimensional vector function f.

f@x_, y_D :=y Cos@xD���������������������������1 + 2 y2

demo1.nb 22

Page 23: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

plt1 = PlotVectorField@81, f@x, yD<, 8x, −3, 3<, 8y, −1, 2<, Frame −> TrueD

-3 -2 -1 0 1

-1

-0.5

0

0.5

1

1.5

2

Ö Graphics Ö

‡ Just for fun!

?ParametricPlot3D

ParametricPlot3D@8fx, fy, fz<, 8t, tmin, tmax<D produces a three-dimensional space curve

parametrized by a variable t which runs from tmin to tmax. ParametricPlot3D@8fx, fy, fz<, 8t,tmin, tmax<, 8u, umin, umax<D produces a three-dimensional surface parametrized by t and u.

ParametricPlot3D@8fx, fy, fz, s<, ... D shades the plot according to the color specification

s. ParametricPlot3D@88fx, fy, fz<, 8gx, gy, gz<, ... <, ... D plots several objects together.

demo1.nb 23

Page 24: Mathematica Demo #1 - UCSB MRSECghf/eng5a/demo1.pdfdemo1.nb 5 % n or Out @n D is a global object that is assigned to be the value produced on the nth output line . % gives the last

ParametricPlot3D@8t, u, Sin@t∗ uD<, 8t, 0, 3<, 8u, 0, 3<D

0

1

2

3

0

1

2

3

-1

-0.5

0

0.5

1

0

1

2

3

0

1

2

3

Ö Graphics3D Ö

demo1.nb 24