Mathematica



Artist and mathematician, George Hart taught a course in the Fall of 2007 at Stony Brook University that covered procedural generation of three-dimensional forms, with an emphasis on making triangulated manifold boundaries suitable for solid freeform fabrication (SFF).

Hart uses Mathematica to generate his models.



Introduction to Mathematica

To start a new program in Mathematica, create a new Notebook. A Notebook is the main window for entering input and for displaying results

To enter input, just start typing. You can create, save, open and close a notebook using the File menu.

Notebooks are organized into units called cells. These cells are identified by the brackets on the right hand side.The insertion point is indicated by a horizontal line that appears after your output; it is called the cell insertion bar . This is where a new cell will appear after you start typing. Move the cursor in your notebook window until it becomes a horizontal I-beam.

To create a text cell, get a cell insertion bar. Then choose Format >Style >Text or use the keyboard shortcut Alt+7 (Command+7 on Mac).

To evaluate input, press SHIFT+ENTER.

Mathematica supports standard mathematical operations. In addition to using * for multiplication, you can just separate numbers by a space. So xy is not the same as x y, but x y is the same as x*y

Calculations in Mathematica are done by calling functions. The most general way to enter a function is to give the name of the function followed by square brackets that enclose the argument or arguments of the function. If a function has more than one argument, separate them by commas. (Function arguments are enclosed in square brackets and are separated by commas.)

The function N is used to give numerical approximations to its first argument:
N[Pi,50]


{x, 0,20} 
specifies that x = 0 to 20

{2, 4, 6} + {100, 40, 15}
returns {102,44,21}

Mathematica is case sensitive and the names of built-in functions are always capitalized.

Like other programming languages, you can assign values to variables like this:
x=10;
If you need to check the value of your variable, do this:
?x
Don't forget to Clear your global variables when you are done with them:
Clear[x]


A sequence of statements separated by semicolons is called a compound expression. You can also nest statements.

The Mathematica Documentation Center is the main interface to documentation in Mathematica. Open the documentation by going to the Help menu in Mathematica and then selecting Documentation Center. Alternatively, select a function name in any notebook and press F1 (or fn F1). Just like variables you can see a function definition by preceding the function with a ?
?Plot


When looking up a function, you can use the * symbol to represent a wildcard:
wildcard.png

If you type a function and then press CTRL+SHIFT+K or Command+SHIFT+K, Mathematica will show you the parameters that the function takes.

Plot takes one parameter. To plot several functions together, you can put them in a list as the first argument to Plot:
Plot[{Sin[x], 2 Sin[2 x], 1/3 Sin[3 x]}, {x, 0, 2 Π}]
The Plot3D function plots a function as a three-dimensional surface.
Plot3D[Sin[x - Sin[y]], {x, 0, 3 Pi}, {y, 0, 4 Pi}]
To rotate this graphic, move your cursor over the graphic, hold down the left mouse button, and rotate the image by moving the mouse.

To zoom in and out, hold down Command or Option Key and move your cursor over the graphic and then drag the pointer up to zoom in and down to zoom out.
Use the SHIFT key to pan.

You can combine surfaces:
Plot3D[{Sin[x - Sin[y]], Sin[y - Cos[x]]}, {x, 0, 3 Pi}, {y, 0, 
  4 Pi}]



//Introduction



You can connect Arduino to Mathematica using the Serial Call and Response concept.


William J Turkel does computational history, big history, STS, physical computing, desktop fabrication and electronics. He created a small guide to connecting the Arduino to Mathematica on Mac OS X with SerialIO in December of 2011:

A Simple Circuit

Create a simple circuit with a potentiometer and an Arduino board connected to a computer via a USB cable.
  1. Connect the potentiometer to pin A0 on the Arduino


  2. Copy and upload the following code into Arduino:
      //----------------------Arduino code-------------------------
    
    /*
     arduino_mathematica_example
    
     This code is adapted from
     http://arduino.cc/en/Tutorial/SerialCallResponse
    
     When started, the Arduino sends an ASCII A on the serial port until
     it receives a signal from the computer. It then reads Analog 1,
     sends a single byte on the serial port and waits for another signal
     from the computer.
    
     Test it with a potentiometer on A1.
     */
    
    int sensor = 0;
    int inByte = 0;
    
    void setup() {
      Serial.begin(9600);
      establishContact();
    }
    
    void loop() {
      if (Serial.available() > 0) {
        inByte = Serial.read();
        // divide sensor value by 4 to return a single byte 0-255
        sensor = analogRead(A0);
        sensor=map(sensor, 0,1024, 0,255);
        delay(15);
        Serial.write(sensor);
      }
    }
    
    void establishContact() {
      while (Serial.available() <= 0) {
        Serial.print('A');
        delay(100);
      }
    }
    //--end Arduino code--------------------------------



  3. Upload the sketch to the Arduino. Close the Arduino IDE (otherwise the device will look busy when you try to interact with it from Mathematica).


  4. Install the SerialIO package in /HD/Library/Mathematica/Applications
    and make sure that it is in your path. If the following command does not evaluate to True
    MemberQ[$Path, "/Library/Mathematica/Applications"]
    then you need to run this command
    AppendTo[$Path, "/Library/Mathematica/Applications"]



  5. Edit the file /Library/Mathematica/Applications/SerialIO/Kernal/init.m so the line
    $Link = Install["SerialIO"]
    reads:
    $Link =
    Install["/Library/Mathematica/Applications/SerialIO/MacOSX/SerialIO",
    LinkProtocol -> "Pipes"]



  6. If you need to find the port name for your Arduino, you can open a terminal and type
    ls /dev/tty.*



  7. Paste the following into a notebook:
    <<SerialIO`
    
    myArduino = SerialOpen["/dev/tty.usbmodem3a21"]
    
    SerialSetOptions[myArduino, "BaudRate" -> 9600]
    
    SerialReadyQ[myArduino]
    
    Slider[
     Dynamic[Refresh[SerialWrite[myArduino, "B"];
      First[SerialRead[myArduino] // ToCharacterCode],
      UpdateInterval -> 0.1]], {0, 255}]



  8. The Mathematica code loads the SerialIO package, sets the rate of the serial connection to 9600 baud to match the Arduino, and then polls the Arduino ten times per second to get the state of the potentiometer. It doesn’t matter what character you send the Arduino (here we use an ASCII B). You need to use ToCharacterCode[] to convert the response to an integer between 0 and 255. If everything worked correctly, you should see the slider wiggle back and forth in Mathematica as you turn the potentiometer. When you are finished experimenting, you need to close the serial link to the Arduino with:
    SerialClose[myArduino]