- #include<iostream>
- #include<stdio.h>
- #include<math.h>
- #include <stdlib.h>
- #include<iomanip>
- #include<fstream>
- #include <stdlib.h>
- #define MAX_ITERATION 10000
- #define EPS 1e-16
- using namespace std;
- long double f1(long double x) {
- //return sin(x/4)*sin(x/4) - x;
- //return x*x*x +/4*x - 20;
- //return exp(5)*x*x*x +20;
- }
- long double f2(long double x) {
- return tan(2*x)-x-1;
- //return (x-1)*x*x +8*x;
- }
- long double isConcurrent(long double(*f)(long double), long double x) {
- if (((f(x + EPS) - f(x)) / EPS ) > 1)
- return false;
- else
- return true;
- }
- long double derivative(long double(*f)(long double), long double x) {
- return (f(x + EPS) - f(x)) / EPS;
- }
- long double pickard_f1(long double x) {
- return sin(x/4)*sin(x/4);
- }
- long double pickard_f2(long double x) {
- return atan(x+1)/2;
- }
- string show_function1() {
- return "sin^2(x/4)- x = 0";
- }
- string show_function2() {
- return "tan(2x)- x - 1 = 0";
- }
- void writeTitleToFile(ofstream &file) {
- file << setw(5) << "Lp." << setw(20) << right << "Przyblizenie Xn" << setw(20) << right <<
- "Estymator " << setw(20) << right << "Residuum" << endl << endl;
- }
- void writeResultToFile(ofstream &file, int i, double result, double estimator, double residuum) {
- file.width(5);
- file << i <<".";
- file.width(20);
- file << right << result;
- file.width(20);
- file << right << estimator;
- file.width(20);
- file << right << residuum << endl;
- }
- double Pickard_function(long double (*f)(long double), long double (*pickard_f)(long double), long double x0, ofstream &file) {
- long double estimator = 0;
- long double residuum = 0;
- long double x;
- writeTitleToFile(file);
- for(int i = 1; i< MAX_ITERATION; i++) {
- x = pickard_f(x0);
- residuum = fabs(f(x0));
- estimator = fabs(x - x0);
- x0 = x;
- writeResultToFile(file, i, x, estimator, residuum);
- if(residuum < EPS) {
- break;
- }
- if(estimator < EPS) {
- break;
- }
- }
- return x;
- }
- double Bisekcja_function(long double (*f)(long double), long double a, long double b, ofstream &file) {
- long double estimator = 0;
- long double residuum = 0;
- long double x;
- writeTitleToFile(file);
- for(int i = 1; i< MAX_ITERATION; i++) {
- x = (a+b)/2;
- estimator = fabs((b-a)/2);
- residuum = fabs(f(x));
- writeResultToFile(file, i, x, estimator, residuum);
- if(residuum < EPS) {
- break;
- }
- if(estimator < EPS) {
- break;
- }
- if(f(a)*f(x)<0)
- b = x;
- else
- a = x;
- }
- return x;
- }
- double Newton_function(long double (*f)(long double), long double x0, ofstream &file){
- long double estimator = 0;
- long double residuum = 0;
- long double x;
- writeTitleToFile(file);
- for(int i = 1; i<MAX_ITERATION; i++) {
- x = x0 - f(x0)/derivative(f,x0);
- estimator = fabs(x0 - x);
- residuum = fabs(f(x));
- x0 = x;
- writeResultToFile(file, i, x, estimator, residuum);
- if(residuum < EPS) {
- break;
- }
- if(estimator < EPS) {
- break;
- }
- }
- return x;
- }
- double Sieczna_function(long double (*f)(long double), long double a, long double b, ofstream &file) {
- long double estimator = 0;
- long double residuum = 0;
- long double x;
- writeTitleToFile(file);
- for(int i = 1; i<MAX_ITERATION; i++) {
- double tmp = b;
- b = b - f(b)*(b-a)/(f(b) - f(a));
- a = tmp ;
- estimator = fabs(a-b);
- residuum = fabs(f(b));
- writeResultToFile(file, i, x, estimator, residuum);
- if(residuum < EPS) {
- break;
- }
- if(estimator < EPS) {
- break;
- }
- }
- return a;
- }
- int main()
- {
- cout << setprecision(10);
- cout << setw(20) <<"========== " << show_function1() << " ==========" << endl << endl;
- ofstream Pickard1;
- Pickard1.open("pickard1.txt", ios::out);
- double arg_p1 = -8;
- if(isConcurrent(pickard_f1, arg_p1)) {
- cout << setw(20) << "Pickard " << setw(5) << "x0 = " << Pickard_function(f1, pickard_f1, arg_p1, Pickard1) << endl;
- } else {
- cout << "Nie można policzyć Pickarda. Funkcja rozbiezna!" << endl;
- }
- ofstream Bisekcja1;
- Bisekcja1.open("bisekcja1.txt", ios::out);
- double arg_b1 = -2;
- double arg_b2 = 4;
- if(f1(arg_b1)*f1(arg_b2)> 0 || arg_b1> arg_b2) {
- cout << "Nie można policzyć bisekcji. Podano zly przedzial!" << endl;
- } else {
- cout << setw(20) << "Bisekcja " << setw(5) << "x0 = " << Bisekcja_function(f1, arg_b1, arg_b2, Bisekcja1)<< endl;
- }
- ofstream Newton1;
- double arg_n1 = 8;
- Newton1.open("newton1.txt", ios::out);
- cout << setw(20) << "Newton " << setw(5) << "x0 = " << Newton_function(f1, arg_n1, Newton1) << endl;
- ofstream Sieczna1;
- double arg_s1 = -4;
- double arg_s2 = -2;
- Sieczna1.open("sieczna1.txt", ios::out);
- cout << setw(20) << "Sieczna " << setw(5) << "x0 = " <<Sieczna_function(f1, arg_s1, arg_s2, Sieczna1) << endl;
- /****************************************************************************************************************/
- /****************************************************************************************************************/
- cout << endl;
- cout << setw(20) <<"========== " << show_function2() << " ==========" << endl << endl;
- ofstream Pickard2;
- Pickard2.open("pickard2.txt", ios::out);
- double arg_p2 = -8;
- if(isConcurrent(pickard_f2, arg_p2)) {
- cout << setw(20) << "Pickard " << setw(5) << "x0 = " << Pickard_function(f2, pickard_f2, arg_p2, Pickard2) << endl;
- } else {
- cout << "Nie można policzyć Pickarda. Funkcja rozbiezna!" << endl;
- }
- ofstream Bisekcja2;
- Bisekcja2.open("bisekcja2.txt", ios::out);
- double arg_b21 = 0;
- double arg_b22 = 0.6;
- if(f2(arg_b21)*f2(arg_b22)> 0 || arg_b21> arg_b22) {
- cout << "Nie można policzyć bisekcji. Podano zly przedzial!" << endl;
- } else {
- cout << setw(20) << "Bisekcja " << setw(5) << "x0 = " << Bisekcja_function(f2, arg_b21, arg_b22, Bisekcja2)<< endl;
- }
- ofstream Newton2;
- double arg_n2 = 8;
- Newton2.open("newton2.txt", ios::out);
- cout << setw(20) << "Newton " << setw(5) << "x0 = " << Newton_function(f2, arg_n2, Newton2) << endl;
- ofstream Sieczna2;
- double arg_s21 = -5;
- double arg_s22 = 18;
- Sieczna2.open("sieczna2.txt", ios::out);
- cout << setw(20) << "Sieczna " << setw(5) << "x0 = " <<Sieczna_function(f2, arg_s21, arg_s22, Sieczna2) << endl;
- return 0;
- }