no.5.c

来自「中南大学C语言程序设计实习 1 实验一:C语言图形模式的设置 2 实习」· C语言 代码 · 共 52 行

C
52
字号
#include "stdio.h"
#include "conio.h"
#define MIN -30000
#define ROOT_PRECISION 10e-5

float func1(float x, float a)
{
    return (x*x*x - a);
}

float func1_1(float x, float a)
{
    return (3*x*x);
} 

float func2(float x, float c)
{
    return (1 - c*x);
}

float func2_1(float x, float c)
{
    return (-c);
}           

float newton(float (*f)(float x, float a), float (*f_1)(float x, float c), float a, float x0)
{
    float x, ox;
    ox = x = x0;
    
    do {
        ox = x;
        x = x - f(x, a) / f_1(x, a);
    }while (x - ox > ROOT_PRECISION || ox - x > ROOT_PRECISION);
    return ox;
}

int main()
{
    float x;
    x = newton(func1, func1_1, 10, MIN);
    printf("%.5f\n", x);
    x = newton(func2, func2_1, 0.324, 3);
    printf("%.5f\n", x);
    getch();
}        
    

    
                        
        

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?