Top Banner
Formal Computational Skills Week 1: Functions
31

Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

Dec 29, 2015

Download

Documents

Edward Cummings
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

Formal Computational Skills

Week 1: Functions

Page 2: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

Overview

By the end you should:• Be able to evaluate a function• Know how to visualise a function• Know what e2 is• Know what a linear equation is• Be able to read summation and product notation

These are basics needed in later lectures

Will also do a little bit of logarithms and powers to re-familiarise you with maths and mathematical thinking

Page 3: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

Functions of a single variableUsually: y = f(x) or: y(x) = f(x)

Read as: “y is a function of x”

Sometimes referred to as a mapping or transformation: f: x -> yRead as: “f takes/maps/transforms x into y”

x is the input/argument/parameter/independent variable

f takes x as an input and transforms it into the output y and is therefore known as the dependent variable)

Implicitly assumed that y depends only on x (and constant values eg 2, speed of light etc)

Page 4: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

Examplesy = sin(x)y = cos(x)

xy

xey x

log

)exp(

Last eg seems different as it is y = sum of functions of x.

However all the above (sin, cos, log, e) are actually shorthand for (infinitely) long sums

A lot of learning maths is learning this shorthand

else

xy

,1

3,0

Means “identically equal to” ie defined to be the same

y = 3x + 2

eg the function “if x is less than 3, y = 0, otherwise y = 1” is written:

Page 5: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

Evaluating FunctionsFunctions must be evaluated for values of x

y(x) means y evaluated at x so y(3) means y evaluated at x=3.

Eg to evaluate y(x) = x2 + 3x + sin(x) at x=3 put 3 instead of x in the equation:

y(3) = 32 + 3(3) + sin(3) = 9 + 9 + 0.05 = 18.05

In many cases, functions cannot be evaluated and must be approximated eg by truncation of an infinite sum.

NB most computer evaluated functions are like this and to save time often use eg look-up tables which give y for values of x

Page 6: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

Bracket Ambiguities

Brackets can be ambiguous.

Sometimes they mean multiply eg y(y+1) means y times (y+1)

Sometimes indicate dependence of 1 variable on another eg y(x) (normally) means that y takes x as a (input) variable

Can even be used in the same expression eg:

y(x) = x(x + a) means y is a function of x and is equal to x times (x+a). However, could read as y times x equals x times (x+a)

Knowing which is largely a matter of experience as it depends on the context. Normally do whatever makes ‘sense’

Page 7: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

Visualising FunctionsFunctions are visualised by plotting them as graphs where you can see the value of the output for every input

0 0.5 1 1.5 2 2.5 3 3.50.2

0.4

0.6

0.8

1

1.2

1.4

1.6

1.8

2

2.2

x

y

By convention, independent variable along the horizontal (x-axis) and dependent along the vertical (y-axis)

Plot of y = sin(4x) + 1.2

Page 8: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

Plotting graphsClearly cannot evaluate y for the infinitely many values of x so instead, idea is to evaluate y for a number of values of x:

For a smoother plot use more x (and y) values

Simple as we do this by computer (eg matlab)

x = 0 0.1 0.2 0.3

y=sin(4x)+1 sin(0)+1=1 sin(.4)+1=1.3 sin(.8)+1=1.6 sin(1.2)+1=1.8

and interpolate between them (here with straight lines)

Plot each of the (x,y) pairs

x (0, 1)

x (0.1, 1.3)

x x

Page 9: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

Not just for visualisation: graphs can aid analysis since the line is the function and the function is the line

Thus points satisfying y = 10x are ALL the points on this line and ONLY points on this line

Why?

y has been specified for EVERY x

0 1 2 3 4 50

10

20

30

40

50

x

y

Eg suppose we are asked to solve y = 10x and y = ex ie we want points [(x,y) pairs] that satisfy both equations

“satisfy” means “to make the equations true” so eg (x,y)=(1,10) or (2,20) satisfy y=10x

Page 10: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

Similarly, all the points where y = ex are on this line

points where the lines cross

0 1 2 3 4 50

50

100

150

x

y

0 1 2 3 4 50

50

100

150

x

y

So if we draw both lines on the same graph:

Are on both lines, and thus satisfy both equations

In this way we graphically estimate the solution to the equations

Page 11: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

This relates to question 1

Page 12: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

Functions of several variablesFunctions can have many dependent variables and are written:

z = f(x,y) z = x exp(-x2 + -y2)or (more often):

y = f(x1, x2, …, xn)

Which reads as “y is a function of n variables x1, x2, …, xn”

Implicitly assumed that the variables are of the same type. Thus, in neural networks see:

y = f(x1, …, xn, w1, …, wm)

Which reads (implicitly) as: “the output, y, is a function of n x-type variables (eg inputs) and m w-type variables (weights)”

Page 13: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

In such cases, functions are often visualised as surfaces or landscapes eg

-2-1

01

2

-2

-1

0

1

2

-0.4

-0.2

0

0.2

0.4

The analogy only really holds for functions of 2 variables, but often used for higher dimensions (possibly misleadingly…)

Plot ofz = x exp(-x2 + -y2)

Page 14: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

0 1 2 3 4 5-5

-4

-3

-2

-1

0

x

y

0 1 2 3 4 50

5

10

15

20

x

y

Equation of a straight lineThe equation of a straight line is: y = mx + c Where m and c are constants. m is the gradient or slope, c is the intercept.

y = 4x, m=4

m specifies how much y changes if we change x by 1.

y = 1x =x, m=1

y = 2x, m=2

y = 0x = 0, m=0

y = -x, m=-1

If m = 0, y = constant, If m<0 y decreases as x increases.

Page 15: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

c specifies where the line crosses (intercepts) the y-axis

0 1 2 3 4 5-5

0

5

10

15

x

yy = 2x + 2

y = 2xy = 2x - 3

If y= mx +c say equations are linear

ONLY linear equations specify straight lines.

c=- 3

c=0c=2

Thus find c by evaluating y at x=0

Page 16: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

Say an equation is linear if the highest power of the dependent variables is 1 (or less)

Eg y = 2x1+4x2+1, or y = 6x1+x3-4x8 but not y = 6x1+x32

Linear Equations

Equation is linear in x but non-linear in w

If an equation is not linear it is said to be non-linear and it will be a curve.

What about: y= w12 x1+w2x2 ?

Linear equations of more variables specify planes or hyperplanes

Page 17: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

Linear equations are important because they are mathematically simple

This makes them easy to work with and to solve eg what is x when y = 3?

In particular, they are predictable as everything stays the same. Thus, if we experiment locally to find properties we know what is happening globally

Linear is easy: x=6 But for non-linear can be many solutions: more complex

Page 18: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

Summation and Product Notation

Simply shorthand (again) for lazy mathematicians eg:

Read as: “the sum for i = 1 to 4 of aixi”. Can think of it as a ‘for’ loop:

sum = 0; for i = 1 to 5 do

sum += a[i] x[i]; end;

Often see

In which case it is implicitly assumed that the sum is over ALL i

ori

iixa iixa

44332211

4

1

xaxaxaxaxayi

ii

Page 19: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

323222121

313212111

xaxaxa

xaxaxa

Double summation

helps to write out some terms:

321

3

1

ppppi

i

2

1332211

2

1

3

1

)(i

iiii j

jij xaxaxaxa

Product notation: same as summation but with multiplication eg

Page 20: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

This relates to questions 2-4

Page 21: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

PolynomialsPolynomials are functions like:

y = 2 + x + 3x2 – 1.3x4

So linear equations are polynomials

33

2210

0

xaxaxaaxayi

ii

In general they are functions which can be written as

111 xaxa 0

000 1 xaaa andRemember

EVERY FUNCTION IS A POLYNOMIAL

Page 22: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

Order of a Polynomial

The order of the polynomial is the highest power of x that appears in it (Often most of the ai’s are 0).

Eg: y = 2 + x + 3x2 – 1.3x4 is 4th orderlinear equations: z = 2x +3y – 6 are 1st order

The higher the order the more complex the function can be

- ie the more complicated it is to explain to somebody- ie the more information you would need to tell them about it

– directly related to the number of ai’s you would have to tell someone so they could recreate the function exactly(sort of) Information Theory

Page 23: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

1st order: linear

0 1 2 3 4 50

2

4

6

8

10

x

y

0 1 2 3 4 51

1.5

2

2.5

3

x

y

0th order: constant 2nd order: quadratic

0 1 2 3 4 50

5

10

15

20

25

x

y

-10 -5 0 5 10-50

0

50

100

150

200

250

x

y

When approximating or generalising, we ignore higher order terms as they often specify noise (NB if |x|<1, xi gets small for high i)

high order

-10 -5 0 5 10-50

0

50

100

150

200

250

x

y

Page 24: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

PowersMultiplication:

22 23 = (2 x 2) (2 x 2 x 2) = 2 x 2 x 2 x 2 x 2 = 25

Similarly: y2 y3 = (y x y) (y x y x y) = y5

And:ba

baba

ba xxxxxxxxxxxx

Division:

ba

ba

b

a

b

a

xxxxxxx

xxx

x

x

2222

222

2

2 12

3

And:

Page 25: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

2 special cases:

ab

bab

aaaba xxxxxxxx )(

Powers of powers:

01 xxx

x aaa

a

Special case: x1/2

ie anything to the power of 0 is 1

aaaa

xxx

x

x 0

01 ie anything to a negative power is 1 over it to a positive power

xxxxx 2/12/1222/1 )(

Similarly: aa xx /1

So x0.5 is ok but what about x2.12 or even x ??? Actually something more complicated going on but won’t affect us

Page 26: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

Exponential equationsExponential equations are equations like: y = 2kx where k is a constant.

If k>0 say y is exponentially increasing eg bacteria doubling: 1, 2, 4, 8, … Numbers get very big very quickly

-5 0 50

5

10

15

20

25

30

35

2x

-5 0 50

5

10

15

20

25

30

35

2x

3x

Often see ex. e is simply a number, 2.7183 … to be precise (-ish)

Why is it important? Because if you differentiate ex, it stays exactly the same. Means that it is easy to work with

-5 0 50

5

10

15

20

25

30

35

ex

Page 27: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

If k<0 say y is exponentially decreasing

eg radioactive decay. 1, ½, ¼, …

Numbers decrease but approach 0 asymptotically (ie by smaller and smaller amounts and never quite reaching it)

What about k=0??

-5 0 50

1

2

3

4

5

e-0.5x

e-0.25x

The bigger the size (magnitude) of k (written as |k|), the quicker the increase or decrease

Page 28: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

LogarithmsLogs are basically the opposite of powers and are defined as:

bxxa a

b log

Means implies and is implied by or analogously if and only if

So: log10100 = 2 since 102 = 100

However, the only bases (in logb the base is b) used are 10 and e (and sometimes 2). All others can be got simply from these

log10 is almost always shorthanded to log and loge is shorthened to ln (as it is the natural logarithm).

The (important) exceptions are most programming languages where log(x) normally returns ln(x)

Page 29: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

Some rules of logs (won’t prove them but you can if you like):

1. By definition logs undo powers and vice versa so:

ln(ecos(x)) = cos(x)

Same way as multiplication undoes division (notion of function inverses)

2. log(xy) = log(x) + log(y) eg if:

3

1

3

1

loglog)log(i

ii

i ppy

321

3

1

ppppyi

i

Used in probability as easier to work with a sum than a product

Page 30: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

-5 0 50

1

2

3

4x 10

7

Why are they used? How long to get 768 bacteria if populatioon is doubling every second? Must solve:

n = 2t = 768 ??? Do log2768 = n = 9.59 s

-5 0 5

-10

0

10

Also if you plot the number of bacteria (n) from an experiment against time, numbers get very big very quickly: hard to plot the graph and hard to calculate errors.

But if you plot log(n) against t, if the relationship is exponential, graph is a straight line and the gradient determines k

Page 31: Formal Computational Skills Week 1: Functions. Overview By the end you should: Be able to evaluate a function Know how to visualise a function Know what.

This relates to questions 5-7