Scientific Computing and Multimedia Applications

7 downloads 0 Views 1MB Size Report
The graphs of polynomials of order 1 (linear equations) gives a straight ... Gradient. The gradient of a straight line is a measure of its slope with respect to the x-axis. ... quadratic, either by factorisation or by using formulae ..... Set each factor = 0 and solve for x. ... Given a vector r of roots you can create a polynomial ,p, with.
CM2202: Scientific Computing and Multimedia Applications General Maths: 1. Polynomials Prof. David Marshall School of Computer Science & Informatics

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Definitions: General Form, Degree/Order of a Polynomial

The general form of a polynomial function is: f (x) = ax n + bx n−1 + cx n−2 + · · · + k Assuming a 6= 0 the f (x) is said to be a polynomial of degree (or order) n in the variable x.

2 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Example: Polynomial Degree/Order Examples of polynomials are:

f (x) = 3x 4 − 2x 3 + 5, Polynomial of degree (order) 4 f (x) = x 5 − 23 + x, Polynomial of degree 5 f (x) = x 2 − 4 Polynomial of degree 2 f (x) = 2x − 1 Polynomial of order 1. The order of a polynomial is the highest power of x in the function. 3 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Roots of a Polynomial: Graphs and Factorisation If we consider the equation f (x) = 0 (f (x) a polynomial of order n) Solving f (x) for x, this will have up to n roots. Polynomials can be represented as graphs or sketches. The roots of a polynomial are the points where the graph crosses the x-axis. In practice, we find these by writing the polynomial as an equation f (x) = 0, and factorising. 4 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Polynomial Order 1 Graph Example The graphs of polynomials of order 1 (linear equations) gives a straight line. e.g. f (x) = 2x − 1 = 0 180 160 140 120 100 80 60 40 20 0 0

10

20

30

40

50

60

70

80

90

100

5 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Polynomial Order 1 Root Example The root of f (x) = 2x − 1

Root

0

0.5

1

Clearly we can trivially solve this analytically, x = 0.5 6 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Polynomial Order 2 Graph Example The graph of polynomials of order 2 (quadratic equations): e.g. f (x) = x 2 − 4 f(x) = x2 − 4 20

15

10

5

0

−5 −5

−4

−3

−2

−1

0

1

2

3

4

5

with roots clearly at x = −2 and x = 2. This shape is also called a parabola 7 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Polynomial Order 3 Graph Example The graph of polynomials of order 3 gives a cubic curve. e.g. f (x) = x 3 − 2x + 1 f(x) = x3 − 2x +1 20

15

10

5

0

−5

−10

−15

−20 −3

−2

−1

0

1

2

3

(We’ll worry about finding its roots a little later) 8 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Polynomial Order 4 Graph Example The shape of the quartic curve f (x) = x 4 − 3x 3 + 2x 2 − 2x + 1 looks like this. f(x) = x4 −3x3 + 2x2 −2x +1

180 160 140 120 100 80 60 40 20 0 −3

−2

−1

0

1

2

3

(We’ll worry about finding its roots a little later) 9 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

The Equation of a Straight Line The equation of a straight line is given by: (f (x) =)y = mx + c where m represents the slope of the line (i.e. the gradient ) and c is the point where the line crosses the y -axis (i.e. the y -intercept ). The point where the line crosses the x-axis is called the x-intercept. 180 160 140 120 100 80 60 40 20 0 0

10

20

30

40

50

60

70

80

90

100

E.g.: Polynomial Order 1: f (x) = 2x − 1 above. 10 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Gradient The gradient of a straight line is a measure of its slope with respect to the x-axis. Gradient is defined as rate of inclination: the difference in y , dy divided by the difference in x, dx, between one point and another point on the line. A positive gradient indicates an ’uphill’ slope with respect to the positive direction of the c x-axis. A negative gradient indicates a ’downhill’ slope with respect to the positive direction of the x-axis.

y = mx + c

dy

y θ dx m = tan(θ) =

x

dy dx

11 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Calculating the Gradient The gradient of a line may be found from any two points on a line. In general the gradient, m of the line passing through A(x1 , y1 ) and B(x2 , y2 ) is : m=

B(x2 , y2 )

y2 − y1 dy = dx x2 − x1

m is also the tangent of the angle the line makes with the x-axis.

m = tan(θ) =

y = mx + c

dy

θ A(x1 , y1 )

dx m = tan(θ) =

dy dx

=

y2 −y1 x2 −x1

dy y2 − y1 = dx x2 − x1

12 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Quadratic equations A equation of the form ax 2 + bx + c = 0 is called a quadratic e.g. x 2 − 3x + 2 = 0 f(x) = f(x) = x2 − 3x + 2 45 40 35 30 25 20 15 10 5 0 −5 −5

−4

−3

−2

−1

0

1

2

3

4

5

Notice that the graph crosses the x-axis at two points x = 1 and x = 2. These values are the roots of the quadratic equation x 2 − 3x + 2 = 0 13 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Finding Roots of a Quadratic Equation The roots of a quadratic equation can also be found by solving the quadratic, either by factorisation or by using formulae Factorisation example: x 2 − 3x + 2 = 0 (x − 1)(x − 2) = 0 This means that either x − 1 = 0 or x − 2 = 0 Therefore the roots of this equation are x = 1 and x = 2

14 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

General quadratic equation solution by formula The roots of the general quadratic polynomial ax 2 + bx + c with a 6= 0 are given by the formula √ −b ± b 2 − 4ac x= 2a Note if a = 0 then the equation would not be a quadratic! So for x 2 − 3x + 2 = 0, a = 1, b = −3 and c = 2, and so b 2 − 4ac = −3 × −3 − 4 × 1 × 2 = 9 − 8 = 1 Therefore x

= =

√ −(−3) ± 1 3±1 = 2 2 4 2 or = 2 or 1 as above. 2 2 15 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Using the Discriminant to determine the nature of the roots of a quadratic Note: The value of the expression will determine the nature of the roots of the equation, and it is called the discriminant. If the discriminant is zero, i.e. b 2 − 4ac = 0 , the roots of the −b equation are x = −b 2a , 2a — Two equal roots. If the discriminant is positive i.e. b 2 − 4ac > 0 the roots are unequal √and √ 2 2 x = −b+ 2ab −4ac or x = −b− 2ab −4ac — Two distinct real roots. If the discriminant is negative i.e. b 2 − 4ac < 0 there are no real roots of the equation, i.e. the roots are complex (see later) 16 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

The 3 possible cases visualised f(x) = x2 − 4

f(x) = x2

25

f(x) = 3x2 + x + 2 80

20 70

20

15

60

50

15

10 40

10

30

5

20

5

0 10

0 −5

−4

−3

−2

−1

0

1

2

3

4

5

−5 −5

0 −5

−4

−3

−2

−1

0

1

2

3

4

−4

−3

−2

−1

0

1

2

3

4

5

5

(i) Two equal roots (ii) Two distinct roots (iii) No real roots The above diagrams of ax 2 + bx + c (a 6= 0) show the three possible cases: In (i) the curve touches the x-axis, i.e. for y = 0 there are two equal values of x. In (ii) the curve cuts the x-axis, i.e. for y = 0 there are two real distinct values of x. In (iii) the does not cut the x-axis, i.e. for y = 0 there is no real values of x. 17 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Exercise: Quadratic equations (1) Without solving the equation 4x 2 − 12x − 9 = 0 , determine the nature of its roots.

18 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Exercise: Quadratic Equations (2) Solve the following quadratic equation: 4x 2 − 8x − 9 = 0

19 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Exercise: Quadratic Equations(3) Given that the quadratic equation (k 2 − 1)x 2 − 2kx + 3k + 1 = 0 (in x) has equal roots, show that 3k 3 − 3k − 1 = 0.

20 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Curve Sketching

In this section we will look at How we sketch or plot polynomials and other functions. Properties of functions that may be observed from their graph. Special case functions — rational functions.

21 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Curve Sketching: Straight Lines (1) Graph of a straight line y = mx + c 180 160 140 120 100 80 60 40 20 0 0

10

20

30

40

50

60

70

80

90

100

c is the point at which the line cuts the y-axis m is the gradient Need only find 2 points to plot a straight line.

22 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Curve Sketching Example: Straight Lines (2) Sketch y = 2x − 6 From the equation (value of c) we know that the line will cross the y -axis at coordinates (0, −6) To find the second point, let’s find out where the line cuts the x-axis: Let y = 0 then solve 2x − 6

=

0

2x

=

6

x

= 3

Therefore the second point has coordinates (3, 0)

23 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Curve Sketching Example: Straight Lines (2) So we can plot: y = 2x − 6 5

0 (−c/m=3,0)

−5 (0,c=−6)

−10

−15

−20

−6

−4

−2

0 x

2

4

6

24 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Curve Sketching: Quadratic Equations (1) Graph of a quadratic y = ax 2 + bx + c: If a < 0 then we get hill or frown shape curve. If a > 0 then we get valley or smile shape curve. f(x) = −2x3 −1x2 +20

f(x) = 2x3 −1x2 −20

200 180

0

160 140 120

−50

100 80 −100

60 40 20

−150

0 −20 −200 −10

−8

−6

−4

−2

0

2

4

6

8

10

−10

−8

−6

−4

−2

0

2

4

6

8

10

Find where the curve cuts the x-axis by factorising (coming very shortly) (let y = 0). The curve cuts the y -axis at c (by setting x = 0) Later we will look at how you can investigate the nature of turning points and find their co-ordinates. (See Calculus: differentiation) 25 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Odd and Even functions Any function for which f (−x) = f (x) is called an even function. An example of an even function is

f(x) = x 2 : f(−3) = 9 = f (3). Any function for which f (−x) = −f (x) is called an odd function. An example of an odd function is

f(x) = x 3 : f(−3) = −27 = −f (3) In general, functions f (x) = x n have the property that f (−x) = f (x) for even values of n f (−x) = −f (x) for odd values of n. 26 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Exercise: Odd and Even functions

Show that the function y = f (x) = 4x 3 − x is an odd function.

27 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Visualising Odd and Even functions From the sketch of a graph we can notice the following: The graph of an even function will be symmetrical about the y -axis. The graph of an odd function will be unchanged under a 180◦ rotation about the origin. Examples: f(x) = x2

f(x) =4*x3 − x 250

100 200 150

80 100 50

60

0

40

−50 −100

20 −150 −200

0 −250 −4

−3

−2

−1

0

1

2

3

Odd Function

4

−10

−8

−6

−4

−2

0

2

4

6

8

10

Even Function 28 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Is the following Graph Odd or Even? sin(x)

Odd or Even? 29 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

And is this Graph Odd or Even? cos(x)

Odd or Even? 30 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Trigonometric Functions: sin(x) sin(x)

sin(x) is odd Clearly: sin(−x) = − sin(x) 31 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Trigonometric Functions: cos(x) cos(x)

cos(x) is even Clearly: cos(−x) = cos(x)

Note : These results will be important later (see Complex Numbers and Fourier Theory) 32 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Odd or Even:

Are the following functions odd or even: f (x) = − sin(x)? f (x) = − cos(x)? f (x) = sin(x)2 ? f (x) = cos(x)2 ? f (x) = cos(x) + sin(x)?

33 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Rational Functions Not all functions are polynomials or even continuous. Rational Functions: A function which can be expressed as D(x) are both polynomials.

N(x) D(x)

where N(x) and

For example: f (x) =

(x + 1) (x − 1)

34 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Example: Graph of f (x) =

Rational Functions

MATLAB

(x+1) (x−1) (x + 1)/(x − 1)

4

3

2

1

0

−1

−2

−6

−4

−2

0 x

2

4

6

Notice this graph tends to ∞ at x =. Such a line is called an Asymptote A straight line towards which a curve approaches but never meets 35 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Curve Sketching Revisited (for Rational Functions) The five basic investigations for curve sketching are 1

Symmetry (or even function) (if obvious) does f (x) = f (−x)?

2

Intersection with axes, does the curve cross the x-axis or y -axis?

3

Behaviour of f (x) as x → ∞ and as x → ±0

4

Where is f (x) undefined? Where are the poles? where the denominator is zero and the function shoots off to infinity (∞)

5

Where are the maximum and minimum points (this will be covered later when we look at Calculus: differentiation)

36 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Example: Rational Function Curve Sketching Sketch: f (x) =

2x + 1 (x − 2)(x + 1)(x − 3)

To sketch a graph like this we Factorise the numerator and denominator.

We have pre-factorised f (x) in this example. A little more on factorisation to follow soon. And then use the basic investigations: Is the function even (is the graph symmetrical)? Where does f (x) intersect with the axes What happens to the function when x becomes very large ±, i.e. x → ±∞ ? What happens to the function when x gets close to zero (from ±)? Where are the poles — the points where the denominator is zero? 37 / 70

Poly. Order

Roots

Line

Quadratics

Example: f (x) =

Skecthing

Odd/Even

Rational Functions

MATLAB

2x+1 (x−2)(x+1)(x−3)

In this example we can see that: It has poles (goes to infinity) when x = −1, x = 2 and x = 3: The places where the denominator equals zero Easily obtained from the factors of the denominator Set each factor = 0 and solve for x.

These are examples of vertical asymptotes. (2 x + 1)/((x + 1) (x − 2) (x − 3)) 2.5

2

1.5

1

0.5

0

−0.5

−1

−4

−2

0

2

4

6

x

38 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Exercise: Rational Functions Consider the function: f (x) =

x (1 − x)

Where is the pole? Where does it intersect with the x and y axes? What happens for ± large values of x, x → ±∞? Roughly sketch the graph of f (x).

39 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Polynomials/Equations and Mathematical Functions in MATLAB MATLAB provides some excellent and easy to use tools to help us to Represent and manipulate polynomials More generally, represent and manipulate almost any mathematical expressions or equations Easy way to plot graphs of mathematical equations/expressions Easy way to factorise mathematical expressions Easy way to solve equations More advanced maths - see later 40 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Polynomials in MATLAB MATLAB provides some basic functions for declaring and manipulating polynomials. This is part of basic MATLAB — not an additional toolbox Some (not all) MATLAB functions: poly(r), Create/return a polynomial whose roots are r. r is a vector whose elements are the coefficients of the polynomial whose roots are the elements of r .

roots(p), find the roots of a polynomial, p. polyval(p,x) returns the value of a polynomial p evaluated at x. x can be a single value or a MATLAB vector of several values

For full information see MATLAB documentation: Functions → Mathematics → Polynomials and help poly etc. 41 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

MATLAB Polynomial Representation and Creation

In MATLAB: A polynomial variable p is a vector of length n + 1 whose elements are the coefficients in descending powers of the polynomial to be evaluated: f (x) = p1 x n + p2 x n−1 + . . . + pn x + pn+1

There are two ways to create p : 1

via its roots

2

via its coefficients

42 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

MATLAB Polynomial Creation: poly() Given a vector r of roots you can create a polynomial ,p, with poly(r): poly create eg.m >> r = [-1,1]; % Define roots >> p = poly(r) % Create a polynomial with roots p = 1 0 -1 >> r1 = roots(p) % Solve the polynomial r1 = -1 1

Here we have a created a polynomial f (x) = x 2 − 1 (the x coefficient element is zero in the vector, p). The function roots(p) returns the roots of a given polynomial, p and solved it. Clearly, poly() and roots() perform opposite roles. 43 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

MATLAB Creating Polynomials: Declaring a poly and evaluating it, polyval() Now we have seen the MATLAB polynomial structure we can simply declare it as a MATLAB vector; p = [1, 0, -1]; % a Polynomial f (x) = x 2 − 1 Evaluating a polynomial,polyval() Given a polynomial p and a point or vector of points, x, the function, polyval(p, x) evaluates them: >> p = [1, 0, -1]; % A Polynomial f (x) = x 2 − 1 >> x = 1; % A data point x >> y = polyval(p,x) % Evaluate p at data point x y = 0 44 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

MATLAB Creating Polynomials: polyval() multiple values

In MATLAB it is easy to create a range of values, x and use these with polyval(): >> p = [1, 0, -1]; % a Polynomial f (x) = x 2 − 1 >> x = 1:3 % A range of points x = 1 2 3 >> y = polyval(p,x) % Evaluate p at data points x y = 0 3 8

45 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

MATLAB: Plotting Polynomial Graphs (1)

The MATLAB plot() function1 plot creates linear x-y plots; If x and y are vectors of the same length, the command: plot(x,y) Opens a MATLAB figure (graphics window) Draws an x-y plot of the elements of x versus the elements of y .

Stay tuned for a lot more details on plotting in MATLAB in Dr. Lai’s Lectures 1

46 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

MATLAB: Plotting Polynomial Graphs (2) Given a set of values x we can evaluate a given polynomial, p using polyval(): poly1.m p = [2, -1]; % Create a polynomial f (x) = 2x − 1 x= -5:100; % Create some x values to evaluate f(x) y = polyval(p,x); % evaluate polynomial plot(x,y); % plot x against y axis tight ; % force concise display of plot roots p = roots(p) % Solve the Equation

47 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

MATLAB: Plotting Polynomial Graphs (3) Running poly1.m we get the following output and figure:

180

poly1.m Output

160 140

>> poly1

120 100 80

roots p =

60 40

0.5000

20 0 0

10

20

30

40

50

60

70

80

90

100

which is the solution we expect from f (x) = 2x − 1 = 0.

48 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

More Polynomial Examples: poly2.m poly2.m f(x) = x2 − 4

% Create a polynomial % f ( x ) = x ˆ2 −4 p1 = [ 1 , 0 , −4]; % C r e a t e some v a l u e s o f x xmin = −5.0; xmax = 5 . 0 ; xstep = 0.2; % Make x d a t a p t s x=xmin : x s t e p : xmax ; % Evaluate polynomial y = p o l y v a l ( p1 , x ) ;

20

15

10

5

0

−5 −5

−4

−3

−2

−1

0

1

2

3

4

5

Note: poly2.m contains some more code to make plot prettier.

plot (x , y ); % plot i t 49 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

More Polynomial Examples: poly2 1.m, poly2 2.m, poly2 3.m Some similar examples (different polynomials - Quadratics): poly2 1.m — f (x) = x 2 − 3x + 2

f(x) = f(x) = x2 − 3x + 2 45 40

p = [ 1 , −3, 2 ] ;

35 30

poly2 2.m — f (x) = 3x 2 + x + 2

25 20 15

p = [3 , 1 , 2];

10 5

poly2 3.m — f (x) = x

2

0 −5 −5

p = [1 , 0 , 0];

−4

−3

−2

−1

0

1

2

3

4

5

Output of poly2 1.m

Note: We have seen examples of all these plots in previous slides. 50 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

More Polynomial Examples: poly3.m, poly4.m Some further examples (different polynomials - Cubic and Quartic): f(x) = x3 − 2x +1 20

poly3.m — f (x) = x 3 − 2x + 1

15

10

p = [ 1 , 0 , −2, 1 ] ;

5

0

poly4.m — f (x) = x 4 − 3x 3 + 2x 2 − 2x + 1 p = [ 1 , −3, 2 , −2, 1 ] ;

−5

−10

−15

−20 −3

−2

−1

0

1

2

3

Output of poly3.m

Note: We have seen examples of all these plots in previous slides. 51 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

A Convenient Polynomial Plotting Function: poly plot() poly plot() — poly plot.m [ r o o t s p ] = p o l y p l o t ( p , xmin , xmax , x s t e p , y o f f s e t )

Let’s combine all the handy plotting facilities seen in last examples into a convenient function: Download this function into your MATLAB workspace and type help poly plot or view in text editor. The complete inner workings of this function are not so important for now.

52 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

A Convenient Polynomial Plotting Function: poly plot() Usage poly plot() Usage: type help poly plot >> h e l p p o l y p l o t p o l y p l o t ( p , xmin , xmax , x s t e p , y o f f s e t ) Function to take i n a a r b i t r a r y polynomial , p and p l o t i t o v e r a g i v e n r a n g e and p l o t a l l i t s REAL r o o t s . INPUTS : p − v e c t o r o f p o l y n o m i a l c o e f f i c i e n t s . See man p o l y f o r d e t a i l s xmin − minimum v a l u e o f x r a n g e xmax − maximum v a l u e o f x r a n g e xstep − increment step s i z e for x y o f f s e t − o f f s e t from y v a l u e s f o r plot prettiness . Output : r o o t s p − r o o t s o f t h e p o l y n o m i a l 53 / 70

Poly. Order

Roots

Line

Quadratics

poly plot eg.m:

Skecthing

Odd/Even

Rational Functions

MATLAB

Calling poly plot() example

poly plot eg.m % Create a polynomial % f ( x ) = 4 x ˆ3 − x p = [4 ,0 , −1 ,0] % C r e a t e some x v a l u e s xmin = −4.0; xmax = 4 . 0 ; xstep = 0.1; % E x t e n d a x e s beyond r a n g e o f y f o r p l o t yoffset = 10.0; % C r e a t e some x v a l u e s x=xmin : x s t e p : xmax ; % S o l v e and P l o t p o l y n o m i a l p r o o t s p = p o l y p l o t ( p , xmin , xmax , x s t e p , y o f f s e t ) 54 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

poly plot eg.m output f(x) =4*x3 − x 250

poly plot eg.m Output

200 150

>> p o l y p l o t e g

100 50 0

ans =

−50 −100

0 0.5000 −0.5000

−150 −200 −250 −4

−3

−2

−1

0

1

2

3

4

Output of poly plot eg.m

55 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

MATLAB Polynomials/Rational Functions: Symbolic Computation MATLAB allows you to do direct algebra via symbolic computation Create algebraic variables and functions Manipulate these algebraically or symbolically Easily solve equations Easily plot graphs A lot more complex operations possible — but not studied here. Other commercial packages also allow similar facilities: e.g. Mathematica and Maple 56 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

MATLAB Symbolic Math Toolbox

MATLAB provides this amazing computation potential via its Symbolic Math Toolbox Additional (add-on) Toolbox Not part of standard MATLAB

Check that your MATLAB licence supports this. Available on School MATLAB Licence.

For full details see MATLAB documentation: Symbolic Math Toolbox and doc sym, help sym or help syms etc.

57 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Creating a Symbolic Variable You have to create a special symbolic variable in MATLAB. The easiest way is via the syms command: syms x ;

Having created a symbolic variable you can create symbolic expressions, e.g: f = x ˆ2 −2∗x +1;

See doc syms, or help syms etc. for more details. (Also help sym) 58 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Plotting a Symbolic Expression This could not be simpler via ezplot(): f(x) =− 2*x2 − x + 20

30

20

10

ezplot ( f )

0

−10

−20

−4

−3

−2

−1

0

1

2

3

4

You can use ezplot() with all other MATLAB Graphics functions Such as plot(), axis control and labelling. 59 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

line plot symbolic eg.m

line plot symbolic eg.m % S i m p l e Example S c r i p t t o p l o t a s t r a i g h t syms x ; m=2;

l i n e u s i n g Symbolic Toolbox

c= −6;

y = m∗x +c ; ezplot (y );

% P l o t X and Y a x e s and l a b e l t i t l e ( ’ y = 2x − 6 ’) h o l d on p l o t ([ −6 6 ] , [ 0 0] , ’ − k ’ ) ; % X A x i s p l o t ( [ 0 0] ,[ −20 6] , ’ − k ’ ) ; % Y A x i s axis tight

60 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

line plot symbolic eg.m cont.

line plot symbolic eg.m % Plot c y axis i n t e r c e p t p l o t (0 , c , ’ o ’ , ’ LineWidth ’ , 2 , . . . ’ MarkerEdgeColor ’ , ’ k ’ , . . . ’ MarkerFaceColor ’ , ’ r ’ , . . . ’ MarkerSize ’ , 1 0 ) ; % P l o t −c /m x a x i s i n t e r c e p t p l o t (−c /m, 0 , ’ o ’ , ’ L i n e W i d t h ’ , 2 , . . . ’ MarkerEdgeColor ’ , ’ k ’ , . . . ’ MarkerFaceColor ’ , ’ b ’ , . . . ’ MarkerSize ’ , 1 0 ) ; % label intercepts offset = 0.5; t e x t (0+ o f f s e t , c , ’ ( 0 , c = −6) ’); t e x t (−c /m,0−2∗ o f f s e t , ’(− c /m= 3 , 0 ) ’ ) ;

61 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

line plot symbolic eg.m Output y = 2x − 6 5

0 (−c/m=3,0)

−5 (0,c=−6)

−10

−15

−20

−6

−4

−2

0 x

2

4

6

62 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Evaluating a Symbolic Expression: subs() Use the subs() command to evaluate a function: syms x ; >> f = x + 1 ; >> s u b s ( f , 1 ) ans =

2

>> s u b s ( f , 1 : 3 ) ans =

2

3

4

63 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Substitution via subs() subs() can do much more, it can substitute variable and expressions: >> >> >> >>

syms x ; syms y ; f = x + 1; subs ( f , x , y )

ans =

y + 1

>> g = y + 1 ; >> s u b s ( f , x , g ) ans =

y + 2 64 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Let’s do some algebra

You can manipulate expressions via subs() but you can also do things directly: >> g = x ; >> g2 = 2∗ x + 1 ; >> g3 = g + g2 ans = 3∗ x + 1

65 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Simplifying, Factorising, Expanding Expressions There are MATLAB functions to achieve these: >> syms x ; >> f = x ˆ2 −2∗x +1; >> f 1 = s i m p l i f y ( f ) ans = ( x − 1)ˆ2 >> f 2 = f a c t o r ( f ) f2 = ( x − 1)ˆ2 >> expand ( f 2 ) a n s = x ˆ2 − 2∗ x + 1

simplify() does more than just factorise: >> s i m p l i f y ( s i n ( x ) ˆ 2 + c o s ( x ) ˆ 2 ) ans = 1 66 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Solving Equations: solve()

Simple using solve(): >> syms x ; >> f = x ˆ2 −2∗x +1; >> r o o t s f = s o l v e ( f ) roots f = 1 1

67 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Symbolic Math Rational Expressions You can define rational and trigonometric (as well as other) expressions: (2 x + 1)/((x + 1) (x − 2) (x − 3))

symbolic eg.m

2.5

2

f 1 = ( x +1)/( x −1);

1.5

1

% Another f 2 = ( 2 ∗ x +1)/(( x −2)∗( x +1)∗( x − 3 ) ) ; ezplot ( f2 )

0.5

0

−0.5

−1

% Trigonometric f3 = s i n ( x )ˆ2 ;

−4

−2

0

2

4

6

x

ezplot(f2) See symbolic eg.m for examples of all Symbolic methods discussed here. 68 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Converting from Polynomial to Symbolic Representations MATLAB provides convenience functions to convert between both types of polynomial representations. poly2sym(): >> syms x ; >> p = [ 1 , −2, 1 ] ; % C o n v e r t from P o l y p t o s y m b o l i c >> f x = p o l y 2 s y m ( p ) fx =

x ˆ2 − 2∗ x + 1

% S p e c i f y to Symbolic V a r i a b l e >> f t = p o l y 2 s y m ( p , ’ t ’ ) f t = t ˆ2 − 2∗ t + 1

69 / 70

Poly. Order

Roots

Line

Quadratics

Skecthing

Odd/Even

Rational Functions

MATLAB

Converting from Symbolic to Polynomial Representations As expected, sym2poly(), converts from Symbolic to Polynomial: Creates a vector with the coefficients of given symbolic expression. >> syms x ; >> f = 3∗ x ˆ3 + 2∗ x ˆ2 + x ; >> p = s y m 2 p o l y ( f ) p = 3

2

1

0

70 / 70