您的位置:首页 > 其它

牛顿迭代法改造!!!!

2017-05-30 11:37 323 查看
参考:

百度百科   牛顿迭代法

迭代次数可以用控制精度代替,迭代次数无依据

// TestNewdun.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include "math.h"

#include "stdlib.h"

double func(double x) //函数

{

    return x*x*x*x-3*x*x*x+1.5*x*x-4.0;

}

double func1(double x) //导函数

{

    return 4*x*x*x-9*x*x+3*x;

}

int Newton(double *x,double precision,int maxcyc) //迭代次数

{

    double x1,x0;

    int k;

    x0=*x;

   for(k=0;k<maxcyc;k++) //可以 while(1)

    {

        if(func1(x0)==0.0)//若通过初值,函数返回值为0

        {

            printf("迭代过程中导数为0!\n");

            return 0;

        }

        x1=x0-func(x0)/func1(x0);//进行牛顿迭代计算

        if(fabs(x1-x0)<precision || fabs(func(x1))<precision)////达到结束条件

        {

            *x=x1; //返回结果

            return 1;

        }

        else //未达到结束条件

        {

            x0=x1; //准备下一次迭代

        }

    }

    printf("迭代次数超过预期!\n"); //迭代次数达到,仍没有达到精度

    return 0;

}

 

int main()

{

    double x,precision;

    int maxcyc;

    printf("输入初始迭代值x0:");

    scanf("%lf",&x);

    printf("输入最大迭代次数:");

    scanf("%d",&maxcyc);

    printf("迭代要求的精度:");

    scanf("%lf",&precision);

    if(Newton(&x,precision,maxcyc)==1) //若函数返回值为1

    {

        printf("该值附近的根为:%lf\n",x);

    }

    else //若函数返回值为0

    {

        printf("迭代失败!\n");

    }/**/
system("pause");

    return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: