Blitz++ 计算二阶导数
计算 ++ 导数 二阶
2023-09-27 14:27:55 时间
// 整理 by RobinKin
#include blitz / vector.h
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 ++i)
// {
// double _t1 = cos(i * delta);
// y[i] = _t1 * _t1;
// }
Vector double y = sqr(cos(R * delta));
// Sample the exact second derivative
Vector double 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 double 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 ++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 few elements from the vectors.
// This range constructor means elements 1 to 91 in increments
// of 15.
Range displayRange( 1 , 91 , 15 );
cout " Exact derivative: " y2exact(displayRange) endl
" Approximation: " y2(Range(displayRange)) 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
第三章 导数与微分 一、导数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...
#include blitz / vector.h
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 ++i)
// {
// double _t1 = cos(i * delta);
// y[i] = _t1 * _t1;
// }
Vector double y = sqr(cos(R * delta));
// Sample the exact second derivative
Vector double 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 double 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 ++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 few elements from the vectors.
// This range constructor means elements 1 to 91 in increments
// of 15.
Range displayRange( 1 , 91 , 15 );
cout " Exact derivative: " y2exact(displayRange) endl
" Approximation: " y2(Range(displayRange)) 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
第三章 导数与微分 一、导数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...
相关文章
- 解读革命性容器集群CCE Turbo:计算、网络、调度全方位加速
- 衡量模型复杂度:①模型参数量、②FLOPs(浮点运算数,计算量,s小写)【全连接层无权值共享,浮点运算数==参数量】;衡量硬件(GPU)性能的指标:FLOPS(每秒浮点运算次数,计算速度,S大写)
- 默安科技聂万泉:2018年后云计算安全需求将全面爆发
- 云计算降低的企业隐形成本,你知多少
- 卷积神经网络(CNN)张量(图像)的尺寸和参数计算(深度学习)
- 畅销书作家Mark Mueller-Eberstein :云计算大数据推动业务创新发展
- 云计算与大数据下的革新 行业应用广泛
- 2023.2.8,周三【图神经网络 学习记录21】动态图分类(重点 以离散网络 和 连续型网络为例);DySAT算法:快照,结构Self-Attention构造,时域Self-Attention的计算
- 阿里集团CTO王坚:云计算是一种公共服务
- Blitz++与MTL两大数值计算程序库(C++)的简介