newton.cpp
来自「Topics Practices: Programming and Nume」· C++ 代码 · 共 36 行
CPP
36 行
//EJEMPLO DE NEWTON DE LA CLASE
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return 3*x+sin(x)-exp(x);//return x*x-2;
}
float d_f(float x)
{
return 3+cos(x)-exp(x);//return 2*x;
}
main()
{
int iter_max,i;
float x_0,x,x_prev,tol;
printf("Ingrese el valor inicial \n");
scanf("%f", &x_0);
printf("Ingrese el valor de la tolerancia \n");
scanf("%f", &tol);
iter_max=1000;
x=x_0;
x_prev=100*x_0;
i=0;
while(fabs(x-x_prev)>tol && i<=iter_max)
{
x_prev=x;
x=x_prev-f(x_prev)/d_f(x_prev);
printf("x=%f\n", x);
++i;
}
printf("La estimacion del valor de la raiz es %f \n", x);
printf("La funcion en ese punto vale %f", f(x));
getch();
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?