20、泰勒展开式

泰勒展开式

1、求出e^x某一项小于0.001

  源程序代码如下:

/*
    2017年6月9日11:07:45
    功能:求出e^x某一项小于0.001
*/
#include "stdio.h"

#define M 0.001

double e_Toly(int i, int Num);                     //声明与定义函数的格式一致,两则仅有一个分号的区别                            

int main()
{
    int count = 1, num;
    double sum = 0, temp;
    printf("please input a number : ");
    scanf("%d",&num);

    while(1)
    {
        temp = e_Toly(count, num );

        if(temp > M)
        {
            sum += temp;
            count++;                        
        }
        else
        {
            sum += temp;
            break;
        }
    }
    printf("请输出求和的最后结果:%f
",sum);
    printf("请输出最后一项的值:%lf
",temp);    
    printf("请输出总共计算了%d 项",count);
    printf("
");

    return 0;
}
double e_Toly(int i, int Num )                  //输入的值,就是表达式中X元素
{                            
    double result = 1 , power = 1;              //power是进行幂运算的起始值,如果在一次中调用函数被多次调用,此时double result = 1 , power = 1;这样的定义需注意
    if(i == 1)
    return result;
    else if (i > 1)
    {
        for(int j = 1; j < i; j++)
        {
            result *= j; 
            power *= Num ;
        }
        result =power /result;
        return result;
    }
}
/*
    总结:
    在VC++6.0中显示的结果:
    ————————————————————————
    please input a number : 3
    请输出求和的最后结果:20.085469
    请输出最后一项的值:0.000256
    请输出总共计算了14 项

    ————————————————————————

*/

2、求出cosx某一项小于0.001 

   源程序代码如下:  

/*
    2017年6月10日06:52:21
    cosx的泰勒展开式某一项小于0.001
*/

#include "stdio.h"

#define M 0.001

double cosx_Toly(int i, double Num);
int main()
{
	int count = 1;
	double input_num;
	double sum = 0, temp;
	printf("please input a number: ");
	scanf("%lf",&input_num);

	while(true)
	{
		temp = cosx_Toly(count, input_num);

		if(temp > M)
		{
			if(count % 2 != 0)
			{
				sum += temp;
			}
			else
			{
				sum -= temp;
			}

			count++;
		}
		else
		{
			if(count % 2 != 0)
			{
				sum += temp;			//根据泰勒公式展开奇数项为正,偶数项为负
			}
			else
			{
				sum -= temp;
			}

			break;
		}
		
	}
	printf("please output the number of temp: %lf
",temp);
	printf("please output the number of sum: %lf
",sum);
	printf("please output the number of count: %d
",count);

	return 0;
}

double cosx_Toly(int i, double Num)
{
	int j;
	double result = 1, power = 1;
	for(j = 1; j <= (2*i-2); j++)								//注意此处的是<=,不是<
	{
		result *= j;
		power *= Num;
	}
	result = power / result ;
	return result;
}

/*
	总结:
	在vc++6.0中的结果显示:
	——————————————————————————————
	please input a number: 3.14
	please output the number of temp: 0.000104
	please output the number of sum: -1.000003
	please output the number of count: 8

	——————————————————————————————
*/

3、求出sinx某一项小于0.001

 1、源程序代码如下:

/*
	2017年6月10日06:24:46
	功能:求sinx的某一项小于0.001
*/
#include "stdio.h"

#define M 0.001

double sinx_Toly(int i, double Num);

int main()
{
	int count = 1;
	double input_num;
	double sum = 0, temp;
	printf("please input a number:");
	scanf("%lf",&input_num);

	while(true)
	{
		temp = sinx_Toly(count, input_num);

		if(temp > M)
		{
			if(count % 2 != 0)
			{
				sum += temp;
			}
			else
			{
				sum -= temp;
			}

			count++;
		}
		else
		{
			if(count % 2 != 0)
			{
				sum += temp;
			}
			else
			{
				sum -= temp;
			}

			break;
		}

	}
	printf("please output the number of temp: %lf
",temp);
	printf("please output the number of sum: %lf
",sum);
	printf("please output the number of count: %d
",count);
	return 0;
}

double sinx_Toly(int i, double Num)
{
	double power = 1;
	int j;
	double result = 1;  
	for(j = 1; j <= (2*i-1); j++)
	{
		power *= Num;
		result *= j; 
	}
	result = power / result;
	return result;
}
/*
	总结:
	在vc++6.0中显示的结果为:
	——————————————————————————————————
	please input a number:3
	please output the number of temp: 0.000256
	please output the number of sum: 0.141131
	please output the number of count: 7
	——————————————————————————————————
*/

 2、源程序代码如下:

/*
	2017年3月16日09:08:58
	功能:求sin(x)的最后一项的绝对值小于10^(-5),并统计项数。
*/

#include<stdio.h>
#include<math.h>
#define M 0.000001

void main()
{
	int x, i, k, m = -1, count = -1;
	double j, result = 0;

	printf("please input the number x = ");
	scanf("%d",&x);

	for(i = 1;fabs(j) >= M;i += 2)
	{	
		j = 1;
		m=-m;											  //控制多项式符号的
		for(k = 1; k <= i; k++)                           //这是算阶乘的
			j *= k;	
		j = m*pow(x,i)/j;                                 //这是计算单个多项式的结果的
		count++;
		result += j;									  //累加多个多项式
	}

	printf("count= %d
",count);						  //累计项数的次数
	printf("sin(x)= %lf
",result);						  //测试结果的,调用的 sin(x)函数

	
}
/*
	总结:
	在VC++6.0中显示的结果:
	—————————————————————————
	please input the number x = 3
	count= 8
	sin(x)= 0.141120
	—————————————————————————
*/

  

4、求出(1+x)^m某一项小于0.001

 源程序代码如下:

/*
	2017年6月10日08:01:06
	功能:求(1+x)^m的某一项小于0.001
*/
#include "stdio.h"

#define M 0.001

double M_Toly(int i, double Num, int m);

int main()
{
	int count = 1, n;
	double input_num;
	double sum = 0, temp;
	printf("please input a number:");
	scanf("%lf",&input_num);
	printf("please input a power_number:");
	scanf("%d",&n);

	while(true)
	{
		temp = M_Toly(count, input_num, n);

		if(temp > M)
		{
			
			sum += temp;
			count++;
		}
		else
		{
			sum += temp;
			break;
		}

	}
	printf("please output the number of temp: %lf
",temp);
	printf("please output the number of sum: %lf
",sum);
	printf("please output the number of count: %d
",count);
	return 0;
}

double M_Toly(int i, double Num, int m)
{
	double power = 1;
	int j, m_num = 1;
	double result = 1;  
	if(i == 1)
		return result;
	else if(i >1)
	{
		for(j = 1; j < i; j++)
		{
			result *= j;
			m_num *= (m-j+1);
			power *= Num;
		}
	}
	result =(power * m_num)/ result;
	return result;
}
/*
	总结:
	在vc++6.0中显示的结果为:
	——————————————————————————————————
	please input a number:2
	please input a power_number:3
	please output the number of temp: 0.000000
	please output the number of sum: 27.000000
	please output the number of count: 5

	——————————————————————————————————
*/

5、求出ln(1+x)某一项小于0.001

 源程序代码如下:

/*
	2017年6月10日09:11:06
	功能:求ln(1+x)的某一项小于0.001
*/
#include "stdio.h"

#define M 0.001

double ln_Toly(int i, double Num);

int main()
{
	int count = 1;
	double input_num;
	double sum = 0, temp;
	printf("please input a number:");
	scanf("%lf",&input_num);

	while(true)
	{
		temp = ln_Toly(count, input_num);

		if(temp > M)
		{
			
			if(count % 2 != 0)
			{
				sum += temp;
			}
			else
			{
				sum -= temp;
			}

			count++;
		}
		else
		{
			if(count % 2 != 0)
			{
				sum += temp;		//根据泰勒公式展开奇数项为正,偶数项为负
			}
			else
			{
				sum -= temp;
			}

			break;
		}

	}
	printf("please output the number of temp: %lf
",temp);
	printf("please output the number of sum: %lf
",sum);
	printf("please output the number of count: %d
",count);
	return 0;
}

double ln_Toly(int i, double Num)
{
	double power = 1;
	int j;
	double result = 1;  
	for(j = 1; j <= i; j++)
	{
		power *= Num;
	}
	result =power / j;
	return result;
}
/*
	总结:
	在vc++6.0中显示的结果为:
	——————————————————————————————————
	please input a number:0.5
	please output the number of temp: 0.000977
	please output the number of sum: 0.189369
	please output the number of count: 7
	——————————————————————————————————
*/

  

  

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注