Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- double f(double x)
- {
- return tan(x) - 1;
- }
- double solve_bis(double a, double b, double eps, double c)
- {
- double q;
- while (abs(a - b) > eps)
- {
- q =(a + b) / 2;
- if ((f(a) - c) * (f(q) - c) < 0)
- b = q;
- else
- a = q;
- }
- return q;
- }
- double solve_chord(double a, double b, double eps, double c)
- {
- double fa, fb, fc, k, l, c1, c2;
- c1 = f(a);
- c2 = f(b);
- while (abs(c2 - c1) > eps) {
- c2 = c1;
- fa = f(a) - c;
- fb = f(b) - c;
- k = (fa - fb) / (a - b);
- l = fa - k * a; // b
- c1 = -l/k;
- fc = f(c1) - c;
- if ((fa * fc) <= 0)
- b = c1;
- if ((fb * fc) <= 0)
- a = c1;
- }
- return c1;
- }
- int solve(int method, double a, double b, double eps, double c)
- {
- if ((f(a) - c)*(f(b) - c) < 0)
- {
- if (method == 1)
- {
- printf("%.6f\n", solve_bis( a, b, eps, c));
- return 0;
- }
- else if (method == 2)
- {
- printf("%.6f\n", solve_chord(a, b, eps, c));
- return 0;
- }
- else if (method == 3)
- {
- printf("%.6f\n", solve_bis( a, b, eps, c));
- printf("%.6f\n",solve_chord(a, b, eps, c));
- return 0;
- }
- }
- return 1;
- }
- int main()
- {
- double c;
- double a, b;
- double eps;
- int method;
- scanf ("%d%lf%lf%lf%lf", &method, &a, &b, &eps, &c);
- solve(method, a, b, eps, c);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement