提问人: 提问时间:1/22/2022 更新时间:1/22/2022 访问量:61
如何更改我的 C 程序以提供正确的微分计算输出?
How can I change my C program to give correct output for differentiation calculation?
问:
我正在尝试计算由于直接计算(即 cos(1))而导致的 f'(1)(其中 f(x)=sin(x))计算中的绝对误差,以及使用公式完成的绝对误差
f'(x) = (f(x+h)-f(x))/h
即误差函数 = cos(1) - ((sin(1+h)-sin(1))/h),对于较小的 h 值。
但是对于“h”的某个值(比如 10^{-8}),当我通过一个非常精确的计算器(凯尔桑计算器)进行计算时,绝对误差为 4.207355e-9,但使用以下程序计算相同的内容:
#include <stdio.h>
#include <math.h>
int main() {
double h = 1e-8;
double a = (sin(1 + h) - sin(1)) / h;
printf("%10e", cos(1) - a);
}
我得到了 2.969885e-09。是否可以在 C 上正确计算?
答:
0赞
n. m. could be an AI
1/22/2022
#1
一旦你对两个非常接近的值进行四舍五入并减去四舍五入的值,结果就会到处都是。
使用一点三角函数,您可以更准确地表示两个正弦的差异。
double a = 2 * cos(1+h/2) * sin(h/2) / h;
此更改导致打印的正好是 4.207355e-09。
评论