I don't like "questions" where the OP explain absolutely nothing about the problem, just throw some code and ask "What do you think?" (or something similar). To me, these questions imply: "think whatever you want about this code, but post some answer", so the answer may include anything I wish. Well, this is my answer:
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "f(x)=x*7-77"
cls
echo f(x) = %f(x)%
echo ------------------
set /A "x=1, y0=%f(x)%"
for /L %%x in (1,1,100) do (
set /A "x=%%x, y1=%f(x)%, sign1=y0*y1, sign2=sign1>>31, y0=y1"
REM echo f(%%x^) = !y1!
if !sign1! equ 0 (
echo Exact root found: %%x
set y0=1
) else if !sign2! equ -1 (
echo Root exist between %%x and previous X
)
)
EDIT: Another version:
Code: Select all
@echo off
setlocal EnableDelayedExpansion
cls
echo Enter polynomial coefficients (i.e. 1 3 -5 -10 for x^^3 + 3*x^^2 - 5*x - 10)
set /P "coef="
set /P "range=Enter range for X: "
for /F "tokens=1,2" %%a in ("%range%") do set /A minX=%%a, maxX=%%b
echo/
(
set "coef="
for %%a in (%coef%) do set "coef=%%a !coef!"
)
set /A "y0=0, x=1"
for %%N in (%coef%) do set /A "y0+=%%N*x, x*=minX"
for /L %%x in (%minX%,1,%maxX%) do (
set /A "y1=0, x=1"
for %%N in (%coef%) do set /A "y1+=%%N*x, x*=%%x"
set /A "sign1=y0*y1, sign2=sign1>>31, y0=y1"
echo f(%%x^) = !y1!
if !sign1! equ 0 (
echo Exact root found: %%x
set y0=1
) else if !sign2! equ -1 (
set /A prevX=%%x-1
echo Root exist between !prevX! and %%x
)
)
Output example:
Code: Select all
Enter polynomial coefficients (i.e. 1 3 -5 -10 for x^3 + 3*x^2 - 5*x - 10)
1 3 -5 -10
Enter range for X: -10 10
f(-10) = -660
f(-9) = -451
f(-8) = -290
f(-7) = -171
f(-6) = -88
f(-5) = -35
f(-4) = -6
f(-3) = 5
Root exist between -4 and -3
f(-2) = 4
f(-1) = -3
Root exist between -2 and -1
f(0) = -10
f(1) = -11
f(2) = 0
Exact root found: 2
f(3) = 29
f(4) = 82
f(5) = 165
f(6) = 284
f(7) = 445
f(8) = 654
f(9) = 917
f(10) = 1240
Antonio