Tuesday 5 January 2010

Chunk 55

I will be completing Chunk 55 which explains how polynomials can be used to draw different types of curves in Processing.
The introduction to polynomials has already been written by Barry and I will attempt to carry on from there. This will mean that this chunk will be co-authored by Barry and myself.

Text for Chunk 55 following Barry's introduction.

Each component of a polynomial expression is called a term, and these are usually ordered from the highest exponent to the lowest. Polynomial expressions do not contain negative or fractional exponents. A typical polynomial expression would have an ordered syntax:E.g. 6xcubed+5xsquared+7x-12 (Not sure how superscript is used in blogger)
In this expression the leading term which has the largest exponent is 6xcubed and its degree is 3. ie 6x6x6 meaning 6 to the power of 3.
The leading term has a coefficient of 6, the second term coefficient is 5, the third term has a coefficient of 7 and the final term, which is a constant, doesn't have a coefficient.
Monomial expressions have only one term, binomials have 2 and trinomials 3.
When working with polynomials it is usual to name them for their leading degree (largest exponent) The expression 6xcubed + 5xsquared +7x - 12 would therefore be a 3rd degree polynomial or cubic.
Polynomials of 2 degrees are called quadratics
Polynomials of 3 degrees are called cubics
Polynomials of 4 degrees are called quartics
Polynomials of 5 degrees are called quintics
In Chunk55 we will only be dealing with quadratic and cubic polynomials but it is interesting to see how the degree and coefficient of a polynomial expression affect the curves when plotted on a graph.
Polynomials with an even numbered degree in their leading term, always enter and leave in the same direction. ie up or down. Therefore, quadratic polynomials are always parabolas.
If the coefficient is even, then the curve will enter and leave at the top of the graph and if the coefficient is odd, it will enter and leave at the bottom.
Polynomials with an odd numbered degree in their leading term, however, have curves that start and end in opposite directions. An odd numbered leading degree with a positive coefficient, will have a curve that starts at the bottom and exits at the top, while a negative coefficient, will have its starting curve coming in from the top and exiting at the bottom.
To plot quadratic polynomials, we need to be able to place the x and y coordinates from 0 to the size of the window. We do this by using an offset of half the width/height of the window.
The width of the parabola is shifted right by adding half the width of the window to the x coordinate and this variable has been named xShift.
The height of the parabola is shifted down by subtracting half the height of the window from the y coordinate and this variable has been named yShift. This allows both upfacing and downfacing curves to be displayed to be displayed together in the window.
An example quadratic such as:
y= 6xsquared-7x+10 In Processing is written as:
y= 6* pow(x, 2)-7*x + 10;
We know that the Processing display screen's coordinates start at the upper left corner,
which is the opposite of a Cartesion Grid's layout, therefore we plot x and y with the statement:
point(x+xShift, yShift-y*ratio); In our sketch we want to be able to display positive and negative versions of a quadratic expression at the same time so we must use yShift variable
to place the curves centrally in our display window. The ratio uses our maximum coEfficient to
calculate the spacing for our plots. As our maximum coEfficient is 50 we work out the scale with
the statement:
float ratio = height/(50*pow(loopLimit-1, 2)-50*loopLimit-1 + 100);
The statements: for (int i=-xShift; i<loopLimit; i++){ y = coEfficient* pow(x, 2)-7*x + 1; point(x+xShift, yShift-y*ratio); }
would draw one curve with plots of x and y for our chosen quadratic expression.
In this sketch, we will replace the leading term's coEfficient, with a variable between 2 and 50, in a for next loop. This will give us a graph of nested parabolas with a variable coEfficient.
A
s the exponent is an even number, we know our curves will all enter and leave in the same direction, also because our equation is quadratic we will have a parabola and because the coEfficient is even its opening will be at the top.


graph of y= coEfficient*xsquared-7x+10
where coEfficient=2 To 50
When we add a plot of a quadratic parabola, with a negative coefficient in its leading term, we end up with this graph:

graph of y= coEfficient*xsquared-7x+10 and
y= -coEfficient*xsquared-7x+10

Code for final quadratic sketch:
/*author: Rosie Wood
January 2010*/
size(400, 400);
background(255);
strokeWeight(3);
float x = 0, y = 0;
int loopLimit = 200;
//shifts curve to the right
int xShift = width/2;
int yShift = height/2;
/*As explained in Greenberg if magic numbers are used, the curves can
be too large for the display window. He used the
polynomial and plugged in the loop limit to get the maximum. This
way if the window size changes, the program should still work.
In this sketch I have tried to get a ratio that will work as long
as the leading term's coEfficient is <=50 the second term coefficient
does not make much difference and can be greater than 100 and still fit in the window. A constant also makes little difference to how well the curve fits in the window
although the beginning and end of the curves could be outwith the
boundaries of the display window*/
float ratio = height/(50*pow(loopLimit-1, 2)-50*loopLimit-1 + 100);
int coEfficient=0;
for(coEfficient=2;coEfficient<50;coEfficient++){
stroke(random(255),random(255),random(255));
for (int i=-xShift; i<loopLimit; i++){

x = i;
/*Here the coefficient of the leading term is positive so the parabola
should enter and leave at the top*/

y = coEfficient* pow(x, 2)-7*x + 1;
point(x+xShift, yShift-y*ratio);
/*Here the coefficient of the leading term is negative so the parabola
should enter and leave at the bottom*/

y = -coEfficient* pow(x, 2)-7*x + 1;
point(x+xShift, yShift-y*ratio);
}}

Cubic Polynomials
When graphing polynomials, the maximum number of bumps is equal to the degree of the polynomial-1. i.e. n-1 , where n =the degree of the leading term.
A cubic polynomial has a degree of 3 in its leading term (x*x*x), so it will have at most 2 bumps (inflections/changes of direction).

Cubic Polynomial with a
positive coefficient in it's

leading term.


Cubic Polynomial with a
negative coefficient in it's
leading term.

This sketch shows both types
of polynomials
together.

To be co
ntinued......

1 comment:

  1. Nice diagrams, I hope the publisher can do them justice.

    ReplyDelete