C程序——泰勒系列_长公式 [英] C program - taylor series_long formula
问题描述
这个公式来自我的一个朋友——我为他修正了它.但我似乎无法弄清楚如何获得每个角度的正确正弦计算.有人可以帮我在罪恶部分获得正确的命令吗?
This formula is from a friend of mine --- and I fixed it up for him. But I can't seem to figure out of how to get the right sine calculations per angle. Can someone please help me in getting the right commands in the sin part?
代码:
#include<stdio.h>
#define PI 3.141592653589
#define NUMBER_OF_TERMS 10
double factorial(double x)
{
double counter, total;
counter=x;
total=x;
while(counter>1)
{
counter--;
total = total * counter;
}
return total;
}
double power(double x, double y)
{
double counter, j;
counter=0;
j = x;
while (counter<(y-1))
{
counter++;
x = x * j;
}
return x;
}
double cosine_func(double radians)
{
int counter, x;
double cosine;
x=0;
counter=0;
cosine = 0;
while(counter<NUMBER_OF_TERMS-1)
{
counter++;
x=x+2;
if(counter%2 == 0)
{
cosine = cosine - (power(radians, x)/factorial(x));
}
else if(counter%2 != 0)
{
cosine = cosine + (power(radians, x)/factorial(x));
}
}
return cosine;
}
double sine_func(double radians)
{
int counter, x;
double sin;
x=0;
counter=0;
sin = 0;
while(counter<NUMBER_OF_TERMS-2)
{
counter++;
x=x+3;
if(counter%2 != 0)
{
sin= sin -(power(radians, x)/factorial(x));
}
else if(counter%2 != 0)
{
sin= sin + (power(radians, x)/factorial(x));
}
}
return sin;
}
main()
{
double counter;
double x_angles;
double x_radians;
double cosine;
double sin;
printf("11526769\n\n");
printf("x(degrees)\t x(radians)\t\t cos x\t\t sin x\t\t");
counter = 0;
x_angles = -185;
while (x_angles<180)
{
counter++;
x_angles+=5;
x_radians=x_angles*PI/180.0;
cosine=1-cosine_func(x_radians);
sin=x_radians-sine_func(x_radians);
printf("%.2lf\t\t%.12lf\t\t%.12lf\t\t%.12lf\n", x_angles, x_radians, cosine, sin);
}
return 0;
}
推荐答案
允许任意数量术语的代码
从https://stackoverflow.com/a/28227419/3088138
避免重新计算递归函数(例如相同参数的幂和阶乘)始终是一个好主意.因此,简化为(最少量的)基本算术运算如下所示:
Avoiding the recomputation of recursive functions like powers of the same argument and factorials is always a good idea. Thus a reduction to (a minimal amount of) elementary arithmetic operations looks like this:
public static double sine(int terms, double x) {
double result = 1;
double mxx = -x*x;
double addens = 1;
double temp = 2;
for(int n = 2; n <= terms; n++) {
addens *= mxx/temp++/temp++;
result += addens;
}
return x*result;
}
<小时>
要研究生产质量实现,您可以访问基本计算器 bc 的数学库 libmath,gnu 版本可以在 http://code.metager.de/source/xref/gnu/bc/1.06/bc/libmath.b
它规范了论点wrt.pi,然后使用罪的泰勒级数.
It normalizes the argument wrt. pi and then uses the Taylor series of sin.
如果您被限制在幂级数中的固定项数,您可以使用类似 Horner 的代码进行计算.例如,取 4 个术语,然后
If you are restricted to a fixed number of terms in the power series, you can use a Horner like code for its evaluation. As example, take 4 terms, then
x-x^3/3!+x^5/5!-x^7/7! = x*(1-x²/6*(1-x²/20*(1-x²/42)))
这可以放入一个循环中
res = 1
for( k=num_terms; k-->1;) {
res = 1-x*x*res/(2*k*(2*k+1))
}
res = res*x;
或者你可以展开循环
res = 1-x*x/42;
res = 1-x*x/20*res;
res = 1-x*x/6*res;
res = x*res;
您可以自由组合 x2=x*x
或重命名 x=angle_radians
.
You are free to combine x2=x*x
or to rename x=angle_radians
.
这篇关于C程序——泰勒系列_长公式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!