Main Content

fnder

Differentiate function

Description

example

fprime = fnder(f,dorder) returns the dorder-th derivative of the function in f. The default value of dorder is 1. For negative dorder, the particular |dorder|-th indefinite integral is returned that vanishes |dorder|-fold at the left endpoint of the basic interval.

The output is of the same form as the input, they are either both ppforms, or both B-forms, or both stforms.

If the function in f is m-variate, then dorder must be given, and must be of length m.

Also:

  • If f is in ppform, or in B-form with its last knot of sufficiently high multiplicity, then, up to rounding errors, f and fnder(fnint(f)) are the same.

  • If f is in ppform and fa is the value of the function in f at the left end of its basic interval, then, up to rounding errors, f and fnint(fnder(f),fa) are the same, unless the function described by f has jump discontinuities.

  • If f contains the B-form of f, and t1 is its leftmost knot, then, up to rounding errors, fnint(fnder(f)) contains the B-form of ff(t1). However, its leftmost knot will have lost one multiplicity (if it had multiplicity > 1 to begin with). Also, its rightmost knot will have full multiplicity even if the rightmost knot for the B-form of f in f doesn't. To verify this, create a spline, sp = spmak([0 0 1], 1). This spline is, on its basic interval [0..1], the straight line that is 1 at 0 and 0 at 1. Now integrate its derivative: spdi = fnint(fnder(sp)). The spline in spdi has the same basic interval, but, on that interval, it agrees with the straight line that is 0 at 0 and –1 at 1.

fnder(f) is the same as fnder(f,1).

Examples

collapse all

This example shows how to calculate the first and second order derivative functions of three B-splines of order 2, 3, and 4. Then it plots the splines, and their derivatives, and compares the results.

% Create the knots sequences
t1 = [0 .8 2];
t2 = [3 4.4 5  6];
t3 = [7  7.9  9.2 10 11];
tt = [t1 t2 t3];

% Accessory variables and commands for plotting purposes
cl = ['g','r','b','k','k'];
v = 5.4; d1 = 2.5; d2 = 0; s1 = 1; s2 = .5;
ext = tt([1 end])+[-.5 .5];
plot(ext([1 2]),[v v],cl(5))
hold on
plot(ext([1 2]),[d1 d1],cl(5))
plot(ext([1 2]),[d2 d2],cl(5))
ts = [tt;tt;NaN(size(tt))];
ty = repmat(.2*[-1;0;NaN],size(tt));
plot(ts(:),ty(:)+v,cl(5))
plot(ts(:),ty(:)+d1,cl(5))
plot(ts(:),ty(:)+d2,cl(5))

% Spline 1 (linear)
b1 = spmak(t1,1);
p1 = [t1;0 1 0];
% Calculate the first and second derivative of spline 1
db1 = fnder(b1);
p11 = fnplt(db1,'j');
p12 = fnplt(fnder(db1));
lw = 2;
plot(p1(1,:),p1(2,:)+v,cl(2),'LineWidth',lw)
plot(p11(1,:),s1*p11(2,:)+d1,cl(2),'LineWidth',lw)
plot(p12(1,:),s2*p12(2,:)+d2,cl(2),'LineWidth',lw)

% Spline 2 (quadratic)
b1 = spmak(t2,1);
p1 = fnplt(b1);
% Calculate the first and second derivative of spline 2
db1 = fnder(b1);
p11 = [t2;fnval(db1,t2)];
p12 = fnplt(fnder(db1),'j');
plot(p1(1,:),p1(2,:)+v,cl(3),'LineWidth',lw)
plot(p11(1,:),s1*p11(2,:)+d1,cl(3),'LineWidth',lw)
plot(p12(1,:),s2*p12(2,:)+d2,cl(3),'LineWidth',lw)

% Spline 3 (cubic)
b1 = spmak(t3,1);
p1 = fnplt(b1);
% Calculate the first and second derivative of spline 3
db1 = fnder(b1);
p11 = fnplt(db1);
p12=[t3;fnval(fnder(db1),t3)];
plot(p1(1,:),p1(2,:)+v,cl(4),'LineWidth',lw)
plot(p11(1,:),s1*p11(2,:)+d1,cl(4),'LineWidth',lw)
plot(p12(1,:),s2*p12(2,:)+d2,cl(4),'LineWidth',lw)

% Formatting the plot
tey = v+1.5;
text(t1(2)-.5,tey,'linear','FontSize',12,'Color',cl(2))
text(t2(2)-.8,tey,'quadratic','FontSize',12,'Color',cl(3))
text(t3(3)-.5,tey,'cubic','FontSize',12,'Color',cl(4))
text(-2,v,'B','FontSize',12)
text(-2,d1,'DB','FontSize',12)
text(-2,d2,'D^2B')
axis([-1 12 -2 7.5])
title({'B-splines with Simple Knots and Their Derivatives'})
axis off
hold off

Input Arguments

collapse all

Spline in either ppform, B-form or stform, specified as a structure with these fields:

Form of the spline, returned as pp, B-, or tp00. pp indicates that the spline is given in piecewise polynomial form, B- indicates that the spline is given in B-form, and tp00 indicates that the spline is given in stform.

Knot positions of the spline, returned as a vector or as a cell array of vectors for multivariate data. Vectors contain strictly increasing elements that represent the start and end of each of the intervals over which the polynomial pieces are defined.

Coefficients of polynomials for each piece, returned as a matrix or as an array for multivariate data.

Number of polynomial pieces describing the spline, returned as a scalar or as a vector of numbers of pieces in each variable for multivariate data.

Order of the polynomial function describing each polynomial piece of the spline, returned as a scalar or as a vector containing the order in each variable for multivariate data.

Dimensionality of the target function, returned as a scalar.

Order of the derivative of the function f, specified as a scalar or vector for multivariate functions.

Data Types: single | double

Output Arguments

collapse all

Derivative function of the f spline in either ppform, B-form, or stform, returned as a structure with these fields:

Form of the spline, returned as pp, B-, or tp00. The form of the derivative function is the same as the form of the f function. pp indicates that the spline is given in piecewise polynomial form, B- indicates that the spline is given in B-form, and tp00 indicates that the spline is given in stform.

Knot positions of the spline, returned as a vector or as a cell array of vectors for multivariate data. Vectors contain strictly increasing elements that represent the start and end of each of the intervals over which the polynomial pieces are defined.

Coefficients of polynomials for each piece, returned as a matrix or as an array for multivariate data.

Number of polynomial pieces describing the spline, returned as a scalar or as a vector of numbers of pieces in each variable for multivariate data.

Order of the polynomial function describing each polynomial piece of the spline, returned as a scalar or as a vector containing the order in each variable for multivariate data.

Dimensionality of the target function, returned as a scalar.

Limitations

  • The fnder function does not work with rational splines. To work with rational splines, use the fntlr function instead.

  • The fnder function works for stforms only in a limited way: if the type is tp00, then dorder can be [1,0] or [0,1].

Algorithms

For differentiation of either polynomial form, the fnder function finds the derivatives in the piecewise-polynomial sense. The function differentiates each polynomial piece separately, and ignores jump discontinuities between polynomial pieces during differentiation.

For the B-form, the function uses the [PGS; (X.10)] formulas for differentiation.

For the stform, differentiation relies on knowing a formula for the relevant derivative of the basis function of the particular type.

Introduced before R2006a