Answer the question
In order to leave comments, you need to log in
What to do if the program gives an error when entering the double function?
#include <iostream>
#include <stdio.h>
#include <math.h>
using namespace std;
//Функция для полинома:
double F(double x) {
return 5 * tan(x) - (10 / (x) ^ 2) - 3;
}
//Функция поиска корня:
double FindRoot(double (*f)(double), double a, double b, double eps) {
double c;
while ((b - a) / 2 > eps) {
c = (a + b) / 2;
if ((f(a) * f(c)) > 0) a = c;
else b = c;
}
return c;
}
int main() {
//Интервал, погрешность и корень:
double a, b, eps, x;
cout << "interval: ";
cin >> a;
cin >> b;
//Проверка корректности интервала:
if (F(a) * F(b) > 0) {
cout << "Wrong interval!\n";
return 0;
}
cout << "error: ";
cin >> eps;
//Поиск решения:
x = FindRoot(F, a, b, eps);
cout << "x = " << x << endl;
return 0;
}
Answer the question
In order to leave comments, you need to log in
It would be nice to write in which line the error is (and for starters, format the code normally - there is a button in the editor).
Apparently, the problem here return 5 * tan(x) - (10 / (x) ^ 2) - 3;
(x) ^ 2
is not exponentiation, but XOR (exclusive OR). Squaring like this: x*x
for example
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question