Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // mo3.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include "pch.h"
- #include <iostream>
- #include <cmath>
- #include <tuple>
- #include <iomanip>
- const int MAX_IT = 30; // maksymalna ilosc iteracji
- const double TOL_X = 10e-10; //kryterium dokladnosci wyznaczenia Xn
- const double TOL_F = 10e-10; //kryterium wiarygodnosci Xn jako przyblizenia pierwiastka
- double fun1(double x) {
- return sin(x / 4) * sin(x / 4) - x;
- }
- double fun1pic(double x) {
- return sin(x / 4) * sin(x / 4);
- }
- double fun1prim(double x) {
- // 1/2*sin(x/4)*cos(x/4)-1
- return sin(x / 2) / 4 - 1;
- }
- double fun2(double x) {
- return tan(2 * x) - x - 1;
- }
- double fun2pic(double x) {
- return 2 / (cos(2 * x) * cos(2 * x));
- }
- double fun2pic2(double x) {
- return 1 / (2 * (x + 1) * (x + 1) + 2);
- }
- double fun2prim(double x) {
- return 2 / (cos(2 * x) * cos(2 * x)) - 1;
- }
- std::tuple<double, double> bisekcja(double(*fun)(double), double a,double b) {
- double rez, e, x, fx; // reziduum, estymator, x, f(x)
- double fa = fun(a); // f(a)
- double fb = fun(b); // f(b)
- std::cout << std::endl << "Bisekcja" << std::endl;
- if (fa * fb > 0) {
- std::cout << "f(a) i f(b) maja ten sam znak";
- return std::make_tuple(12345, 54321);
- }
- if (fa * fb == 0) {
- if (fa == 0) {
- std::cout << "Miejsce zerowe w x = " << a << std::endl;
- return std::make_tuple(a, fa);
- }
- if (fb == 0) {
- std::cout << "Miejsce zerowe w x = " << b << std::endl;
- return std::make_tuple(b, fb);
- }
- }
- std::cout << "IT" << std::setw(9) << "x:" << std::setw(29) << "f(x):" << std::setw(31) << "Estymator:" << std::setw(25) << "Reziduum:" << std::endl;
- std::cout << std::scientific << std::setprecision(16);
- for (int i = 1; i <= MAX_IT; i++) {
- x = (a + b) / 2;
- fx = fun(x);
- e = fabs(b - a) / 2;
- rez = fabs(fx);
- std::cout << std::setw(2) << std::right << i << ".\t" << std::right << std::setw(23) << x << " " << std::setw(23) << fx << " " << std::setw(23) << e << " " << std::setw(23) << rez << std::endl;
- if (e < TOL_X || rez < TOL_F) {
- break;
- }
- if (fa * fx <= 0) {
- b = x;
- fb = fx;
- }
- else {
- a = x;
- fa = fx;
- }
- }
- return std::make_tuple(x, fx);
- }
- std::tuple<double, double> picard(double(*fun)(double), double(*pic)(double), double x) {
- double fx, e, rez;
- std::cout << std::endl << "Picard" << std::endl;
- if (fabs(pic(x)) > 1) {
- std::cout << "Funkcja rozbiezna!" << std::endl;
- return std::make_tuple(12345, 54321);
- }
- std::cout << "IT" << std::setw(9) << "x:" << std::setw(29) << "f(x):" << std::setw(31) << "Estymator:" << std::setw(25) << "Reziduum:" << std::endl;
- std::cout << std::scientific << std::setprecision(16);
- for (int i = 1; i <= MAX_IT; i++) {
- fx = fun(x);
- e = fabs(x - fx);
- rez = fabs(fx);
- std::cout << std::setw(2) << std::right << i << ".\t" << std::right << std::setw(23) << x << " " << std::setw(23) << fx << " " << std::setw(23) << e << " " << std::setw(23) << rez << std::endl;
- if (e < TOL_X || rez < TOL_F) {
- break;
- }
- x = fx;
- }
- return std::make_tuple(x, fx);
- }
- std::tuple<double, double> newton(double(*fun)(double), double(*prim)(double), double xn) {
- double xn1, e, rez, fx, fpx;
- std::cout << std::endl << "Newton" << std::endl;
- std::cout << "IT" << std::setw(9) << "x:" << std::setw(29) << "f(x):" << std::setw(31) << "Estymator:" << std::setw(25) << "Reziduum:" << std::endl;
- std::cout << std::scientific << std::setprecision(16);
- for (int i = 1; i <= MAX_IT; i++) {
- fx = fun(xn);
- fpx = prim(xn);
- xn1 = xn - fx / fpx;
- e = fabs(xn - xn1);
- rez = fabs(fx);
- std::cout << std::setw(2) << std::right << i << ".\t" << std::right << std::setw(23) << xn << " " << std::setw(23) << fx << " " << std::setw(23) << e << " " << std::setw(23) << rez << std::endl;
- if (e < TOL_X || rez < TOL_F) {
- break;
- }
- xn = xn1;
- }
- return std::make_tuple(xn, fx);
- }
- std::tuple<double, double> sieczne(double(*fun)(double), double xn, double xn1) {
- double xn2, fxn2, e, rez;
- double fxn = fun(xn);
- double fxn1 = fun(xn1);
- if ((xn - xn1 == 0) || (fxn - fxn1 == 0)) {
- std::cout << "Bledne xn i xn1" << std::endl;
- return std::make_tuple(12345, 54321);
- }
- std::cout << std::endl << "Sieczne" << std::endl;
- std::cout << "IT" << std::setw(9) << "x:" << std::setw(29) << "f(x):" << std::setw(31) << "Estymator:" << std::setw(25) << "Reziduum:" << std::endl;
- for (int i = 1; i <= MAX_IT; i++) {
- fxn = fun(xn);
- fxn1 = fun(xn1);
- if ((xn - xn1 == 0) || (fxn - fxn1 == 0)) {
- std::cout << "Bledne xn i xn1" << std::endl;
- return std::make_tuple(12345, 54321);
- }
- xn2 = xn1 - fxn1 / ((fxn1 - fxn) / (xn1 - xn));
- fxn2 = fun(xn2);
- e = fabs(xn - xn1);
- rez = fabs(fxn2);
- std::cout << std::setw(2) << std::right << i << ".\t" << std::right << std::setw(23) << xn2 << " " << std::setw(23) << fxn2 << " " << std::setw(23) << e << " " << std::setw(23) << rez << std::endl;
- if (e < TOL_X || rez < TOL_F) {
- break;
- }
- xn = xn1;
- xn1 = xn2;
- }
- return std::make_tuple(xn2, fxn2);
- }
- int main()
- {
- std::cout << "Funkcja sin^2(x/4) - x = 0";
- std::tuple<double, double> f1_picard = picard(fun1, fun1pic, 1);
- std::tuple<double, double> f1_bisekcja = bisekcja(fun1, -1, 2);
- std::tuple<double, double> f1_newton = newton(fun1, fun1prim, 1);
- std::tuple<double, double> f1_sieczne = sieczne(fun1, 1, 3);
- std::cout << std::endl << "Obliczone x0:" << std::endl;
- std::cout << "Metoda Picarda:\t\t" << "x = " << std::get<0>(f1_picard) << "\tf(x) = " << std::setw(23) << std::get<1>(f1_picard) << std::endl;
- std::cout << "Metoda bisekcji:\t" << "x = " << std::get<0>(f1_bisekcja) << "\tf(x) = " << std::setw(23) << std::get<1>(f1_bisekcja) << std::endl;
- std::cout << "Metoda Newtona:\t\t" << "x = " << std::get<0>(f1_newton) << "\tf(x) = " << std::setw(23) << std::get<1>(f1_newton) << std::endl;
- std::cout << "Metoda siecznych:\t" << "x = " << std::get<0>(f1_sieczne) << "\tf(x) = " << std::setw(23) << std::get<1>(f1_sieczne) << std::endl;
- std::cout << "Funkcja tg(2x) - x - 1 = 0";
- std::tuple<double, double> f2_picard = picard(fun2, fun2pic2, 1);
- std::tuple<double, double> f2_bisekcja = bisekcja(fun2, -1, 2);
- std::tuple<double, double> f2_newton = newton(fun2, fun2prim, 1);
- std::tuple<double, double> f2_sieczne = sieczne(fun2, 1, 3);
- std::cout << std::endl << "Obliczone x0:" << std::endl;
- std::cout << "Metoda Picarda:\t\t" << "x = " << std::get<0>(f2_picard) << "\tf(x) = " << std::setw(23) << std::get<1>(f2_picard) << std::endl;
- std::cout << "Metoda bisekcji:\t" << "x = " << std::get<0>(f2_bisekcja) << "\tf(x) = " << std::setw(23) << std::get<1>(f2_bisekcja) << std::endl;
- std::cout << "Metoda Newtona:\t\t" << "x = " << std::get<0>(f2_newton) << "\tf(x) = " << std::setw(23) << std::get<1>(f2_newton) << std::endl;
- std::cout << "Metoda siecznych:\t" << "x = " << std::get<0>(f2_sieczne) << "\tf(x) = " << std::setw(23) << std::get<1>(f2_sieczne) << std::endl;
- }
- // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
- // Debug program: F5 or Debug > Start Debugging menu
- // Tips for Getting Started:
- // 1. Use the Solution Explorer window to add/manage files
- // 2. Use the Team Explorer window to connect to source control
- // 3. Use the Output window to see build output and other messages
- // 4. Use the Error List window to view errors
- // 5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
- // 6. In the future, to open this project again, go to File > Open > Project and select the .sln file
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement