Introduction to Programming

“Processing” is a free programming environment designed for non-programmers. This page describes how to use Processing to calculate values of a function. This might be useful for algebra homework:

  1. Download the Processing programming environment. Processing is available for  Mac and Windows (and Linux).
  2. Unzip or install, as needed. Set up a shortcut if you wish.
  3. Invoke Processing
  4. Copy and paste this program into the main window:
    void setup() {
    
      int x;
      float y;
    
      for ( x=1; x<10; x++) {
        y = log( x );
    
        print(x);
        print("\t");
        println(y);
    
      }
    
    }
  5. To run the program, press the “play” button on the top left. The answers appear in the black part of the window below where you pasted the program. Ignore the little pop-up box as we’re not using this feature.

Other tips:

  1. Log here is really ln. Very bad choice of name, if you ask me.
  2. If you need a log to a different base, remember than
    log(different base) (x) =  log (x)/log(different base)
  3. Use plenty of parenthesis to make sure that calculations are done in the order you would like
  4. Use as many spaces or new lines as you wish to make it more readable. The program ignores these so-called “white spaces”.
  5. To raise to a power, use the “pow” command e.g. x squared is pow(x,2)
  6. Times is ‘*’
  7. Your equation (and every statement) must end with a semicolon
  8. If you change your program you have to hit the “stop” button and then the “play” button again in order to see the results

Here is a more complex example for y=5+(log(base 12) ((x-7)squared). Note that in this example x must be greater than 7 so that we never take the log of zero:

void setup() {

  int x;
  float y;

  for ( x=8; x<20; x++) {
    y = 5+
      (     log ( pow( x-7 , 2))     /log(12)   );

    print(x);
    print("\t");
    println(y);

  }

}

Here is an example of a quadratic function, and also shows how to increase x in steps of 5:

void setup() {

  int x;
  float y;

  for ( x = -20; x < 20; x += 5) {
    y = 3*pow (x, 2)  + 12*x - 5;

    print(x);
    print("\t");
    println(y);

  }

}
Print Friendly
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.

Leave a Reply

Your email address will not be published. Required fields are marked *