Here's the code that increases x value by 0.01 increment and outputs: x, placeholder for initial x+t and how many loop cycles it took to find a solution to the equation in function f1() in intervals for temp (0.5 ; 1.0)U(1.0 ; 1.5).
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <iomanip>
using namespace std;
double f1(double z){
return pow(z,4)+3*pow(z,3)+pow(z,2)-3*z-2;
}
int main()
{
double temp = 0.0;
double init = 0.104;
double x = 0.0;
int k = 0;
double i = 0.505 - init;
double eps = 0.001;
while ( (init + i) < 1.5 )
{
x = init + i;
if(x != 1)
{
k=0;
while (fabs(f1(x)) > eps)
{
x=4*x*(1-x);
k++;
}
temp = init + i;
cout << setprecision(4) << x << " " << temp<< " " << k << endl;
}
i += 0.01;
}
return 0;
}
But the problem is - as soon as initial x+t hits the value of 1.0 square iterator loop messes :
while (fabs(f1(x)) > eps)
{
x=4*x*(1-x);
k++;
}
it all up and x becomes -inf, cause x value can't be 1 according to the rules. i'm not allowed to change x=4*x*(1-x) formula. Need the program to output x, temp, and k in the intervals stated above. I understand that i somehow have to adjust this part (fabs(f1(x)) > eps) and then adjust the intervals, but i'm unable to come up with a proper solution.
Thanx in advance!