# Maxima Basics #2

Maxima Version 5.20.1
wxMaxima Version 0.8.4

(1)
With all computer algbra systems there are differences between functions and expressions.
Names are assigned to expressions using the colon.  Functions are defined differently (see
below).  We substitute into expressions, and we evaluate functions.

There are several ways to substitute values into expressions.  You have already seen the
subst() command.  Another approach is to use at().  

f: (3*x+1)*(x-1)*(x+2)^2;

at( f, x=1 );

at( f, x=-3/2 );

subst( a^2, x, f );

g: 2*x*y+y^2-x;

at( g, [x=1,y=2] );

kill( f, g );

(2)
Since the example above made use of polynomials, let's look at some commands used to 
deal with polynomials:
expand( expr ) expands expression expr and combines like terms.
factor( expr ) attempts to factor expression expr.
divide( expr1, expr2, var ) returns the quotient and remainder when expr1 is 
   divided by expr2, where the expressions are polynomials in variable var.

f: (2*x+1)^2*(x-3)*(5*x-2)*x^2;

expand(f);

factor(%);

divide( f, x^2+1, x );

kill(f);

(3)
Maxima's solve() command is used to find exact solutions of an equation.

f: x^2-3*x;

sol: solve(f=7,x);

The solve command returned a list containing the two solutions.  You can reference the
individual elements of a list using list[1], list[2], etc. 

sol[1]; sol[2];

The solutions were given as equations.  rhs( eq ) returns the right-hand side of eq.

root1: rhs( sol[1] );

subst( root1, x, f );

expand(%);

kill(all);

(4)
When an exact solution cannot be found, find_root() gives a numerical approximation:
find_root( eq, var, a, b ) returns the numerical approximation of a solution
of equation eq in variable var bewteen var=a and var=b.

solve( cos(x)=x, x );

find_root( cos(x)=x, x, 0, 5 );

(5)
Maxima's diff() command is used for differentiation.
diff( expr, var, n ) returns the nth derivate of expr with respect to variable var.
If n=1, the order can be omitted.

f: 3*x^2 + 1/x + sin(x) + exp(x);

fp: diff( f, x );

diff( fp, x ); diff( f, x, 2 );

kill(all);

Instead of defining f as an expression, here we'll define f as a function.  To do so
we need to use " := ".

f(x):= 3*x^2 + 1/x + sin(x) + exp(x);

f(%pi);

float(%);

diff( f(x), x );

Unfortunately, if we use diff() to differentiate f(x), we get an expression, not a function.
One way to get the diff() to return a function is to use it with a dummy variable 
and the at() command.  Try to follow these examples...

fp(x):= at( diff(f(u),u), u=x );

fp(x);

fpp(x):= at( diff(f(u),u,2), u=x );

fpp(x);

fp(2); fpp(%pi); f(%e);