Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* sqrt 2
- #include <iostream>
- #include <cstdlib>
- #define R_DEPTH_MAX 50
- double f(double x)
- {
- // d --> glebokosc rekurencji
- static int d = 0;
- d++;
- if (d >= R_DEPTH_MAX) return 0;
- return 1 / (2 + f(x));
- }
- int main()
- {
- double sqrt2 = f(1.0);
- std::cout << "zakonczono liczenie: " << std::endl;
- std::cout << sqrt2;
- system("pause");
- return 0;
- }
- */
- #include <iostream>
- #include <cstdlib>
- #define R_DEPTH_MAX 40
- typedef double(*function_ptr)(double);
- double f2(double x)
- {
- return sin(x);
- }
- double f(double x)
- {
- return 0.5 * exp(x) - 5*x + 2;
- }
- double bisection(double A, double B, function_ptr f)
- {
- double midpoint = (A + B) / 2;
- // d --> glebokosc rekurencji
- static int d = 0;
- d++;
- if (d >= R_DEPTH_MAX) return midpoint;
- if (f(A)*f(midpoint) >= 0)
- {
- A = midpoint;
- }
- if (f(B)*f(midpoint) >= 0)
- {
- B = midpoint;
- }
- return bisection(A, B, f);
- }
- int main()
- {
- double x0 = bisection(2, 4, f2);
- std::cout << "zakonczono liczenie: " << std::endl;
- std::cout << x0 << std::endl;
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement