Loop Application: Numerical Methods, Part 1 The power of Matlab Mathematics + Coding

Preview:

Citation preview

Loop Application:Numerical Methods, Part 1

The power of Matlab

Mathematics + Coding

Numerical Methods

We are going to develop some programs in the class of numerical methods. Numerical 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 – for example, it might mean that there is less than 1% change or it might mean less than 0.00001% change.

1. Newton’s Method

Prepare a MATLAB program that uses Newton’s Method to find the roots of the equation:

y(x) = x3 + 2x2 – 300/x

Newton’s Method is used to find the roots of an equation – the values of the independent variable that make the dependent variable 0.

Newton’s Method

f(x) = x3 + 2x2 – 300/x

What does the answer appear to be?

Newton’s Method

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

Xn is the initial guess

Xn+1 is the next approximation using the tangent line

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

The Big Picture (1/4)

repeat – repeat – repeat…

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

1st: guess! xn

2nd: Find the y value for this x-value - go hit the actual curve

3rd: Using the slope m, the coordinates (Xn , Yn), calculate Xn+1 :

At this (xn , yn), calculate the slope of the curve using the derivative:

m = f’(xn)

The Big Picture (2/4)

repeat – repeat – repeat…

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

1st: guess again!

2nd: find y and m

3rd: calculate new X intercept

TOO FAR

The Big Picture (3/4)

repeat – repeat – repeat…

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

1st: guess again!

2nd: find y and m

3rd: calculate new X intercept

TOO FAR

The Big Picture (3/4) – Eventually…

Stop the loop, when there is not much difference between the guess and the new value!

CLOSE ENOUGH! guess

X inter-ecept

Newton’s MethodAlgorithm:• Provide an initial guess at the answer• Find the slope of the tangent line at the point (requires derivative)• Using the line’s equation, find the x-intercept• Repeat above until close enough, using the x intercept as the new “guess”

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) end

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

% 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+1x_n = 1000;

x_np1 = 5; % initial guess

Newton’s MethodAdd 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

End of Presentation

Recommended