BLITZ ++ calculates the second order derivative

xiaoxiao2021-03-06  14

// Solving 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 numericly using a // [1 -2 1] Mask, and the approximation error is computed.

/ * The second-order derivative of COS (X) ^ 2 is 2 (sin (x) ^ 2 - cos (x) ^ 2)

In the range of [0, 1) below, Delta is a step, and the second order derivative is calculated using [1 -2 1].

Look at the error of the exact value * /

Const int NumSample = 100; // Number of Samples Double Delta = 1. / NumSample; // spacing of samples range R (0, Numsamples - 1); // Index Set of the Vector

// Sample the function y = cos (x) ^ 2 over [0,1) /// A 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 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 secret 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 not apply. 98, since // we need one element on each one omen Either Side To Apply the mask. Range i (1 , Numsamples-2; Vector Y2 (NUMSAMPLES);

Y2 (i) = (y (i-1) - 2 * y (i) y (i 1)) / (delta * delta); // the Above Difference Equation Will Be Transformed Into // Something Along The Lines Of // / Double _T2 = delta * delta; // for (int i = 1; i <99; i) // Y2 [i] = (y [i-1] - 2 * y [i] Y [i 1]) / _t2; // now Calculate the root mean square approximation error: double error = SQRT (mean (SQR (Y2 (i) - Y2Exact (i)))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))))); // Display a From the product. // this Range ConstructionRements // of 15. Range DisplayRange (1, 91, 15); cout << "Exact DeriVative:" << Y2Exact (DisplayRange) << Endl << "Approximation:" << Y2) << Endl << "RMS Error:" << Error << Endl;

Return 0;}

OUTPUT:

Exact Derivative: [-1.9996 -1.89847 -1.62776 -1.21164 -0.687291 -0.1015495] Approximation: [-1.99953 -1.89841 -1.6277 -1.2116 -0.687269 -0.1015468] RMS error: 4.24826e-05

转载请注明原文地址:https://www.9cbs.com/read-48396.html

New Post(0)