I started to learn C ++ recently and the skills are not very big yet, please help with the task, no matter how it is solved:

Find the root of the equation y = ax ^ 2sinx + b, provided that the root is unique and lies on the interval [x1, x2]. To find use the method of dividing the segment in half. The accuracy of the calculation is given by the value ε> 0 which determines the length of the interval containing the root.

Closed due to the fact that off-topic by pavel participants, Vladimir Martyanov , Nicolas Chabanovsky 29 Sep '16 at 5:09 .

  • Most likely, this question does not correspond to the subject of Stack Overflow in Russian, according to the rules described in the certificate .
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • You definitely need to familiarize yourself with the part of the standard library math - Malov Vladimir
  • According to community rules, questions should not be reduced to completing tasks for students. Give an example of your implementation and ask a question describing specific problems. - Nicolas Chabanovsky

1 answer 1

See - we have the equation y(x) = 0 . It is necessary to define this function.

 double y(double x) { // Тело напишите сами } 

We have a segment on which we are looking. So? So there is

 double a,b; 

(don't forget to initialize!). Next, we need to know the signs of the function values ​​at these points:

 double ya = y(a), yb = y(b); 

It would be nice to check that they have different signs ...

Further we will work in a cycle, narrowing the segment. Those. until we get |ab| < eps |ab| < eps , or since we always have b > a , just ba < eps . Those.

 while(b - a > eps) { 

And what is there? Delhi in half:

 double x = (a + b)/2; 

and find the value of the function:

 double f = y(x); 

Now let's see ... if it is of the same sign as a , we move a , if b is, respectively.

 if (ya * f >= 0) a = x; else b = x; 

This cycle ends ...

And now collect all this in a heap, add what is missing, achieve that it is compiled, debug it (I could have missed a mistake ... :)) and carry it.

PS You did not really hope to get a finished job? :)

  • Thank you very much, no, of course, this is quite enough to understand where to go and how the code should work - Valery