0% found this document useful (0 votes)
9 views

MME 208 Lecture-6

math lab

Uploaded by

Samiul Karim Sk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

MME 208 Lecture-6

math lab

Uploaded by

Samiul Karim Sk
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 39

Lecture: 06

MME 208: Computer Applications


to Materials Engineering
Topic: Numerical Differentiation and Integration

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.

• Assign x = [0:5]; %time


• y= [15, 10, 9, 6, 2, 0]; %temperature

• If, for example, these data describe the


measured temperature of a reaction
chamber at different points in time, the
slopes denote the cooling rate during
each time segment.
The diff Function
• diff - find the difference between element values in a vector and
that can be used to calculate the slope of ordered pairs of data.
• For example, to find the change in our x-values, we type

• Because the x-values are evenly spaced, this returns

• Similarly, the difference in y-values is


The diff Function
• To find the slope, we just need to divide delta_y by delta_x:

• 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:

x(:, 1:5) is a way to select columns 1 through 5 of the matrix x. It


includes columns 1, 2, 3, 4, and 5 while keeping all rows. The : in
the middle signifies a range, and it's a common notation in MATLAB
to specify a range of indices for selection or operations.

For the reverse Y axis: Follow the steps

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.

• Both lines are created by connecting


the points with straight lines; however,
the big_x and big_y values are so
close together that the graph looks like
a continuous curve.
The diff Function
• The slope of the x–y plot was calculated with the diff function

• Using more point, we can get a smoother representation, though still


discontinuous:
The diff Function
• Using more points,

• This code results in an almost smooth representation of the slope as


a function of x.
Different syntax of diff function
• f’ = diff(f)
• f’ = diff(f, a) • We are already familiar with f’ = diff(f)
• f’’ = diff(f, b, 2)
• Evaluating the derivative of a function at a specified value using
subs(y,x,k).
• subs(y,x,k), it gives the value of function y at x = k
% Create a symbolic expression in
# variable x
syms x
f = cos(x);
disp("f(x) :");
disp(f);

% Derivative of f(x)
d = diff(f);
val = subs(d,x,pi/2);

disp("Value of f'(x) at x = pi/2:");


disp(val)
Different syntax of diff function
• f’ = diff(f, a) - returns the derivative of function f with respect to variable a.
% Create a symbolic expression in variable x
syms x t;
f = sin(x*t);
disp("f(x) :");
disp(f);

% Derivative of f(x,t) wrt t


d = diff(f,t);
disp("Derivative of f(x,t) wrt t:");
disp(d);

• f’’ = diff(f, b, 2) - returns the double derivative of function f with respect to


variable b. % Create a symbolic expression in
% variable x,n
syms x n;
f = x^n;
disp("f(x,n) :");
disp(f);

% Double Derivative of f(x,n) wrt x


d = diff(f,x,2);
disp("Double Derivative of f(x,n) wrt x:");
disp(d);
Forward, Backward, and Central Difference
• What if you want to approximate the derivative at a point, instead of
over a range?

• Numerical differentiation estimate the derivative of a function at a


point xk by approximating the slope of the tangent line at xk using
values of the function at points near xk.

• The approximation of the slope


of the tangent line can be done
in several ways, as shown in
Figure
Forward Difference
• The forward difference approximation of the derivative at a single
value of x is the slope of the line between the adjacent points as
follows:

• 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.

• For example, take the sine function


Forward Difference
• We know:

• To find the derivative analytically in MATLAB®

• To approximate the derivative for the first nine values in the x array

• It isn’t possible to find an approximation for the derivative at the last


point in the x array, so we use NaN (not a number) as a place holder.

• 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

• Finally, to create an output table so we can evaluate the results


Forward Difference
• There are significant errors in the approximation as the analytical
result approaches 0, but the absolute error is fairly small.
• Notice that there is no approximation of the derivative for the last
value of x, so a value of NaN was added.
Forward Difference

• Clearly, we can do a better job


of approximating the derivative
by specifying more values of x
(effectively making the points
closer together).
Backward Difference
• The backwards difference is very similar. Instead of assigning the
approximation of the derivative to the first value in a range, it is
assigned to the last value.

• To solve this problem in MATLAB®, we can use the diff function


again.
• Similarly to the first example, a value of NaN was added to the
dydx_approx matrix, but this time it is the first value, not the last.
.
Backward Difference
• To create an output table:

• The errors from forward and


backward difference
techniques are almost
identical.

• The large error for the final


table entry is due to the
division by 0.
.
Central Difference
• Using a central difference technique, which considers both forward and
backward points, brings us closer to the actual point of interest.
• The approximation is therefore:

• Downside of using this technique is that it won’t work for either the first or
last value in the array.

• gradient - approximates derivatives using forward and backward


differences at the array's endpoints and centered differences for the other
points.

• It takes y and x arrays as inputs and returns the derivative approximation. If


no x array is provided, the program assumes the points are evenly spaced
with a step size of 1.
Central Difference

Central difference follows the analytical solution better


Higher Derivatives by Interpolation
• Interpolation methods are commonly used when you have discrete data
points and want to estimate derivatives at points between the given data.

• 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

• f = @(x) x.^3 - 4*x.^2 + 5*x - 2;

• % Define interpolation points

• x_interp = linspace(-2, 3, 10); % Interpolation points


• y_interp = f(x_interp);

• % Calculate coefficients for the interpolation polynomial

• coeff = polyfit(x_interp, y_interp, length(x_interp) - 1);

• % polyfit – calculates coefficients of polynomial that fits the given data.


• % Degree of polynomial - length(x_interp) - 1
Example 1: Polynomial Interpolation

• % Calculate coefficients of derivatives using polyder


• derivatives = polyder(coeff);

• % polyder - calculates the coefficients of the derivatives of the polynomial represented by the
coeff array.

• % Define the point at which we want to evaluate the derivatives


• x_eval = 0.5;
Example 1: Polynomial Interpolation
• % Calculate the derivatives using polyval

• y_values = polyval(coeff, x_interp);

• % polyval function to evaluate the polynomial and its derivatives at the interpolation points
• % stored in x_interp. y_values stores the original polynomial values

• first_derivative_values = polyval(derivatives, x_interp);


• second_derivative_values = polyval(polyder(derivatives), x_interp);
• third_derivative_values = polyval(polyder(polyder(derivatives)), x_interp);
Example 1: Polynomial Interpolation
• % Create a single plot with multiple lines

• 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

• first_derivative_at_x_eval = polyval(derivatives, x_eval);


• second_derivative_at_x_eval = polyval(polyder(derivatives), x_eval);
• third_derivative_at_x_eval = polyval(polyder(polyder(derivatives)), x_eval);

• % Displaying the results

• disp(['First Derivative at x_eval:', num2str(first_derivative_at_x_eval)]);


• disp(['Second Derivative at x_eval:', num2str(second_derivative_at_x_eval)]);
• disp(['Third Derivative at x_eval:', num2str(third_derivative_at_x_eval)]);
Numerical Integration
• An integral is often thought of as the area under a curve.
• The curve's area is calculated by summing contributions from divided
rectangles:

• The MATLAB® commands to calculate this area are

• This is called the


trapezoid rule, since the
rectangles have the same
area as adjacent
trapezoids.
Trapezoid Rule
• MATLAB® includes a built-in function,
trapz, which gives the same result,
and uses the syntax

• We can estimate the area under a function-defined curve using


ordered x-y pairs. Improved approximations result from increasing the
elements in our x and y vectors.
• For example, to find the area under the function

from 0 to 1, we would define a vector of 11 x-values and calculate the


corresponding y-values:
Trapezoid Rule
• To find the area under the curve:

• This result gives us an approximation


of the area under the function:

• The preceding answer corresponds


to an approximation of the integral
from x 0 to x 1, or
Simpson’s Rule
• If the area under a curve is approximated by areas under quadratic
curve, with the interval [a, b] divided into 2n equal subintervals, then
the area can be approximated by Simpson’s rule:

• where the xk values


represent the endpoints
of the subintervals and
where x0 = a, x2n = b, and
∆x = (b − a)/(2n)
Simpson’s Rule
• MATLAB provides two quadrature functions for performing numerical
function integration without requiring the user to specify how the
rectangles are defined.
• The two functions differ in the numerical technique used.
• Functions with singularities may be solved with one approach or the
other, depending on the situation.
• The quad function uses adaptive Simpson quadrature:
Simpson’s Rule
• The quadl function uses adaptive Lobatto quadrature:

• Both functions need a user-provided function in the first field. This


function can be directly entered as a string, as demonstrated, or
defined in an M-file or as an anonymous function.

• The last two fields in the function define the limits of integration, in
this case from 0 to 1.

• Both techniques aim at returning results within an error of 1*10-6


Simpson’s Rule
• Here’s another example, using a function handle and an anonymous
function.
• First we’ll define an anonymous function for a third-order polynomial.

• Let’s plot the function:

• The integral of this


function

is the area under the


curve
Simpson’s Rule
• To evaluate the integral we’ll use the quad function.

KEY IDEA
Use trapz for ordered pairs of data.
Use quad or quadl for functions.
Example Problem
Example Problem

• Notice that in this solution


we defined an anonymous
function for P, and used the
function handle as input to
the numerical integration
functions.
Example Problem
• We could just as easily have defined the function by using a character
string inside the quad and quadl functions.
• However, in that case we would have had to replace the variables
with numerical values:

You might also like