MME 208 Lecture-6
MME 208 Lecture-6
Course Instructors: Humayun Kabir, Sumit Bhowmick, Md. Nazmul Ahsan Dipon
Differentiation
• The derivative of the function y = f(x) is a measure of how y changes
with x.
• If you can define an equation that relates x and y, you can use the
functions contained in the symbolic toolbox (You need to install it) to
find an equation for the derivative.
• However, if all you have are data, you can approximate the derivative
by dividing the change in y by the change in x:
Differentiation
• The derivative corresponds to the slope of each of the line segments
used to connect the data as shown in the figure.
• Notice that the vector returned when you use the diff function is
one element shorter than the input vector, because you are
calculating differences.
• When you use the diff function to calculate slopes, you are
calculating the slope between values of x, not at a particular value.
The diff Function
• If you want to plot these slopes against x, probably the best approach
is to create a bar graph, since the rates of change are not continuous.
• The x-values were adjusted to the average for each line segment:
a = axes;
bar(x, slope,1)
set (a, ‘Ydir’, ‘reverse’)
The diff Function
• The diff function can also be used to approximate a derivative
numerically if you know the relationship between x and y. For
example, if
• We could create a set of ordered pairs for any number of x-values.
% Derivative of f(x)
d = diff(f);
val = subs(d,x,pi/2);
• We can use the difference function and assign the result as the
derivative at the first point in the range. This is termed forward
difference, as it approximates the derivative by examining the
following x and y values in the array.
• To approximate the derivative for the first nine values in the x array
• Notice that to make the code more general we’ve defined the last
element number using the length function, which in this case returns
a value of 10.
Forward Difference
• To find the percentage error between this approximation and the
analytical value
• Downside of using this technique is that it won’t work for either the first or
last value in the array.
• Let's say you have data points (xi, yi) and you want to estimate the second
derivative at a point x.
• You can perform polynomial interpolation using the data points to get an
interpolated polynomial equation P(x).
• Then, differentiate P(x) twice to obtain the second derivative P''(x), and
• Finally, evaluate P''(x) at the desired point x to get the estimated second
derivative.
Example 1: Polynomial Interpolation
• % Define the function
• % polyder - calculates the coefficients of the derivatives of the polynomial represented by the
coeff array.
• % polyval function to evaluate the polynomial and its derivatives at the interpolation points
• % stored in x_interp. y_values stores the original polynomial values
• figure;
• hold on;
• plot(x_interp, y_values, 'b', 'DisplayName', 'y vs x');
• plot(x_interp, first_derivative_values, 'g', 'DisplayName', '1st derivative vs x');
• plot(x_interp, second_derivative_values, 'r', 'DisplayName', '2nd derivative vs x');
• plot(x_interp, third_derivative_values, 'm', 'DisplayName', '3rd derivative vs x');
• hold off;
• xlabel('x');
• ylabel('Values');
• title('Function and Derivatives');
• legend('Location', 'best');
• grid on;
Example 1: Polynomial Interpolation
Example 1: Polynomial Interpolation
• % Finding derivatives at x_eval = 0.5
• The last two fields in the function define the limits of integration, in
this case from 0 to 1.
KEY IDEA
Use trapz for ordered pairs of data.
Use quad or quadl for functions.
Example Problem
Example Problem