Approximation to The Derivative

Introduction Often times in practice, we aren’t working with a function but a set of data where we don’t have a function that produces the data. We may need the derivative for some other calculation. We know derivative rules when we have an equation, but what are we supposed to do when we don’t really know anything about a function? We can calculate a numerical derivative. There are several ways of doing this, they are called finite differences. Basically, we use the slope of the line connecting two adjacent points as an approximation to the derivative. The three ways we can do this approximation is forward differences, backward differences, and centered differences. We’ll be doing forward differences in this exercise. The built-in function diff calculates the difference of a point and its neighbor. If your matrix has the form
y=[y Approximation to The Derivative
1

# ORDER A PLAGIARISM-FREE PAPER HERE

,y
2

,y
3

,…,y
n−1

,y
n

]
then
diff(y)=[y
2

−y
1

,y
3

−y
2

,y
4

y
3

,…,y
n

−y
n−1

]
. This means that if
y
has n elements,
diff(y)
has one less element Approximation to The Derivative
(n−1)
. If you divide
diff(y)
by
Δx
(the spacing between the points in the domain of
y
), then you get an approximation to
y

(x)
. Instructions 1.) Create an
m
-file called exercise3_first name_last name.m 2.) Create the variable
delx=
100
π

; 3.) Create the matrix
x=(0:
del
x:8π)
4.) Create a second matrix called
y=cos(2x)
; 5.) You’re going to create a numerical approximation to the derivative of
y=cos(2x)
. Create the matrix
dy
by giving the function diff your matrix
y
. 6.) Your last step is to create the matrix
dydx
by dividing dy by del
x
. 7.) Graph
x
vs.
y
in red dots. 8.) Graph the derivative in black dots. To graph it, you’ll have to use
x(2:end)
. That’s because diff returns a matrix with one less element. So you can’t use
x
, it has one too many values. 9.) Calculate the true derivative by hand and graph it in green dots. 10.) Add a legend, title, xlabel, and ylabel. You don’t need units in the labels. 11.) Answer the following question using the disp() function. As
Δx
gets smaller, should the approximation get better or worse? Justify your answer. Approximation to The Derivative