Numeric approximation : process of solving a mathematical problem using a numeric algorithm that estimates a solution
Function handle provides a way to pass a function as a parameter
Examples:
sinHandle = @sin; natLog = @log; xx = 0.1 : 0.01 : 2*pi; yy1 = sinHandle(xx); yy2 = natLog(xx); plot(xx, yy1, 'b:', xx, yy2, 'r-.')
Anonymous function : a way to define (write) a function handle without defining a separate file for the function
Examples:
quadHandle1 = @(x)x.^2 + 2*x + 3; quadHandle2 = @(p)(p – 1).^2; disp(quadHandle1(2) + quadHandle2(-1))
Example:
function result = addNoise(values, noiseFctn) % Modifies the values by using the given function to add "noise" result = values + noiseFctn(values);
Using the addNoise function:
xx = -10 : 0.001 : 20; myFctn = @(x)polyval([0.01, -0.1, -2, 5], x); yy = myFctn(xx); noise1 = addNoise(yy, @cos); noise2 = addNoise(yy, @(x)0.5*sin(5*x)); plot(xx, yy, 'g-', xx, noise1, 'r--', xx, noise2, 'b-.')
Example: estimating the derivative of a function
Recall from calculus:
function dfdx = derivative(fctn, x) % Estimates the derivative of the given function at the given x value(s)
Using the derivative function:
dyy = derivative(myFctn, xx); plot(xx, yy, 'g-', xx, dyy, 'k:')
Find the roots of f(x) = x3 - 3x2 + x/2 + 2cos(x)
Define function (in fctn.m)
function val = fctn(x) val = x.^3 - 3*x.^2 + x/2 + 2*cos(x); end
Plot
xx = -1 : 0.1 : 3.5; plot(xx, fctn(xx), 'b', [-1 3.5], [0 0], 'k:', [0 0], [-4 8], 'k:')
Find the roots
root1 = root2 = root3 =
Find where f(x) = 1
Consider the following intersecting curves
xx = 0 : 0.1 : 5; yy1 = (xx - 3).^2 + 5; yy2 = xx.*sin(xx) + 10; plot(xx, yy1, 'b:', xx, yy2, 'k') axis([0 5 0 15])
Area between the curves =
Define function (in f2_minus_f1.m)
function y_diff = f2_minus_f1(x) y1 = (x - 3).^2 + 5; y2 = x.*sin(x) + 10; y_diff = y2 - y1;
Find where the functions intersect
a = b =
Numerically integrate to find area between curves
area =
Finding volumes of revolution
General approach: integrate the cross-sectional area of slices perpendicular to the x-axis.
Volume =
| around x-axis | around y-axis |