Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Методы решения нелинейных уравнений:
- 1. Ньютона
- 2. Бисекций
- 3. Ппростых итераций
- */
- #include <iostream>
- #include <cmath>
- #include <cstdio>
- using namespace std;
- // ln(x+1)-2x^2+1=0
- // ln(x+1)-2x^2+x+1=x
- // ln(x+1)-(x-1)(2x+1) = x
- // 2x^2-x-1=0
- // D=1+8 = 9 = 3^2
- // x1=(1+3)/4 = 1
- // x2=(1-3)/4 = -1/2
- // 2x^2-x-1=2(x-1)(x+1/2)=(x-1)(2x+1)
- // 0.5*(log(x+1)+1)/x=x
- inline double f(double x) {
- // f(x) = 0
- return log(x+1)-2*x*x+1;
- }
- inline double df(double x) {
- // f(x) = 0
- return 1/fabs(x+1)-4*x;
- }
- inline double g(double x) {
- // x = g(x)
- return 0.5*(log(x+1)+1)/x;
- }
- double solve_bisection(double (*f)(double x), double a, double b, double eps, int limit) {
- cout << "\tMethod of bisections:" << endl;
- double x = 0.5*(a+b);
- int count = 0;
- while ( fabs( (*f)(x) ) > eps && count < limit) {
- printf("%d:\t x = %0.9lf \t f(x) = %0.9lf\n", count, x, (*f)(x));
- if ( (*f)(a)*(*f)(x) < 0 )
- b = x;
- else
- a = x;
- x = 0.5*(a+b);
- ++count;
- }
- cout << endl;
- return x;
- }
- double solve_iteration(double (*f)(double x), double eps, int limit) {
- cout << "\tMethod of simple iterations:" << endl;
- double x = 1, y = (*f)(x);
- int count = 0;
- while (fabs(x - y) >= eps && count < limit) {
- printf("%d:\t x = %0.9lf \t f(x) = %0.9lf\n", count, x, x-(*f)(x));
- x = y;
- y = (*f)(x);
- count++;
- }
- cout << endl;
- return x;
- }
- double solve_neuton(double (*f)(double x), double (*df)(double x), double eps, double limit) {
- cout << "\tMethod of Neuton:" << endl;
- double x = 10, y = (*f)(x)/(*df)(x);
- int count = 0;
- while (fabs((*f)(x)) >= eps && count < limit) {
- printf("%d:\t x = %0.9lf \t f(x) = %0.9lf\n", count, x, (*f)(x));
- x -= y;
- y = (*f)(x)/(*df)(x);
- count++;
- }
- cout << endl;
- return x;
- }
- int main() {
- double eps = 10e-9;
- solve_bisection(f, 0.1, 10, eps, 1000);
- solve_iteration(g, eps, 1000);
- solve_neuton(f, df, eps, 1000);
- return 0;
- }
Add Comment
Please, Sign In to add comment