ENG 06 – Lecture 11; 2/12/2013
Numerical Calculus and Solution of Nonlinear Equations
Numerical Calculus
o Matlab has good built in methods to give numerical solutions
o Have to make sure answers make sense
Review: Anonymous Functions
o One line function that you can use quickly
Name = @(input)statement
Quick numerical derivative estimations
o Derivative is change in y over change in x
o Function diff
accepts a single vector as argument
Calculates difference between each element
Diff(y)./diff(x) estimates derivative
Caution: dimensions of slope vector will not be the same as x and y
vectors, there will be one less
x=0:.001:10;
y = x.^2;
slope = diff(y)./diff(x);
o Slope is now vector of slopes at all points corresponding to x and y vectors
Find index of array closest to 3.4 and read out value of slope
[i,k]=min(abs(x‐3.4)) i=
0 k=
3401 >> slope(k)
ans = 6.8010
Forward Difference Approx. of First Derivative
o To approximate derivative at x(i), find point to the right, x(i+1), and find slope
between the two points

[email protected](t,delt) (func(t+delt)‐func (t))./delt;
Backward Difference Approx of First Derivative
o Look at point behind x(i) this time
f’(x(i)) = (f(x(i))-f(x(i-1)))/(x(i)-(x(i-1))

[email protected](t,delt) (func(t)‐func (t‐delt))./delt;
Central Difference Approx. of First Derivative
o Take points from behind and in front of x(i)
o

[email protected](t,delt) (func(t+delt)‐func (t‐delt))./(2.*delt);
Error Estimates
o Use taylor expansion to find difference
Example
o Velocity of rocket: find acceleration at 16 seconds
o Use exact expression, find forward, backward, and central difference approx..
o Numerical Estimate
>>

[email protected](t) 2000*log((14*10^4)./(14*10^4‐2100.*t))‐9.8.*t;
>>

[email protected](t,delt) (velocity(t+delt)‐velocity(t))./delt;
>>

[email protected](t,delt) (velocity(t)‐velocity(t‐delt))./delt;
>>

[email protected](t,delt) (velocity(t+delt)‐velocity(t‐delt))./(2.*delt);
Use time increment of 2 seconds (delt = 2)
acell_forw(16,2)
ans =30.4739
>> acell_back(16,2)
ans =28.9145
>> acell_cent(16,2)
ans =29.6942
The exact value is 29.674 m/s2. (center approx. is closest)
You can also get a vector of acceleration by inputting delt as a vector of
increments
Integration
o Measures area under a function plotted on a graph
o Limits of integration: x(i-1) is lower, x(i+1) is upper
Numerical Integration
In estimating integral, two options:
o If you are given discrete points
Use trapz
x = 0:.001:10;
y = x.^2;
Z = trapz(x,y);
disp(Z)
333.3333
o If you are given a function (or if you are given discrete points and use
interpolation/regression to create closed form expression)
Use quad or quadl
Give a function as a function handle from anonymous or user-defined
function as well as upper and lower limits
Quad and quadl will usually give the same answer
Ex.
>> y = @(x) x.^2;
Z = quad(y,0,10)