lecture2: algebra, calculus, numerical analysis algebra

Upload: aroobadilawer

Post on 30-May-2018

241 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/14/2019 Lecture2: Algebra, Calculus, Numerical Analysis Algebra

    1/20

  • 8/14/2019 Lecture2: Algebra, Calculus, Numerical Analysis Algebra

    2/20

    f@a + b ^ 2 + c 2D

    3 + a + b2 +c2

    As you can see, the function f did its job: It took the argument provided and added 3. In this case the pattern x_

    took the value a+b^2+c/2. If you had defined f[x], instead of f[x_], this would have not worked:

    g@zD = z + 3;

    g@a + b ^ 2 + c 2D

    gAa + b2 + c2

    E

    As you can see, g[z] is not as smart as f[x_]!! You should have done:

    2 Lecture2.nb

  • 8/14/2019 Lecture2: Algebra, Calculus, Numerical Analysis Algebra

    3/20

    g@zD . z a + b ^ 2 + c 2

    3 + a + b2 +c2

    to make it work, because g takes only the variable z as its argument; but generally this is not a convenient way to

    define a function.

    IMPORTANT: Remember that the argument of a function is a pattern (x_), not a variable (x).

    As with theMathematica Intrinsic functions and with all variables, with the functions defined by yourself you can

    check their definition with the question marks, ?for ??f.

    The generalization to functions of several variables is trivial:

    p@x_, y_, z_D = x + y z ^ 2

    By the way, you can apply f[x_] to an expression, say y, in three different ways:

    f[y] Like we did above

    y // f Like we have already done with the function N, e.g. Expression // Nf @ y

    Delayed evaluation of functions

    Sometimes you may want to wait to evaluate a function till a lower position in the program. Sometimes you will

    have no choice and will not be able to evaluate a function immediately. In that case,Mathematica still allows you to

    define the function, and to refer to it later on, when it can be evaluated because the needed information has become

    available. Delayed evaluation is specified by using := instead of=

    f@x_D := x ^ 2 10.

    You can type SHIFT+ENTER (like I have just done), but you won't see any output. If you read theMathematica

    Book, you will discover that this so called "delayed equal" is presented as the standard way to define a function,

    because it is generally true that we introduce functions long before we can use them. However, when possible, it is

    legal to define function with the normal = symbol. That is possible more often than you may think. You may think

    you need the delayed equal for f[x_]:=x^2+a, if x or a are not defined yet. Wrong! Remember thatMathematica

    strength is symbolic manipulations, andMathematica is perfectly happy to receive the expression x^2+a and work

    with it, until you decide, perhaps, to give x some value. The delayed evaluation becomes truly necessary in situa-

    tions where the function is defined for example using the solution of an equation (maybe a differential equation)

    that has not been solved yet.

    You can define functions also with conditions. This is necessary when functions have discontinuities, so a simple

    analytical expression without conditions cannot cover the full range of values of the argument of the function. Thegeneral syntax makes use of/; like this:

    f[x_] := something /; condition

    Here is the Heaviside function:

    h@x_D := 1 ; x > 0;

    h@x_D := 0 ; x < 0

    Lecture2.nb 3

  • 8/14/2019 Lecture2: Algebra, Calculus, Numerical Analysis Algebra

    4/20

    The condition can be any of many Mathematica logical statements:

    x = = y Test if they x and y are equal

    x y Test if x and y are not equal

    x > y Test if x is larger than y

    x y ..........

    x < y ..........

    x y ...............

    condition1 && condition2 Test if both conditions are true

    condition1 | | condition2 Test if either condition1 or condition2 are true

    Example:

    4 < 3 & & 2 1

    False

    Calculus

    You can askMathematica to compute derivatives of a function with two different ways:

    D[f[x],x]

    f ' [x]

    The first syntax is more general, because it is trivially generalized to partial derivatives as well:

    D[f[x,y,z],y]

    For example:

    f@x_, y_D := x ^ 2 + y;

    D@f@xD, xD

    2 x

    You can easily see how the other syntax is insufficient for a partial derivative, unless you are interested only in the

    derivative of the first variable in the argument of the function:

    4 Lecture2.nb

  • 8/14/2019 Lecture2: Algebra, Calculus, Numerical Analysis Algebra

    5/20

    f '@yD

    2 y

    I was trying to get the partial derivative with respect to y, but I still got it with respect to the first argument, x_,

    which is now called y (which is logic because x_ is a pattern, not the variable x).

    If you want to compute a Taylor series to define an approximation of a function of x around a point x=a, you may

    think you have to compute all the necessary derivatives in the series expansion:

    fHxL > fHaL + Hx- aLfHaL + 12Hx - aL2 fHaL + ... + 1

    n!Hx - aLn f

    xnx=a + ...

    Actually,Mathematica is better than that, it can compute the power series directly with the function

    Series[f[x],{x,a,n}]

    where n stands for the number of terms in the series. For example:

    g@x_D := Cos@xD ^ 2 + Sqrt@x ^ 2D;

    s = Series@g@xD, 8x, a, 4

  • 8/14/2019 Lecture2: Algebra, Calculus, Numerical Analysis Algebra

    6/20

    Integrate@H2 + x ^ 2 + x ^ 4L Ha + x ^ 2L , x D

    H1 - aL x + x33

    +

    H2 - a + a2L ArcTanA x!!!!a

    E !!!

    a

    Integrate@H2 + x ^ 2 + x ^ 4L Ha + x ^ 2L , 8x, -60, 60< D

    IfAImA!!!a E 60 ImA!!!a E -60 ReA!!!a E 0, -120 H-1201 + aL + 2 H2 + H-1 + aL aL ArcTanA60

    !!!!a

    E!!!

    a,

    IntegrateA 2 + x2 + x4a + x2

    , 8x, -60, 60

  • 8/14/2019 Lecture2: Algebra, Calculus, Numerical Analysis Algebra

    7/20

    % N

    88x -1.33357 - 0.992949

  • 8/14/2019 Lecture2: Algebra, Calculus, Numerical Analysis Algebra

    8/20

    423 aikjjjjjj1 - 1 +

    !!!3

    223 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13 -I1 - !!!3 M I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13

    2 213y{zzzzzz2

    +

    9 a2ikjjjjjj1 - 1 +

    !!!3

    223 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13 -I1 - !!!3 M I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13

    2 213y{zzzzzz2

    + c,

    x -10 + a +3

    213 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M23 -3

    !!!3

    213 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M23 +

    3 213 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13 +

    3 213 !!!3 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13 +

    3 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13

    213 -

    3 !!!3 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13

    213 +

    3 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M23

    2 223 +3

    !!!3 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M23

    2 223 ,

    y 1 -1 +

    !!!3

    223 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13 -I1 - !!!3 M I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13

    2 213 =,

    9z 838 - 948 a + 111 a2 - a3 + 271 ikjjjjjj1 - 1 -

    !!!3

    223 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13 -I1 + !!!3 M I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13

    2 213y{zzzzzz -

    297 aikjjjjjj1 - 1 -

    !!!3

    223 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13 -I1 + !!!3 M I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13

    2 213y{zzzzzz +

    27 a2 ikjjjjjj1 - 1 - !!!3

    223 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13 -I1 + !!!3 M I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13

    2 213y{zzzzzz +

    2601ikjjjjjj1 - 1 -

    !!!3

    223 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13 -I1 + !!!3 M I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13

    2 213y{zzzzzz2

    -

    423 aikjjjjjj1 - 1 -

    !!!3

    223 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13 -I1 + !!!3 M I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13

    2 213y{zzzzzz2

    +

    9 a2ikjjjjjj1 - 1 -

    !!!3

    223 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13 -I1 + !!!3 M I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13

    2 213y{zzzzzz2

    + c,

    x -10 + a +3

    213

    I3 - a +

    !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2

    M23 +

    3 !!!3

    213

    I3 - a +

    !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2

    M23 +

    3 213 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13 -

    3 213 !!!3 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13 +

    3 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13

    213 +

    3 !!!3 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13

    213 +

    3 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M23

    2 223 -3

    !!!3 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M23

    2 223 ,

    y 1 -1 -

    !!!3

    223 I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13 -I1 + !!!3 M I3 - a + !!!!!!!!!!!!!!!!!!!!!!!!5 - 6 a + a2 M13

    2 213 ==

    How many boring hours did we save? Solve is so nice that it can be applied to systems of equations with no solu-

    tions or underdetermined, that is with non-unique solutions. If you put the system of equations into the matrix

    form, such system corresponds to a matrix with zero determinant: It may have no solutions or the system is underde-

    termined (you may be able to solve for one variable as a function of some others, so the system has infinite solu-tions).Mathematica messages will be pretty explicit when that happens.

    IMPORTANT: When you are trying to solve equations or systems of equations you must be very careful to

    make sure that your unknowns have not been assigned any values or definitions earlier on, or for example

    right here due to a typo (e.g. = instead of = =). To be safe, you may want to Clear all unknowns prior to use

    them in a system of equations.

    And now the limitations of Solve and NSolve: Pretty strong limitations, actually. Try to solve anything that is not a

    polynomial expression, that is something requiring non-algebraic type of manipulations, and most likelyMathemat-

    ica will complain you are asking too much, even with NSolve. If that happens, then you'd better be CREATIVE.

    8 Lecture2.nb

  • 8/14/2019 Lecture2: Algebra, Calculus, Numerical Analysis Algebra

    9/20

    You will need to guess the solutions, at least the ones you care for, but you will have to invent techniques to educate

    your guess.......but that's the next calss......

    Numerical Analysis

    Finding solutions to complex nonlinear equations is far from an automatica process. You will find it requires some

    creativity, as numerical methods for finding slutions require initial guesses. It may also be hard to know if we have

    found all possible soltions or not. For single equations, or for systems of two coupled equations, you can rely on

    plotting the single function of one variable, or contour plotting the two functions of two variables. The function to

    use is FindRoot, with the following syntax:

    FindRoot[equation,{variable,guess}]

    IMPORTANT: The function FindRoot is for numerical computations, hence the equation must be fully numerical

    (no undefined parameters or variables apart from the unknowns).

    Example:

    FindRoot@Sin@xD - x ^ 3 + Cos@xD - 1.0 0, 8x, -1

  • 8/14/2019 Lecture2: Algebra, Calculus, Numerical Analysis Algebra

    10/20

    Sin@xD - x ^ 3 + Cos@xD - 1.0 . % InputForm

    -7.771561172376096*^-16

    That's a good accuracy of course! But let's imagine my initial guess had been less educated, for example:

    FindRoot@Sin@xD - x ^ 3 + Cos@xD - 1.0 0, 8x, 90000000000000000.9

  • 8/14/2019 Lecture2: Algebra, Calculus, Numerical Analysis Algebra

    11/20

    8x ^ 3 - Exp@2 + y ^ 2D, Cos@xD + Sin@yD< . %

    8-0.334902, 0.000751537