Saturday, June 4, 2016

Maxima - Introduction

Maxima (http://maxima.sourceforge.net/) is a mathematical program to allow you to solve multiple type of equations. It has a syntax very similar to many programming languages (Java, C++, Python..). In this article, I will introduce the basic usage to allow later solving more complicated equations. I will not explain in details the mathematical concepts, I will mainly base this article on the programming language. The program I use is WxMaxima (http://andrejv.github.io/wxmaxima/), but I've used it without any help purposed by the program, I could have done the same with the command console of Maxima.

When starting the WxMaxima application, we see an empty command console. What's a command console? It's a non graphical interface where the user can introduce instructions to an application which will be executed when we introduce the final character of our instruction. What's an instruction? It could be a sum, subtraction, variables assignation, functions creation, equation solving... Normally, it's what we introduce in a single line (but you also can introduce multiple instructions per line). The instructions can have one input parameter (the variables we pass between parenthesis), multiples inputs or none. And each instruction we return some output data. Some will return nothing (as the assignation of a value to a variable), and some will return a simple or complex structure of data (the sum will only return the result of the sum, but when transposing a matrix we will get the transposed matrix as the result). How to tell the program the end of our instruction? With WxMaxima, you can do it with Ctrl+Enter. This will add the character ";" at the end of the line and print the output. If you don't want (don't need) the output, use the "$" character instead (the instruction will be executed too). When will I don't need the output? When you set the value 5 to the variable x, you don't need the program to tell you that you assigned the value 5 to x. When you want to save the definition of equation into a variable, it could be interesting to see the result, to confirm you didn't made a typo. What can I save into a variable? Whatever you want. Numerical values, text (a single word or a complete sentence), equations or functions (I will explain later the difference between both), list of data, list of lists (a matrix)... How do I do the assignation? There is 3 ways to do an assignation depending on what you want to assign: ":", "=" or ":=". To assign a value to a variable, use ":". After this assignation, the variable will have the behavior of the numerical assigned value. This is useful when you need to create/introduce an equation which needs constants (as the permittivity of vacuum, the mass of the electron...). For equation, use "=". When you write an equation where you want to take off the value of a variable, use the "=" in the equation. And finally, ":=" is used for the function assignation. When you want to create a equation where you must specify the input and then you get the output, this is a function. Here is an example of the 3 cases:
Maxima assignations
Each line starts with a (%iN), this means the N input we've made. And the output will be (%oN). In the first input, we've created the function f(x) as a*x (the multiplication sign is mandatory, otherwise it will consider a variable named ax). From now we can use f(x) instead of a*x. In the second input, we've assigned the value 5 to the variable a. This way f(x) will remains as 5*x. In the third input, we've created an equation where we want to know the value of y. And in the last input, we've used the Maxima instruction solve to solve our equation. The solve instruction has two inputs, the first is the equation you want to solve, and the second, is the variable you want to know the value (you can have more information at http://maxima.sourceforge.net/docs/manual/maxima_20.html or writing describe(solve); in the application). If we've used the final character ";" instead of "$", the final result wouldn't change:

Lets continue with some useful commands:

Comments: when you're writing complicated and long formulas, sometimes (or always) you need to explain some steps of your formulas. Here you can do it so. The comments start with /* and end with */. They can be at the beginning of the instruction, at the end, or in the middle.Maxima comments

 Blocks: if you have to build a complex and long equation, you can use variables to stock some constant values, or divide your equation in some little steps. When using constants, imagine you use the names a, b and c for the first equation, lets continue with the second equation with d, e and f. If you continue that way, you will mix some constants with the equations. Using blocks, you can have your own a, b and c variables for each equation independently (the naming strategy should be better and more descriptive). Lets see an example.
Blocks in Maxima
First, we see that all the information in the block is stocked into a variable eq() (having parenthesis it's now a function). In this case, we've made a function without inputs, if we need some inputs, they will be declared into those parenthesis.

Blocks in Maxima 
After the block command, we define a list of variables used inside our block, which won't have any meaning outside the block. Then all the instructions which must be used inside the block coma separated (this time we only use the final character when closing the block statement). When editing, we can push Enter to have a new line an have a better readability of our block, Maxima won't compute anything, as it's still missing the final character. And to execute our function, we only have to write eq() and Maxima will execute all the instructions inside our block. When you're asked to write a block with a specific behavior, is quite hard to write is as you see in the picture (from up to down) in one shot. You must first try to decompose the behavior is small parts that you better understand. Dividing a huge function in small parts is always better, as you read it better (for you and for other people), it's better to understand its behavior and repair it if necessary, and more suitable to write and comment.

List and matrices. And finally, I will introduce the data structure used in Maxima. The list and matrices are the most used for inputs and outputs. You can create a list only by typing the symbols "[" and "]" separating each element by ",".
Lists in Maxima
 As you can see, the list can have multiple type of data (this isn't a good practice). And if you want to access to a single element inside the list, you just have to put the index of your element between "[]". For matrices, it's very similar. As it's considered as a list of lists, but you've a specific command to create them.
Matrix in Maxima
 The matrix command is used to create a matrix directly, you just have to specify as input the list of values present at each line.



No comments:

Post a Comment