Let \( y = \frac{x^2+x+a}{x^2-x+a} \).
The given inequality is \( \frac{1}{2} \leq y \leq 2 \) for all \( x \in \mathbb{R} \).
First, consider the denominator \( D(x) = x^2 - x + a \). For \(y\) to be defined for all real \(x\), the denominator must never be zero. Also, for the sign of the denominator to be consistent, it must always be positive or always negative. Since the coefficient of \(x^2\) is 1 (positive), the parabola opens upwards. Thus, for \( D(x) \) to be always positive, its discriminant must be less than zero.
Discriminant of \( x^2 - x + a \) is \( (-1)^2 - 4(1)(a) = 1 - 4a \).
So, \( 1 - 4a<0 \implies 1<4a \implies a>\frac{1}{4} \).
Now, let's work with the given inequalities:
\( \frac{1}{2} \leq \frac{x^2+x+a}{x^2-x+a} \)
Since \( x^2-x+a>0 \) (from \(a>\frac{1}{4}\)), we can multiply by it without changing the inequality direction.
\( \frac{1}{2}(x^2-x+a) \leq x^2+x+a \)
\( x^2-x+a \leq 2(x^2+x+a) \)
\( x^2-x+a \leq 2x^2+2x+2a \)
\( 0 \leq 2x^2 - x^2 + 2x + x + 2a - a \)
\( 0 \leq x^2 + 3x + a \)
This quadratic \( x^2 + 3x + a \) must be greater than or equal to 0 for all \( x \in \mathbb{R} \).
For a quadratic \( Ax^2+Bx+C \) to be \( \geq 0 \) for all \( x \), we must have \( A>0 \) (which is true, \(1>0\)) and its discriminant must be \( \leq 0 \).
Discriminant of \( x^2 + 3x + a \) is \( (3)^2 - 4(1)(a) = 9 - 4a \).
So, \( 9 - 4a \leq 0 \implies 9 \leq 4a \implies a \geq \frac{9}{4} \).
Next, consider the second part of the inequality:
\( \frac{x^2+x+a}{x^2-x+a} \leq 2 \)
Again, multiply by \( x^2-x+a \) (which is positive):
\( x^2+x+a \leq 2(x^2-x+a) \)
\( x^2+x+a \leq 2x^2-2x+2a \)
\( 0 \leq 2x^2 - x^2 - 2x - x + 2a - a \)
\( 0 \leq x^2 - 3x + a \)
This quadratic \( x^2 - 3x + a \) must be greater than or equal to 0 for all \( x \in \mathbb{R} \).
For this, its discriminant must be \( \leq 0 \).
Discriminant of \( x^2 - 3x + a \) is \( (-3)^2 - 4(1)(a) = 9 - 4a \).
So, \( 9 - 4a \leq 0 \implies 9 \leq 4a \implies a \geq \frac{9}{4} \).
Both inequalities and the condition for the denominator give us \( a>\frac{1}{4} \) and \( a \geq \frac{9}{4} \).
The intersection of these conditions is \( a \geq \frac{9}{4} \).
However, the problem states that the inequality holds for a specific value of \( a \). This implies that there might be a unique value of \( a \) for which both bounds are precisely met.
The function \( y = f(x) \) can be written as \( y(x^2-x+a) = x^2+x+a \).
\( x^2(y-1) - x(y+1) + a(y-1) = 0 \).
For \(x\) to be real, the discriminant of this quadratic in \(x\) must be non-negative.
Discriminant \( D_x = (-(y+1))^2 - 4(y-1)(a(y-1)) \geq 0 \)
\( (y+1)^2 - 4a(y-1)^2 \geq 0 \)
\( y^2+2y+1 - 4a(y^2-2y+1) \geq 0 \)
\( y^2+2y+1 - 4ay^2+8ay-4a \geq 0 \)
\( (1-4a)y^2 + (2+8a)y + (1-4a) \geq 0 \)
Let \( Q(y) = (1-4a)y^2 + (2+8a)y + (1-4a) \).
This quadratic in \( y \) must be non-negative for \( y \in \left[\frac{1}{2}, 2\right] \).
Since \( a \geq \frac{9}{4} \), \( 1-4a \) will be negative.
If \( 1-4a<0 \), then the parabola \( Q(y) \) opens downwards. For \( Q(y) \geq 0 \) in an interval, it must be that the roots of \( Q(y) = 0 \) are exactly \( \frac{1}{2} \) and \( 2 \).
If the roots are \( \frac{1}{2} \) and \( 2 \), then \( Q(y) \) can be written as \( C(y-\frac{1}{2})(y-2) \) where \( C \) is a negative constant.
So, \( (1-4a)y^2 + (2+8a)y + (1-4a) = (1-4a)(y-\frac{1}{2})(y-2) \)
\( = (1-4a)(y^2 - 2y - \frac{1}{2}y + 1) \)
\( = (1-4a)(y^2 - \frac{5}{2}y + 1) \)
Comparing coefficients:
Coefficient of \( y^2 \): \( 1-4a \) (matches)
Coefficient of \( y \): \( 2+8a = (1-4a)(-\frac{5}{2}) \)
\( 2+8a = -\frac{5}{2} + 10a \)
\( 2 + \frac{5}{2} = 10a - 8a \)
\( \frac{4+5}{2} = 2a \)
\( \frac{9}{2} = 2a \)
\( a = \frac{9}{4} \)
Constant term: \( 1-4a = (1-4a)(1) \) (matches)
Let's check if \( a = \frac{9}{4} \) satisfies \( 1-4a<0 \):
\( 1 - 4\left(\frac{9}{4}\right) = 1 - 9 = -8 \), which is indeed negative.
Thus, the value of \( a \) must be \( \frac{9}{4} \).