zl程序教程

您现在的位置是:首页 >  大数据

当前栏目

Blitz++ 计算二阶导数

计算 ++ 导数 二阶
2023-09-27 14:27:55 时间
// 整理 by RobinKin 
None.gif 
None.gif#include    blitz / vector.h   
None.gif 
None.gif using   namespace  blitz;
None.gif
None.gif int  main()
ExpandedBlockStart.gif {
InBlock.gif     //  In this example, the function cos(x)^2 and its second derivative
InBlock.gif     //  2 (sin(x)^2 - cos(x)^2) are sampled over the range [0,1).
InBlock.gif     //  The second derivative is approximated numerically using a
InBlock.gif     //  [ 1 -2  1 ] mask, and the approximation error is computed. 
InBlock.gif 
ExpandedSubBlockStart.gif /*  cos(x)^2  的二阶导数 是2 (sin(x)^2 - cos(x)^2)
InBlock.gif
InBlock.gif下面在[0,1) 的范围中,以delta为步长 ,用[ 1 -2  1 ] 的方法计算二阶导数
InBlock.gif
InBlock.gif看看和精确值的误差
ExpandedSubBlockEnd.gif */ 
InBlock.gif 
InBlock.gif 
InBlock.gif     const   int  numSamples  =   100 ;               //  Number of samples 
InBlock.gif      double  delta  =   1 .  /  numSamples;           //  Spacing of samples 
InBlock.gif     Range R( 0 , numSamples  -   1 );               //  Index set of the vector
InBlock.gif
InBlock.gif     //  Sample the function y = cos(x)^2 over [0,1)
InBlock.gif     // 
InBlock.gif     //  An object of type Range can be treated as a vector, and used
InBlock.gif     //  as a term in vector expressions.
InBlock.gif     // 
InBlock.gif     //  The initialization for y (below) will be translated via expression
InBlock.gif     //  templates into something of the flavour
InBlock.gif     // 
InBlock.gif     //  for (unsigned i=0; i   ++i)
InBlock.gif     //  {
InBlock.gif     //      double _t1 = cos(i * delta);
InBlock.gif     //      y[i] = _t1 * _t1;
InBlock.gif     //  } 
InBlock.gif     
InBlock.gif    Vector   double    y  =  sqr(cos(R  *  delta));
InBlock.gif
InBlock.gif     //  Sample the exact second derivative 
InBlock.gif     Vector   double    y2exact  =   2.0   *  (sqr(sin(R  *  delta))  -  sqr(cos(R  *  delta)));
InBlock.gif
InBlock.gif     //  Approximate the 2nd derivative using a [ 1 -2  1 ] mask
InBlock.gif     //  We can only apply this mask to the elements 1 .. 98, since
InBlock.gif     //  we need one element on either side to apply the mask. 
InBlock.gif     Range I( 1 ,numSamples - 2 );
InBlock.gif    Vector   double    y2(numSamples);
InBlock.gif
InBlock.gif    y2(I)  =  (y(I - 1 )  -   2   *  y(I)  +  y(I + 1 ))  /  (delta * delta);
InBlock.gif  
InBlock.gif     //  The above difference equation will be transformed into
InBlock.gif     //  something along the lines of
InBlock.gif     // 
InBlock.gif     //  double _t2 = delta*delta;
InBlock.gif     //  for (int i=1; i   ++i)
InBlock.gif     //      y2[i] = (y[i-1] - 2 * y[i] + y[i+1]) / _t2;
InBlock.gif 
InBlock.gif     //  Now calculate the root mean square approximation error: 
InBlock.gif 
InBlock.gif     double  error  =  sqrt(mean(sqr(y2(I)  -  y2exact(I))));
InBlock.gif 
InBlock.gif     //  Display a few elements from the vectors.
InBlock.gif     //  This range constructor means elements 1 to 91 in increments
InBlock.gif     //  of 15. 
InBlock.gif     Range displayRange( 1 ,  91 ,  15 );
InBlock.gif 
InBlock.gif    cout      " Exact derivative: "      y2exact(displayRange)     endl
InBlock.gif              " Approximation:    "      y2(Range(displayRange))     endl
InBlock.gif              " RMS Error:        "      error     endl;
InBlock.gif
InBlock.gif     return   0 ;
ExpandedBlockEnd.gif
None.gif 
None.gif 
None.gifOutput:
None.gif
None.gifExact derivative:[     - 1.9996    - 1.89847    - 1.62776    - 1.21164   - 0.687291   - 0.1015495 
None.gif   ]
None.gifApproximation:   [    - 1.99953    - 1.89841     - 1.6277     - 1.2116   - 0.687269   - 0.1015468 
None.gif   ]
None.gifRMS Error:        4.24826e-05 
第三章 导数与微分 一、导数1、导数的几何意义与物理意义2、曲线某点导数所代表的斜率,与曲线该点切线的斜率,代表了微观和宏观,但他们是一致的3、函数可导与连续的关系4、基本初等函数的导数公式5、导数的运算公式6、复合函数的导数公式,变化率是传导的 二、高阶导数1、相乘函数的n阶导数公式 三、隐函数、参量函数的导数1、.
最小二乘法-公式推导 基本思想 求出这样一些未知参数使得样本点和拟合线的总误差(距离)最小 最直观的感受如下图(图引用自知乎某作者) 而这个误差(距离)可以直接相减,但是直接相减会有正有负,相互抵消了,所以就用差的平方 1 写出拟合方程y=a+bxy=a+bx 2 现有样本(x1,y1),(x2,y2).
  插值,不论在数学中的数值分析中,还是在我们实际生产生活中,都不难发现它的身影,比如造船业和飞机制造业中的三次样条曲线。那么,什么是插值呢?我们可以先看一下插值的定义,如下:   (定义)如果对于每个1≤i≤n,P(xi...