If you type any letter, such as x, or t, or p, in a Maxima command, it automatically interprets it as a variable. But what if you want to make a variable have a specific value? Let's say we are doing a problem that has a constant R that is defined to be 3.75, and all of the calculations we need to do are written using R. It would be nice to set R=3.75 and then use R in our calculations. Let's try this in the most obvious way:
-->
R=3.75;
\[\tag{%o1} R=3.75\]
O.k., that looks pretty good. But watch what happens if we try to get back the value of R:
-->
R;
\[\tag{%o5} R\]
Or try to do a calculation with R:
-->
%pi*R^2;
\[\tag{%o2} \ensuremath{\pi} {{R}^{2}}\]
Wait... this isn't really what we want! The issue is that Maxima uses = for equations, *not* for setting values. The way to set a value in Maxima is to use a colon (:), which is called -binding-
Now, if we want to use R or A to represent a different value later on, we have two options:
1) We can redefine R and A using (:) again.
2) We can kill them.
You read that right. Maxima is a violent language (things were rougher in the 1960's...) To free up a variable so that you can use it somewhere else, we kill it like so:
You can use kill to free up ANY object that you create in Maxima - not just variables! Finally, you can kill all variables that have been assigned in a given session by typing:
-->
kill(all);
\[\tag{%o0} \mathit{done}\]
2 Functions
Let's say that we want to define a function f(x) = 2x+7. We just saw that using = doesn't cut it. What if we try (:)
-->
f(x):2*x+7;
\[\mbox{}\\\mbox{assignment: cannot assign to }\operatorname{f}(x)\mbox{ -- an error. To debug this try: debugmode(true);}\]
Hrmm... That's frustrating. The issue here is the same issue that we encountered when we tried to multiply using ()'s. Maxima is programmed to read () as a function - even though we are in the process of trying to define f(x)! Maxima has a special symbol for function definitions, it is the same symbol used frequently in Computer Science Textbooks to define values: it is a colon followed by and equal sign (:=)
It works just fine. But what if you wanted to evalute E for x=%pi? Let's give it a try:
-->
E(%pi);
\[\mbox{}\\\mbox{apply: found }E\mbox{ evaluates to }{{x}^{3}}-2 x \sin{(x)}\mbox{ where a function was expected.}\mbox{ -- an error. To debug this try: debugmode(true);}\]
The problem here is that E is an -expression- not a function. We can substitute values into an expression using subst() like so:
-->
subst(x=%pi,E);
\[\tag{%o28} {{\ensuremath{\pi} }^{3}}\]
But what we really need is a way to elevate E to a function. This will become even more important later when we want to "catch" the results of integration and differentiation as functions and not symbolic expressions. The solution is the command define(), and we use it like so:
-->
define(fE(x),E);
\[\tag{%o29} \operatorname{fE}(x):={{x}^{3}}-2 x \sin{(x)}\]
Note that the function has to spell out the fact that x is the dependent variable in the notation fE(x). Now we have the -functional- version fE(x) of E that is a genuine function:
But note that E is now a function, and not a symbolic expression. Most of the time this will not cause any serious issues, but we note it here just in case.
-->
kill(all);
\[\tag{%o0} \mathit{done}\]
3 Equations
3.1 Defining and Working With Equations
We saw that wxMaxima reserves = for use in equations. A cool feature is that we can actually write down an equation and assign a name for it!
-->
E:x^2=4;
\[\tag{E}{{x}^{2}}=4\]
-->
E;
\[\tag{%o2} {{x}^{2}}=4\]
-->
F:2*x-5=7;
\[\tag{F}2 x-5=7\]
Why would you want to name an equation?! Well, this makes it easier to work with (we'll see this later on) but we can also do some pretty cool algebra. For example, let's say I wanted to add equation E to F and see the result. Here's how it works in Maxima:
-->
E+F;
\[\tag{%o16} {{x}^{2}}+2 x-5=11\]
Which is nice, because this is precisely the obvious thing to do. So what about a more complicated combination like: Square both sides of F:
-->
F^2;
\[\tag{%o17} {{\left( 2 x-5\right) }^{2}}=49\]
Or, square root both sides of F and subtract three times E:
You have to admit, that's pretty neat. One last thing. What if we want just the right hand side of an equation, or the left hand side? Maxima has functions rhs() and lhs() that grab the left and right hand sides of equations:
One of the main reasons computer algebra systems like Maxima were created was to simplify the task of solving tedious equations. Maxima has a built in function called solve() that does this:
The %i here represents sqrt(-1), the complex unit.
So we see that the solve function takes in an equation as the first input and the variable we want to solve for as the second. It returns a list of all the solutions. Why do we have to specify a variable? Because Maxima can solve equations that only are written using variables!
\[\tag{%o23} [x=-\frac{\sqrt{{{b}^{2}}-4 a c}+b}{2 a},x=\frac{\sqrt{{{b}^{2}}-4 a c}-b}{2 a}]\]
O.k., so that's already pretty cool, but it gets better. Let's say we have a function f(x) = 3*x^2 - 2*x and want to know when f(x) = 7. We can do the obvious thing:
Yuck! It's nice we did not have to go and calculate all of that! But now that we have this list, what if I want to use one of the solutions, but I don't want to type it in? Here's how we do it: "Catch" the solutions by assigning a name to them:
Now, that's great, but what if we want, for instance, the actual value contained in L[3]? Typing L[3] gives back an equation, not the value. To get the value, we use rhs():
I will be the first to admit that this is a little goofy, but this is one of the challenges that faces us when we use technology to solve problems. The computer can only give back the solutions in the form that it is programmed to give it, and it is up to us to extract the information that we want.
-->
kill(L);
\[\tag{%o108} \mathit{done}\]
3.3 Solving Equations with Radicals
Often we come across equations with radicals in them, like:
-->
E:sqrt(x)=sqrt(8-x);
\[\tag{E}\sqrt{x}=\sqrt{8-x}\]
And we would like to solve them. But notice that when we attempt to use solve() we get:
-->
solve(E,x);
\[\tag{%o7} [\sqrt{x}=\sqrt{8-x}]\]
Which is really not helpful. The problem is that Maxima views the radical expressions are variable quantities in their own right, and thus it thinks that it did solve the equation. To get around this we use a more powerful version of solve() called to_poly_solve() that converts radical equations to polynomial equations first before solving.
So what's the deal with %union? This is telling us that the solutions form a set comprised on the elements in the union. To get the elements out of the union, we use the part() function like so:
We can further extract the values using rhs() and lhs() for use!
-->
kill(all);
\[\tag{%o0} \mathit{done}\]
3.4 Solving an Equation and Substituting the Result
MANY times in Algebra, we need to solve an equation, and then turn around and substitute our solution into another formula. In Maxima, the function: subst() does substitution for us.
-->
subst(x=3, x^2+1);
\[\tag{%o109} 10\]
Admittedly, in this context, subst() seems pretty lame. But let's take a look at how this can be *really* useful. Imagine a problem where the radius of a sphere is found by solving: 2*R^3 - 5*R = 7. So let's solve to find the radius, and then use the radius to find the volume of the sphere!
So, even though giving solutions as equations seems a little awkward, there is good reason for it!
As a parting thought, we can streamline a lot of the work that we just did, provided we already know (by doing the calculation above) yields the real solution:
The above is a demonstration of the real power of a CAS, but please do note that the above is very difficult to read. If you have the option of solving something in one very complicated line of dense expressions, or using several, easy to understand calculations - it is always better to do the latter.
-->
kill(all);
\[\tag{%o0} \mathit{done}\]
Created with wxMaxima.