使用泰勒系列 [英] Using Taylor Series

查看:63
本文介绍了使用泰勒系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要泰勒系列的帮助


A部分:

以度数x_deg扫描角度。使用x = PI * x_deg / 180在

弧度中表示这个角度,并使用math.h函数库计算Y = cos ^ 2(x)

(pow()和cos()

函数)。比较如此计算的Y = cos ^ 2(x)

的值与使用n_term

泰勒系列项获得的近似值y


cos ^ 2(x)= 0.5 *(1 + Sum [( - 1)^ n(2 * x)^(2n)/(2n)!]),

其中n从0变为n_term。打印相对误差

100 *(Y-y)/ Y.


扫描整数n_term值。评估(2n)!通过嵌入式

for循环语句。使用两个do / while语句继续

计算不同的n_term和不同的x_deg。对于

示例,使用flag = 1继续计算内部do / while循环中的不同n_term

,并使用flag = 0退出该循环。

使用Flag = 1继续在外部do / while循环中计算不同的x_deg

,并使用Flag = 0退出该循环

并转到Part B.(回想一下0!= 1)。


这是我的程序:

#include< stdio.h>

#include< math.h>

#define PI 3.141592654

main()

{

int n_terms,n = 0;

double angle_deg,angle_rad,csa,csa2,taylor,sum;

printf(" \ n\\\
Part答:\ n计算cos ^ 2(x)\ n \ n")的真值和近似值;

printf(" Enter x_deg:\ n");

scanf("%lf",& angle_deg);

angle_rad = angle_deg *(PI / 180。);

csa = cos(angle_rad );

csa2 = pow(csa,2。);

printf(&q uot; cos的真值^ 2(x)=%f\\\
\ n",csa2);

printf(&n_term近似cos ^ 2(x)\ nn \\ n");

printf("输入术语数:\ n");

scanf("%d",& n_terms);

printf(" \ n%d term approximation\\\
",n_terms);



}

到目前为止一切都正确显示。我只是停留在如何使用for循环来计算泰勒系列。

I need help with Taylor Series

Part A:
Scan the angle in degrees x_deg. Express this angle in
radians by using x=PI*x_deg/180, and calculate Y=cos^2(x)
by using the math.h library of functions (pow() and cos()
functions). Compare the so calculated value of Y=cos^2(x)
with the approximate value y obtained by using n_term
terms of the Taylor series

cos^2(x)=0.5*(1+Sum[(-1)^n (2*x)^(2n)/(2n)!]),

where n goes from 0 to n_term. Print the relative error
100*(Y-y)/Y.

Scan an integer value of n_term. Evaluate (2n)! by an embedded
for-loop statement. Use two do/while statements to continue
the calculations for different n_term and different x_deg. For
example, use flag=1 to continue calculations for different n_term
within the inner do/while loop, and flag=0 to exit that loop.
Use Flag=1 to continue calculations for different x_deg
within the outer do/while loop, and Flag=0 to exit that loop
and go to Part B. (Recall that 0!=1).

This is my program:
#include <stdio.h>
#include <math.h>
#define PI 3.141592654

main()
{
int n_terms, n=0;
double angle_deg, angle_rad, csa, csa2, taylor, sum;
printf("\n\nPart A:\nCalculation of True and Approximate Values of cos^2(x)\n\n");
printf("Enter x_deg: \n");
scanf("%lf", &angle_deg);
angle_rad = angle_deg * (PI/180.);
csa = cos(angle_rad);
csa2 = pow(csa, 2.);
printf("True value of cos^2(x) = %f\n\n", csa2);
printf("n_term approximation of cos^2(x)\n\n");
printf("Enter number of terms:\n");
scanf("%d", &n_terms);
printf("\n%d term approximation\n", n_terms);


}

so far everything shows up correctly. I''m just stuck on how to use the for loop to calculate the taylor series.

推荐答案

计算任何系列的值的伪代码都是这样的

The pseudo code to calculate the value of any series goes something like this

展开 | 选择 < span class =codeDivider> | Wrap | 行号


抱歉,我是编程新手

你能不能举一个如何做的简单例子?我需要定义多少个double / float值?
sorry, i''m new to programming
could you give like a simple example of how to do it? how many double / float values would i need to define?


保存总数和2个整数值的最小值为1的double值,1表示保存当前术语数,1表示保持要评估的术语数量。你可能需要更多的复杂术语或使计算更具可读性。


所以将**表示为(例如3 ** 2 = 9)的幂来评估系列


1 /(2 ** 0)+ 1 /(2 ** 1)+ 1 /(2 ** 2)+ 1 /(2 ** 3)+ 1 / (2 ** 4)+ ... + 1 /(2 ** n)


即2的权力的一些接收者(这倾向于值2)


代码看起来像

A minimum of 1 double value to hold the total and 2 integer values, 1 to hold the current term number and 1 to hold the number of terms to evaluate. You may need more for complex terms or to make the calculation more readable.

so taking ** to mean to the power of (e.g. 3 ** 2 = 9) to evaluate the series

1 / (2**0) + 1 / (2**1) + 1 / (2**2) + 1 / (2**3) + 1 / (2**4) + ... + 1 / (2**n)

i.e. the some of recipricals of the powers of 2 (this tends to the value 2)

the code could look something like

展开 | 选择 | Wrap | 行号


这篇关于使用泰勒系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆