This is a small starter task in the Terminal-Bench style: implement a script that scans a function on a fixed domain and reports where a sign change occurs between consecutive samples.
Task description:
Create a script that samples f(x) on the domain [-5, 5] with a step of 0.5.
For every consecutive pair (a, b) in the sample grid, compute f(a) and f(b). If f(a) * f(b) <= 0, print the interval [a, b] as a potential root region.
Notes:
You may choose any reasonable function f. For clarity provide a default example: f(x) = x*x - 2.
The script should be self-contained and require only the Python standard library.
Endpoints should be included, i.e., -5 and 5 are evaluated.
Deliverables:
A single script file sign_change_intervals.py that prints the list of intervals to stdout.
Optional: a short README describing how to run it and what the output means.
Implementation tips:
Define f(x) as a Python function.
Build a list of sample points from -5.0 to 5.0 inclusive with step 0.5.
Iterate i from 0 to n-2, with a = xs[i], b = xs[i+1], y0 = f(a), y1 = f(b); if y0 * y1 <= 0, print the interval.
Run: python3 sign_change_intervals.py
Example (using f(x) = x*x - 2):
On domain [-5, 5] with step 0.5, the script should print intervals around the two real roots near x ≈ -1.414 and x ≈ 1.414, e.g.:
Interval [-1.5, -1.0]
Interval [1.0, 1.5]
Code sketch (for illustration; implementers may adapt):
text
def f(x):
return x*x - 2
def main():
a, b, step = -5.0, 5.0, 0.5
xs = [a + i*step for i in range(int((b - a)/step) + 1)]
for i in range(len(xs) - 1):
x0, x1 = xs[i], xs[i+1]
y0, y1 = f(x0), f(x1)
if y0 * y1 <= 0:
print('Interval [{} , {}]'.format(x0, x1))
if __name__ == '__main__':
main()