68
First, some left-overs… Lazy languages Nesting 1

First, some left-overs… Lazy languages Nesting 1

Embed Size (px)

Citation preview

Page 2: First, some left-overs… Lazy languages Nesting 1

Lazy Languages

Lazy Languages (“Short-circuit evaluation”)

Conditions on IF statements and WHILE loops are only evaluated as far as is needed. For example:

y = 3;if 4<y && x>9

4<y is False, so x<9 is not evaluated

Advantages:

1. Performance improvement

2. Avoid run-time errors (“exceptions”)2

Page 3: First, some left-overs… Lazy languages Nesting 1

Lazy Languages

Avoiding run-time errors (“exceptions”)

if x~=0 && y/x>5

Without lazy languages, the right-hand Boolean expression could result in a “Divide by 0” error. Because the left-hand expression is evaluated, if x is 0, the && is False and there is no need to evaluate the right-hand expression.

(Obviously this could be re-written – this is just to provide an example for discussion)

3

Page 4: First, some left-overs… Lazy languages Nesting 1

Lazy Languages

Avoiding run-time errors (“exceptions”)

if exist(filename) && fh=fopen(filename)

Without lazy languages, the right-hand Boolean expression could result in an error. Because the left-hand expression is evaluated, if the file does not exist there is no need to evaluate the right-hand expression and so there will not be an error from opening a non-existent file.

4

Page 5: First, some left-overs… Lazy languages Nesting 1

Nesting

“Nesting” means to embed a flow control construct within another flow control construct.

“Flow control constructs”: IF, SWITCH, WHILE, FOR

“The IF is nested within a WHILE loop”

“We use nested loops to traverse the matrix.”

Any flow control construct can be nested in any other flow control construct – and as deep as you need.

5

Page 8: First, some left-overs… Lazy languages Nesting 1

Numerical MethodsNumerical methods are techniques used to find quantitative solutions

to mathematical problems.

Many numerical methods are iterative solutions – this means that the technique is applied over and over, gradually getting closer (i.e. converging) on the final answer.

Iterative solutions are complete when the answer from the current iteration is not significantly different from the answer of the previous iteration.

“Significantly different” is determined by the program or user – for example, it might mean that there is less than 1% change or it might mean less than 0.00001% change.

8

Page 9: First, some left-overs… Lazy languages Nesting 1

Root Finding

If we can find where f(x) = 0, then we can find where f(x) equals any value.

To find where f(x) = -2, find the roots of f(x)+2

Essentially, we can shift any function up or down and solve for the roots, so all value finding problems can essentially be adjusted to be root finding problems.

We can turn any equation into a root finding problem.

9

Page 14: First, some left-overs… Lazy languages Nesting 1

Bisection Method The Bisection Method is a very common technique for

solving mathematical problems numerically.

Most people have done this even if they didn’t know the name.

The technique involves taking a guess for the desired variable, then applying that value to see how close the result is to a known value.

Based upon the result of the previous step, a better “guess” is made and the test repeated.

14

Page 15: First, some left-overs… Lazy languages Nesting 1

Bisection Method

Typical human method: Find the square root of 127.3 First guess: 11 (since we know that 112 is 121) 11*11 = 121 too low (looking for 127.3) Adjust: 12 is the new guess 12*12 = 144 too high Adjust: 11.5 (“bisect” the difference) 11.5*11.5 = 132.25 too high Adjust: 11.25 11.25 * 11.25 = 126.5625 too low etc.

15

Page 16: First, some left-overs… Lazy languages Nesting 1

Bisection MethodLet’s use the programmer’s method – an algorithm for

finding roots:

Define x1 so that f(x1) is greater than 0.

Define x2 so that f(x2) is less than 0.

Evaluate f((x1+x2)/2)

If it is positive, set x1 to (x1+x2)/2

If is it negative, set x2 to (x1+x2)/2

Repeat until you’re happy with either x1-x2 or change in f()

16

Page 17: First, some left-overs… Lazy languages Nesting 1

Bisection Method

For the Colebrook Equation, we know that this equation must hold:

Re-arrange so that you have 0 on one side, and you have a roots problem. Guess for f and if the equation is

not satisfied, adjust f and try again.

This is the same as solving for 127.3 = X2 . We made it X2 -127.3 = 0 and found the roots.

17

fRD

e

f N

5 1.2

7.3lo g0.2

1

Page 19: First, some left-overs… Lazy languages Nesting 1

Newton’s Method

X is the root we’re trying to find – the value where the function becomes 0

Xn is the initial guess (hopefully, a reasonably-close guess!)

Xn+1 is the next approximation using the tangent line

Credit to: http://en.wikipedia.org/wiki/Newton’s_method19

Page 30: First, some left-overs… Lazy languages Nesting 1

Ex 1: algorithm, skeleton

% Provide an initial guess for the x-intercept

% Repeat until close enough: compare most recent x-intercept to previous x-intercept

% Find the slope of the tangent line at the point (requires derivative)

% Using the slope formula, find the x intercept of the tangent line

30

Page 31: First, some left-overs… Lazy languages Nesting 1

Newton’s Method% Define initial difference as very large (force loop to start)

% Repeat as long as change in x is large enough

% Make the “new x” act as the current x

% Find slope of tangent line using f’(x)

% Compute y-value

% Use tangent slope to find the next value: % two points on line are the current point (x, y) % and the x-intercept (x_new, 0)

31

Page 32: First, some left-overs… Lazy languages Nesting 1

% Define initial difference as very large % (force loop to start)x_n = 1000;x_np1 = 5; % initial guess

% Repeat as long as change in x is large enoughwhile (abs(x_np1 – x_n) > 0.0000001) % x_np1 now becomes the new guess x_n = x_np1;

% Find slope of tangent line f'(x) at x_n m = 3*x_n^2 + 4*x_n + 300/(x_n^2);

% Compute y-valuey = x_n^3 + 2*x_n^2 - 300/x_n;

% Use tangent slope to find the next value: % two points on line: the current point (x_n, y_n) % and the x-intercept (x_np1, 0) x_np1 = ((0 - y) / m) + x_n; end

TOO FAR XnXn+1

? XnXn+1

32

Page 33: First, some left-overs… Lazy languages Nesting 1

Newton’s Method

Add fprintf’s and see the results:

x y------------------- ------------------- 5.000000000000000 115.000000000000000 3.925233644859813 14.864224007996924 3.742613907949879 0.279824373263295 3.739045154311932 0.000095471294827 3.739043935883257 0.000000000011084

Root of f(x) = x3 + 2x2 – 300/x is approximately 3.7390439 33

Page 34: First, some left-overs… Lazy languages Nesting 1

Real-World

Numerical Methods are used in CFD, where convergence can take weeks/months to complete (get “close enough”)...

while loops are generally used, since the number of iteration depends on how close the result should be, i.e. the precision.

Practice! Code this, and usea scientific calculator to see how close the results are.

34

Page 35: First, some left-overs… Lazy languages Nesting 1

Cons

Cons Divide by 0 Error

What happens if x is a maximum or minimum – the slope of the tangent line is 0.

Divergence What if our tangent line

leads us away from our root.

Cycling f(x)=x3-x-3 f’(x)=3x2-1 x0=0

35

xi xi+1

0.0000 -3.0000

-3.0000 -1.9615

-1.9615 -1.1472

-1.1472 -0.0066

-0.0066 -3.0004

-3.0004 -1.9618

-1.9618 -1.1474

-1.1474 -0.0073

-0.0073 -3.0005

-3.0005 -1.9619

Page 37: First, some left-overs… Lazy languages Nesting 1

Derivatives, intro.

Another numerical method involves finding the value of the derivative at a point on a curve.

Basically, this method uses secant lines as approximations to the tangent line.

This method is used when the actual symbolic derivative is hard to find, the formula is simply not needed, or the function is prone to change as the main program does its job.

37

Page 47: First, some left-overs… Lazy languages Nesting 1

Derivatives - Algorithm% Define initial difference as very large % (force loop to start)

% Specify starting range % Repeat as long as slope has changed “a lot”

% Cut range in half

% Compute the new low x value

% Compute the new high x value

% Compute the new low y value

% Compute the new high y value

% Compute the slope of the secant line % end of loop

Let’s do this for f(x) = sin(x) + 3/sqrt(x)

47

Page 48: First, some left-overs… Lazy languages Nesting 1

Code% Define initial difference as very large % (force loop to start)m = 1; oldm = 2;

% Specify starting range and desired pointrange = 10; x=5; ctr=0;

% Repeat as long as slope has changed a lot while (ctr<3 || (abs((m-oldm)/oldm) > 0.00000001))

% Cut range in halfrange = range / 2;

% Compute the new low x valuelox = x – range ;

% Compute the new high x valuehix = x + range ;

% Compute the new low y value loy = sin(lox) + 3/sqrt(lox);

% Compute the new high y valuehiy = sin(hix) + 3/sqrt(hix);

% Save the old slopeoldm = m;

% Compute the slope of the secant linem = (hiy – loy) / (hix – lox);

% increment counterctr = ctr + 1;

end % end of loop

(or have just started)

48

Page 49: First, some left-overs… Lazy languages Nesting 1

Derivatives - the endAdd some fprintf’s and…

LOW x HIGH x Slope Difference in slope 1: 0.000000000000000, 10.000000000000000 -Inf -Inf 2: 2.500000000000000, 7.500000000000000 -0.092478729683983 Inf 3: 3.750000000000000, 6.250000000000000 0.075675505484728 0.168154235168711 4: 4.375000000000000, 5.625000000000000 0.130061340134248 0.054385834649520 5: 4.687500000000000, 5.312500000000000 0.144575140383541 0.014513800249293 6: 4.843750000000000, 5.156250000000000 0.148263340290110 0.003688199906569 7: 4.921875000000000, 5.078125000000000 0.149189163013407 0.000925822723297 8: 4.960937500000000, 5.039062500000000 0.149420855093148 0.000231692079740 9: 4.980468750000000, 5.019531250000000 0.149478792897432 0.000057937804284 10: 4.990234375000000, 5.009765625000000 0.149493278272689 0.000014485375257 11: 4.995117187500000, 5.004882812500000 0.149496899674250 0.000003621401561 12: 4.997558593750000, 5.002441406250000 0.149497805028204 0.000000905353954 13: 4.998779296875000, 5.001220703125000 0.149498031366966 0.000000226338761 14: 4.999389648437500, 5.000610351562500 0.149498087951815 0.000000056584850 15: 4.999694824218750, 5.000305175781250 0.149498102098005 0.000000014146190 16: 4.999847412109375, 5.000152587890625 0.149498105634484 0.000000003536479 17: 4.999923706054688, 5.000076293945313 0.149498106518877 0.000000000884393

Slope of f(x) = sin(x) + 3/sqrt(x) at x=5 is approximately 0.1494981 49

Page 56: First, some left-overs… Lazy languages Nesting 1

Composite Trapezoidal

• Can we just combine multiple trapezoids?• Yes - Instead of 1 large trapezoid, we’ll use multiple smaller trapezoids

and add up their areas (and their errors)

• So we make a and b change. Instead of a=0 and b=5, we’ll move them from

• 0 and 1 then • to 1 and 2 , then• to 2 and 3, then• to 3 and 4, then• to 4 and 5

Page 61: First, some left-overs… Lazy languages Nesting 1

• With a width of 1, the value is 4.8669.• Error = 0.0302

• With a width of 0.5, the value is 4.8924• Error = 0.0074

0.0 0.00000.0482

0.5 0.19270.1937

1.0 0.58200.3878

1.5 0.96940.5554

2.0 1.25210.6624

2.5 1.39730.7030

3.0 1.41470.6874

3.5 1.33500.6323

4.0 1.19410.5544

4.5 1.02370.4679

5.0 0.8480

4.8924

Page 62: First, some left-overs… Lazy languages Nesting 1

Simpson’s 1/3 Rule

• Obviously, there will always be some error. Can we do a better job than a straight line at approximating the curve?

• Trapezoidal Method approximated two consecutive points with a line (i.e. a 1st order polynomial).

• What if we approximate over three points using a 2nd order polynomial?

Page 65: First, some left-overs… Lazy languages Nesting 1

Method h Estimate Absolute Error

Evaluations of f(x)

Math Operation

s

Trapezoidal 1 4.8669 0.0302 6 10+4

Trapezoidal 0.5 4.8924 0.0074 11 20+9

Simpson’s 1/3 1 4.901 0.0011 11 30+4

Simpson’s 1/3 0.5 4.9 0.0001 21 60+9

Simpson’s 3/8 1 4.9004 0.0005 16 90+4

Simpson’s 3/8 0.5 4.8999 0 31 180+9

Boole’s 1 4.8999 0 21 130+4Math Operations = Number of +,-,*,/ to calculate segment + Number of Additions to add up all segments

Page 66: First, some left-overs… Lazy languages Nesting 1

Wrapping Up

Very small amount of code to solve a mathematical problem

MATLAB can iterate 5 times, or 1,000 times, or a million!

The more precision needed, the more iterations, the more time!

Try to code it. See it you can find the slope of any equation.

66

Page 67: First, some left-overs… Lazy languages Nesting 1

Wrapping UpWe learned about four numerical methods 1. Bisection Method

Finds numerical solutions to complex mathematical problems Essentially a “binary search” Very general – essentially “Guess, Test, Adjust”

2. Newton’s Method for finding the roots of an equation Requires the symbolic derivative

3. Secant line approximation of a tangent point Finds the numerical value of the derivative at a point

4. Integration using rectangles, trapezoids, Simpson’s, and Boole’s 67

Page 68: First, some left-overs… Lazy languages Nesting 1

More…

Numerical Methods is often a separate class at some universities.

Other Common Topics are: Solving large systems of equations Polynomial Approximation Curve Fitting Optimization ODE/PDE

Chapra and Canale’s “Numerical Methods for Engineers” is considered “the resource”

Also: http://nm.mathforcollege.com/

68