//整理 by RobinKin
下面在[0,1) 的范围中,以delta为步长 ,用[ 1 -2 1 ] 的方法计算二阶导数
看看和精确值的误差
*/
//整理 by RobinKin
#include
using namespace blitz;
int main()
{
// In this example, the function cos(x)^2 and its second derivative
// 2 (sin(x)^2 - cos(x)^2) are sampled over the range [0,1).
// The second derivative is approximated numerically using a
// [ 1 -2 1 ] mask, and the approximation error is computed.
/* cos(x)^2 的二阶导数 是2 (sin(x)^2 - cos(x)^2)
下面在[0,1) 的范围中,以delta为步长 ,用[ 1 -2 1 ] 的方法计算二阶导数
看看和精确值的误差
*/
const int numSamples = 100; // Number of samples
double delta = 1. / numSamples; // Spacing of samples
Range R(0, numSamples - 1); // Index set of the vector
// Sample the function y = cos(x)^2 over [0,1)
//
// An object of type Range can be treated as a vector, and used
// as a term in vector expressions.
//
// The initialization for y (below) will be translated via expression
// templates into something of the flavour
//
// for (unsigned i=0; i < 99; ++i)
// {
// double _t1 = cos(i * delta);
// y[i] = _t1 * _t1;
// }
Vector y = sqr(cos(R * delta));
// Sample the exact second derivative
Vector y2exact = 2.0 * (sqr(sin(R * delta)) - sqr(cos(R * delta)));
// Approximate the 2nd derivative using a [ 1 -2 1 ] mask
// We can only apply this mask to the elements 1 .. 98, since
// we need one element on either side to apply the mask.
Range I(1,numSamples-2);
Vector y2(numSamples);