%% Lecture 6 % % Last week. % a) Taylor Series expansion, and linear interpolation. % b) Simulating time varying functions? % c) Functions with multiple outputs. % % This week. % a) Estimating Derivatives from functions % b) Newton's method. % c) Stopping conditions. % One key question is "Finding the roots of an equation", finding the % values x such that f(x) = 0. % You may have seen this in math classes in the past, but one key % question that is rarely addressed in a math class is: % How is the function f represented? % What are reasonable choices for this? % 1-- Maybe you have an array of the function values evaluated at many % points? (e.g. you have the stock market closing prices for the last % 10 years) % 2-- Maybe you know the function completely, e.g. you know it is a % polynomial and you know the ceofficients? % 3-- Maybe you have a "matlab function" that you can plug in values and % get results? % How could we compute the zeros of a function for which we have samples of % the function for all values x from 1 to 1000? % If we know the function completely? If the function is something % like y = x.^2 + 2x - 14 % % Maybe you can solve this directly (completing the square or % something, but if you can't, you can at least compute the derivative % and use newton's method). What is the key to newtons method? % % The Fundamental Theorem of Engineering, things are linear. % % Taylor expansion, first order y(x+h) = y(x) + dy/dx * h % As a side note, this is very similar to the "definition of derivative" % dy/dx = ( y(x+h)-y(x) ) / h as h gets small... % so if we want to solve for when y is zero, we remove the assumption % that "h" is small (and add the assumption that the function is linear) % to get: % y(x0+h) = y(x0) + y'(x0) * h % 0 = y(x0) + y' * h % h = -y(x0) / y' % so the next guess for x is x0 + h. function x = newton(x0) % % Special case coded for function y = x.^2 + 2x - 14 x = x0; y = x.^2 + 2*x - 14; % next class, when y = foo(x); while abs(y) > 0.01 % solve for value y = f(x0); y = x.^2 + 2*x - 14; % solve for value of y' = df(x0)/dx yprime = 2*x + 2; % solve for offset = -y / yprime offset = -y / yprime; x = x + offset end % Given some matlab function "foo" that calculates the value of the function.