Maxima Example

Embed Size (px)

Citation preview

  • 8/10/2019 Maxima Example

    1/4

  • 8/10/2019 Maxima Example

    2/4

    Here is some simple arithmetic and algebraentered at the keyboard:

    2^32

    4294967296

    111111111^2(that's a row of nine 1s)12345678987654321 (who would have guessed?)

    sales_tax : 0.085(that's 8.5%)0.085

    cost : 40(the before-tax price of something)40

    cost * sales_tax(compute the tax on the cost)3.4

    cost * 1 + sales_tax(does this give the total amount?)40.085 (apparently not)

    cost * (1 + sales_tax)(does this give the total amount?)43.4 (yes)

    Notice about this example that the result depends on something called the "order of operations". In a mathematical expression without

    parentheses, there is an established order of operations:

    Powers and roots.1.

    Multiplication and division.2.

    Addition and subtraction.3.

    In that order.

    To force a particular sequence of operations that differs from the natural order, use parenthesesto group the desired operations together in

    your expressions. Operations inside parentheses are performed first, followed by operations outside parentheses. And there can be more than

    one layer of parentheses.

    In this example:

    ((a+b)*(c+d))^n

    First ais added to b, and cadded to d. Then those two subgroups are multplied together. Thenthe result is raised to the power n. This example

    forces an opposite of the normal order of precedenceon its terms. Remember when in doubt about how your expressions will be evaluated,

    group terms using parenthesesto clarify your intentions.

    Some more algebra examples:

    %pi;(this is how we identify to Maxima) (this will appear as a Greek letter only if a Greek font has been installed, see"Installation")

    float(%pi);(meaning "give me the numerical value")3.141592653589793

    fpprec:1000;(increase the "floating point precision" or "fpprec")1000

    float(%pi);(again, "give me the numerical value")3.141592653589793 (no change in the number of displayed places. Why?)

    bfloat(%pi);("bfloat" means "big float")3.1415926535897932384626433832[943 digits]3001927876611195909216420196b0 (better, but not what Iwas hoping for)

    set_display(ascii);(this is a special wxMaxima setting that forces all the digits to be printed out)set_display(ascii);

    bfloat(%pi);(try again)(a list of 1000 digits of )

    set_display(xml);(return to the default way of displaying things)set_display(xml);

    The "fpprec" and "set_display(ascii)" settings only affect floating-point numbers, integers are always printed out fully:

    50!;

    30414093201713378043612608166064768844377641568960512000000000000

    500!;

    (a huge list of digits)

    Here's an example that uses a Maxima function, plus one we define as well:

    apply("+",[1,2,3,4,5]);

    15

    This gives me an idea.

    average(a) := apply("+",a)/length(a);

    02. First Examples http://arachnoid.com/maxima/first_examples.html

    2 din 4 02.11.2014 23:19

  • 8/10/2019 Maxima Example

    3/4

    There is another way to write this function:

    average(a) := sum(a[i],i,1,length(a))/length(a)

    average([1,2,3,4,5,6,7,8,9,10]);

    float(%);

    5.5

    Hold on. What does "%" mean? Well, it refers to the prior result. If you are creating intermediate results and want to refer to the immediate prior

    result, use "%". There are variations on this operator that can be used to get any arbitrary prior result examine the help entry for "%".

    Here's something we need to watch out for:

    set_display(ascii);radians(x) := x * %pi / 180;

    x %piradians(x) := -----

    180

    Hold on again. Why did the display revert to a more primitive state (not drawing equations nicely or rendering Greek letters)? It's because we

    forgot to return to "set_display(xml)", so let's fix that:

    set_display(xml);

    set_display(xml);

    radians(x) := x * %pi / 180;

    Okay, new rule: if you want to list all the digits of a floating-point value, use "set_display(ascii);". If you want wxMaxima to render equations and

    functions in a pretty way, use "set_display(xml);". It seems you can't have both at the same time.

    ? diff

    (description of "diff" and related functions from the Maxima help file)

    f(x) := x^3

    f(x):=x3

    According to my Calculus textbook, the first derivative of x3should be 3 x2. Let's see.

    diff(f(x),x)

    3 x2

    diff(f(x),x,2)

    6 x

    diff(f(x),x,3)

    6

    diff(f(x),x,4)

    0

    All correct. When using the "diff()" function, remember we can tell it which derivative to acquire by adding an optional third argument 2 for

    second derivative, 3 for third derivative, etc.. Now let's try the function that does the opposite of derivation.

    integrate(f(x),x);

    integrate(integrate(f(x),x),x);

    integrate(integrate(integrate(f(x),x),x),x);

    Okay, those are the right results, but it seems we can't simply specify which integral to obtain as we can with taking derivatives, instead we mustnest our calls to "integrate". One could write a recursion function to do this to any arbitrary depth, but that's an advanced topic later for that.

    02. First Examples http://arachnoid.com/maxima/first_examples.html

    3 din 4 02.11.2014 23:19

  • 8/10/2019 Maxima Example

    4/4

    sum(n,n,1,9);

    45

    sum(n^2,n,1,9);

    285

    The function "sum()" adds together the results of an expression or function, according to the provided index values.

    product(n,n,1,9);

    362880

    product(n^2,n,1,9);

    131681894400

    "product()" is somewhat like "sum()", but it multiplies the results together instead of adding them. Given how multiplication works, the above

    example might as well be:

    product(n,n,2,9);

    362880

    product(n^2,n,2,9);

    131681894400

    Do you see why? It's because the original form multiplied the result by 1, a rather pointless operation.

    Moving On

    I ask that my readers feel free to explore what this program offers. Type in equations that you are curious about, create functions to encapsulate

    things you have discovered. Explore.

    In the next section, I will show how to load and save your work, and how to put the solution to one problem in a wrapper that makes it available to

    solve others.

    Home | Mathematics | * Maxima | 02. First Examples Share This Page

    02. First Examples http://arachnoid.com/maxima/first_examples.html

    4 din 4 02.11.2014 23:19