160 likes | 344 Views
Numerical Methods Derivative Method. The power of Matlab Mathematics + Coding. Derivatives, intro. Another numerical method involves finding the derivative at a point on a curve.
E N D
Numerical MethodsDerivative Method The power of Matlab Mathematics + Coding
Derivatives, intro. Another numerical method involves finding the derivative at a point on a curve. Basically, this method uses the secant line as an approximation – bringing it closer and closer to the tangent line This method is used when the actual symbolic derivative is hard to find! f’(x)=
Derivatives, big picture What is the slope at this point?
Derivatives (1/5) Choose a and b to be equidistant from that point, calculate slope 1 Slope 1 X
Derivatives (1/5) Reduce the range for the second iteration. Slope 1 range X range/2 range/2
Derivatives (2/5) Slope 1 x loX hiX Zoom in on x
Derivatives (3/5) Slope 1 x loX hiX Find new secant line.
Derivatives (3/5) Slope 1 Slope 2 hiY loY x loX hiX Calculate new slope
Derivatives (3/5) Slope 1 Slope 2 Is slope 2 “significantly different” from slope 1? x loX hiX Assume they are different… let’s iterate!...
Derivatives (4/5) Slope 1 Slope 2 Slope3 x loX hiX Zoom in again..
Derivatives (4/5) Slope 1 Slope 2 Slope3 Is Slope2 very different from Slope3? Iterate while yes! x loX hiX Zoom in again..
(5/5) Eventually… Stop the loop when the slopes are no longer “significantly different” Remember: “significantly different” means • maybe a 1% difference? • maybe a 0.1% difference? • maybe a 0.000001% difference?
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)
Code % Define initial difference as very large % (force loop to start) m = 1; oldm = 2; % Specify starting range and desired point range = 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 half range = range / 2; % Compute the new low x value lox = x – range ; % Compute the new high x value hix = x + range ; % Compute the new low y value loy = sin(lox) + 3/sqrt(lox); % Compute the new high y value hiy = sin(hix) + 3/sqrt(hix); % Save the old slope oldm = m; % Compute the slope of the secant line m = (hiy – loy) / (hix – lox); % increment counter ctr = ctr + 1; end% end of loop (or have just started)
Derivatives - the end Add 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=5is approximately 0.1494981
Wrapping Up • Again, very small 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.