Newton迭代,割线法
程序员文章站
2022-06-07 09:46:46
...
#include<stdio.h>
#include<math.h>
double F(double x)
{
return cos(x) - x;
}
double F_(double x)
{
return -1 - sin(x);
}
double Newton(double x0,double e,int N)
{
double x;
int k = 1;
do
{
if (F_(x0) == 0)
break;
else
x = x0 - (F(x0) / F_(x0));
if (fabs(x - x0) < e || fabs(F(x)) < e)
return x;
else
x0 = x;
k++;
} while (k <= N);
return -1;
}
int main()
{
int N = 100;
double x0 = 0, e = 0.00001;
x0 = Newton(x0, e, N);
printf("%lf", x0);
getchar();
}
#include<stdio.h>
#include<math.h>
double F(double x)
{
return cos(x) - x;
}
double X(double x_k1, double x_k0)
{
return x_k1 - F(x_k1)*((x_k1 - x_k0) / (F(x_k1) - F(x_k0)));
}
double Secant(double x0, double x1, double e)
{
double temp;
while (fabs(x0 - x1) > e)
{
temp = X(x1, x0);
x0 = x1;
x1 = temp;
}
return x1;
}
int main()
{
double x0 = 0, x1 = 1, e = 0.0000001;
x1 = Secant(x0, x1, e);
printf("%lf", x1);
getchar();
}