C言語によるNewton法
筑波大学農林工学系 奈佐原(西田)顕郎
Newton法のプログラム
前にやったように、代数方程式f(x)=0を解くには、Newton法を使えば良い。では、Newton法をC言語のプログラムにしてみよう。
/* Newton.c * 2007/02/01 K. Nishida * solve an equation with Newton method * usage: put your favorite function in the "return()" in the "func" section. * compile: gcc Newton.c -o Newton */ # include<stdio.h> double func(double x){ return (x*x*x+4*x*x-6); } main(){ double x; double f; // f(x) double df; // df/dx double dx=0.00001; int i; printf("Give me the initial value: "); scanf("%lf", &x); for (i=0; i<5; i++){ f=func(x); df=(func(x+dx)-func(x-dx))/(2*dx); x=x-f/df; printf("%.14f\n", x); } }
課題: 上のプログラムを改造して、2の平方根と2の立方根を求めてみよ。
Keyword(s):
References:[数値解析入門]