Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <cmath>
- using namespace std;
- float f(float x)
- {
- return x * x * x - 71;
- }
- float f_prim(float x)
- {
- return 3 * x * x;
- }
- void metodaBisekcji(float a, float b, float s, float ap)
- {
- int i = 0;
- if (f(a) * f(b) < 0)
- {
- while (abs(f(s)) > ap)
- {
- s = (a + b) / (float)2;
- //cout << a << endl;
- //cout << b << endl;
- //cout << s << endl;
- //cout << f(a) * f(s) << endl;
- if (f(a) * f(s) < 0) b = s;
- else a = s;
- i ++;
- }
- }
- cout << endl << "Rozwiazanie metoda bisekcji jest rowne: "
- << (a + b) / (float)2 << endl
- << "Wartosc funkcji: " << f((a + b) / (float)2) << endl
- << "Ilosc iteracji: " << i << endl << endl;
- }
- void metodaNewtona(float a, float b, float s, float x0, float ap)
- {
- float x = 1;
- int i = 0;
- if (f_prim(x0) != 0)
- {
- while (abs(f(x0) > ap))
- {
- x = x0 - f(x0) / f_prim(x0);
- x0 = x;
- i ++;
- }
- }
- cout << "Rozwiazanie metoda Newtona jest rowne: " << x << endl
- << "Wartosc funkcji: " << f(x) << endl
- << "Ilosc iteracji: " << i << endl;
- }
- int main()
- {
- float a = 4;
- float b = 5;
- float x0 = 5;
- float s = 1;
- float ap = 0;
- cout << "Wprowadz dokladnosc: ";
- cin >> ap;
- // x^3 - 71 = 0
- metodaBisekcji(a, b, s, ap);
- metodaNewtona(a,b, s, x0, ap);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement