Find function real zeros.

Find zero   draw curve   Update 2010-01-14
XYGraph v2.3 - web page graph   ☜☞   donate   get code
The Cauchy-Schwarz Master Class   J. Michael Steele   ★★★★★
This file is personal home work. No one
proofread. Cannot promise correctness.
If you suspect any view point wrong,
please ask a math expert near by.
Freeman 2009-06-19-10-46

Please use MSIE browser to read this file.
Did not test other browser. This file is
written under MSIE 6.0


<a name="box11"> RUN:   help1   alert1
bisectf0() use bisection method find a function's zero points.
Output may contain error, Please verify first
Program environment is MSIE 6.0, please use MSIE

draw curve   copy equation to   goto Draw button   alert2
equation
Domain xbgn ; xend ; tolerance
xbgn,xend step0
If L,M,R same sign, favor zero , min/max

Box 11,   example:

RUN:
Box 12,

<a name="JSMathList">
Equation definition must use Javascript language
●●Javascript math function


<a name="docA001">
2010-01-10-15-42
function bisectf0(bsArg1,bsArg2,bsArg3,bsArg4,bsArg5)
find a zero for user assigned function
bsArg1 = function definition string f(x)
bsArg2 = starting number x=xbgn
bsArg3 = ending number   x=xend
bsArg4 = tolerance 1.e-6
bsArg5 = 10 steps between [xbgn,xend]

<a name="docA002">
Program divide [xbgn,xend] to ten
equal length smaller interval.
var step0=10; //9901101246
step0=bsArg5; //9901101729
Hope to enclose a section with two
ends have opposite function value.

<a name="docA003">
For example if 
f(x)=x*x-1
we know its root is x=-1 and x=+1
if xbgn=-2 and xend=3
but program end at x=-1.99999 and iter0=iterMax
After add ten division code, answer
is better, but not perfect.

<a name="docA004">
For example if 
f(x)=x*x-1
we know its root is x=-1 and x=+1
if xbgn=-22 and xend=36
ten division missed root [-1,+1]
section.

<a name="docA005">
If we use 
f(x)=sin(PI*x/0.5)
if xbgn=-2.25 and xend=2.1
program give nine solutions
(not just one solution)

<a name="docA006">
Within a section with two ends have 
opposite function value, program do
bisection root-finding. Program 
locate xleft, find vleft
locate xrite, find vrite
locate xmidl, find vmidl
x**** is domain value
v**** is  range value
('****' represent either 'left',
 or 'rite' or 'midl')
<a name="docA007">
purpose is to find which x**** has
v****=0
xmidl is the middle point of
xleft and xrite
xmidl=(xleft+xrite)/2;
Each iteration discard half of domain 
section. Until narrow down to a x****
which give 
function value v**** < tolerance
tolerance is assigned by user.

<a name="docA008">
Program return multiple solutions
To call function bisectf0() do as following
[[
var ans0=bisectf0(QCboxfunc0x.value,QCboxXbgn.value,QCboxXend.value,QCboxTol0.value,QCboxStep.value);
]]

<a name="docA009">
re-write calling code as next
bisectf0(QCboxfunc0x.value,
        QCboxXbgn.value,
        QCboxXend.value,
        QCboxTol0.value,
        QCboxStep.value);
Above read value from boxes, allow user
modify box value. 

<a name="docA010">
Same calling with actual data is next
bisectf0('sin(PI*x/0.5)',
        -2.25,
        2.1,
        1e-12,
        10);
String into one line is next
bisectf0('sin(PI*x/0.5)',-2.25,2.1,1e-12,10);

<a name="docA011">
'sin(PI*x/0.5)' is function definition
must put in quote. Must use independent
variable x

-2.25 is domain begin value, 
      x=-2.25 is start point.
2.1   is domain end value, 
      x=2.1 is end point.
<a name="docA012">
1e-12 is tolerance value. function
      value f(x) less than 1e-12
      then program stop. (or hit
      iteration Maximum stop)
10    divide [-2.25,2.1] to 10
      smaller sections. Evaluate
      f(xi) for i=0 to 10. Those
      sub-interval with function
      value change at ends will
      be searched for roots.
      user can change 10 to 100
      or to 1.

<a name="docA013">
bisectf0() return answer via 'return'
mechanism. 
'ans0' is calling and receiving function
variable. (user can change to other name)

<a name="docA014">
ans0 is a two dimension array
ans0[0] has first solution
ans0[i][0] has solution x value
ans0[i][1] has solution function value
ans0[i][2] has solution iteration value

<a name="docA015">
A real 'ans0' example is next
ans0 =
[
 [-1.0,4.4408e-16,'1 midl']
 [ 1.0, 0 ,'1 midl']
]

<a name="docA016">
ans0[0] point to
 [-1.0,4.4408e-16,'1 midl']

ans0[1] point to
 [ 1.0, 0 ,'1 midl']

<a name="docA017">
ans0[0][0] point to -1.0. 
It means x=-1.0 has a root

ans0[0][1] point to 4.4408e-16 
It means f(x)=f(-1.)=4.4408e-16

<a name="docA018">
ans0[0][1] point to '1 midl'
It means 1 iteration done root-finding
In the interval [xleft, xmidl, xrite]
f(xmidl) value is smaller than f(xleft)
and f(xrite). 
<a name="docA019">
In most case xmidl is best
answer. But if rigid lock at x=xmidl
as answer. We may miss f(xleft)=0
exact answer. Run Example 04, you will 
find left and rite selected (better 
than midl) Rare case. Because start
point is a root, end point is a root.

<a name="docA020">
If function value abs(ans0[i][1]) > 0.001
If iteration value = 100
both indicate root-finding failure.
If use function f(x)=x*x+1, never find 
a real root. You will see iter0=100
This program find real root only.

<a name="docA021">
To display answer do as following
[[
QCboxd11.value='';
for(var t0=0;t0<ans0.length;t0++)
QCboxd11.value+='zero '+(t0+1)
+':\nx='+ans0[t0][0]
+'\nf(x)='+ans0[t0][1]
+'\niteration='+ans0[t0][2]
+'\n===\n'
]]
In which 'QCboxd11' and 't0' and
'ans0' you can use different variable
name.

<a name="docA022">
For function x*x+1, no real root at all.
you will find
[[
zero 1:
x=undefined
f(x)=undefined
iteration=undefined
===
]]
2010-01-10-16-26 stop

<a name="docA023">
2010-01-10-16-36 start
Example 02 you will find
f(x)=sin(PI*x/0.5)
x begin=-2.25 ;
x end=2.1  ;
tolerance =1e-12
click run get nine solutions

<a name="docA024">
If change to
x end=6  ;
x in [-2.25,6] cover more roots
do you expect more roots found?
click run get four solutions
Because ten division of [-2.25,6]
missed some opposite function
value section, and program exam
only opposite function value 
section.
2010-01-10-16-40 stop

<a name="docA025">
2010-01-10-18-30 start
Added 
xbgn, xend [  ] step0 
allow user specify how many steps
between [xbgn, xend] to exam.
More steps, cost more computing 
time. But for small problem, more
time is not a concern.
2010-01-10-18-35 stop

<a name="docA026">
2010-01-11-09-38 start
If in 
xbgn, xend [  ] step0 
box fill a number greater than 1.
Then domain is divided into two or
more subsections with node point
in between subsections. Each 
subsection two end's function 
values are evaluated. If they 
are of opposite sign, program 
carry out domain bisection method.
<a name="docA027">
If these nodes function values
happen to be of same sign, even
if function has many roots,
program ignore all of them. 
Output a line
iteration=undefined

<a name="docA028">
However, if in
xbgn, xend [  ] step0 
box fill a number 1. program 
carry out domain bisection method
no matter domain ends function 
values are opposite sign or not.
Output a line
iteration=100 midl
<a name="docA029">
In most case iteration<50 solve
problem. iteration=100 is upper
limit.
2010-01-11-09-51 here

<a name="docA030">
Explain example button as following
Example 01 equation is from
http://freeman2.com/tute0026.htm#clickfx&xtyt
Example 'E' button, 'dd[Uf]/dx/dx' box
filled default value.
If click 'Exercise 6.10:' [17] button
program draw four curves. The purple 
curve is 'dd[Uf]/dx/dx'
Example 01 equation is real application
of this file jszero02.htm/bisectf0()

<a name="docA031">
Example 02 equation sin(PI*x/0.5)
has zero at x=0,±0.5,±1,±1.5 etc.
Show that this file jszero02.htm
can solve multiple zeros. 
Miss zero possible.

<a name="docA032">
Example 03 equation x*x-1
and 02 equation all display 'midl'
In one narrow-down subsection, 
evaluate function value at two ends
and middle point. Three function 
values either one smaller than
tolerance=1.e-12 then found one
zero. 'midl' indicate this answer
come from middle point.

<a name="docA033">
Example 04 equation x*x-1
illustrate that besides 'midl' 
other two 'left', 'rite' are both
possible. Example 04 start point
is a zero point. End point is
another zero point.

<a name="docA034">
Example 05 equation sin(1/x)
test function. sin(1/x) has infinite
many zero between x in (0,0.32)

<a name="docA035">
Example 06 equation x*x
Domain xbgn -2 ; xend 2
xbgn, xend 10 step0 create x array
-2, -1.6, -1.2, -0.8, -0.4, 0
+0.4, +0.8 +1.2, +1.6, +2
except x=0, all x has positive 
function value. For x=0, x*x=0
not change sign to negative. Then
program not find answer.

<a name="docA036">
Example 07 equation sin(PI*x/0.5)
same as example 02 equation.
07 setting is
Domain xbgn -2.75 ; xend 2.25
xbgn, xend 5 step0 
then all node points function
value are +1. Not change sign.
Although sin(PI*x/0.5) has ten
zeros, for x in [-2.75, 2.25]
all zeros missed.
<a name="docA037">
But if change from 5 step0 
to 15 step0 , use fine tunning.
You will find ALL ten roots.

Function bisectf0() is not perfect
but it can help in most cases.
2010-01-11-10-28 stop

<a name="docA038">
2010-01-11-12-12 start
Why write bisectf0() ?
2010-01-06 start write
http://freeman2.com/tute0026.htm
in which, need find function zero
and write the following code
<a name="docA039">
[[
w3=(x2-x1)/200;
x=x1;

//Ufunc2 is function second derivative
//Ufunc2 is supplied by user.
v1=v3=eval('with(Math){'+Ufunc2+'}');

for(w0=x1;w0<=x2;w0+=w3)
        {
x=w0;
v2=eval('with(Math){'+Ufunc2+'}');
if((v2-v0)*(v3-v0)<0)break
v3=v2;
        }
]]
<a name="docA040">
This is dumb code. Divide domain 
to 200 small sections. Each 
iteration drop 1/200 until find
sign change. Code can not narrow
down to shorter and shorter domain
section.

<a name="docA041">
On the other hand bisectf0() drop
1/2 at one iteration. bisectf0() 
can narrow down to shorter domain
section.
2010-01-09 created bisect00.htm
(upload as jszero02.htm) and write
bisectf0() code.
2010-01-11-12-23 stop

2010-01-11-12-38 done spelling check

<a name="docA042">
2010-01-11-16-51 start
Write bisection method bisectf0()
start from 2010-01-09-18-44
until 2010-01-11-13-00
although program work, but it is 
low efficiency. Because I did not
found numerical analysis textbook
until 2010-01-11-16-40.
LiuHH will imporve code in the
future updates.
2010-01-11-16-54.

<a name="docA043">
2010-01-13-13-39 start
Update 2010-01-14 made the following
change
(1) added Javascript math functions
    definition
(2) in function bisectf0() delete
    QCspanAlert10.innerHTML and
    QCspanAlert11.innerHTML
<a name="docA044">
(3) in function bisectf0() added
    var dbg7=''
(4) main change.
    in function bisectf0()
    xleft=xmidl; //9901122156
    xrite=xmidl; //9901122158 choose smaller
    xleft=xmidl; //9901122159
    xrite=xmidl; //9901130039 choose smaller
    xleft=xmidl; //9901130040
<a name="docA045">
(5) add click button <!--9901122215-->
    type="button" value="08"
(6) add
    Box 12,<!--9901122245 use box12 in bisect00.htm-->
<a name="docA046">
(7) add more control
    If L,M,R same sign, favor
     zero    , min/max
(8) added  drawing program
2010-01-13-13-48 here

<a name="docA047">
Detail as following
(1) added Javascript math functions
    definition
This file jszero02.htm find real root
for a math equation in user assigned
x domain. Need to define math equation,
must comply with Javascript language
usage. To include Javascript math 
functions definition provide a reference
for user.

<a name="docA048">
(2) in function bisectf0() delete
    QCspanAlert10.innerHTML and
    QCspanAlert11.innerHTML
Initially write jszero02.htm, use
QCspanAlert11.innerHTML to report
how many intervals have opposite
sign of function values at two ends.
<a name="docA049">
After copy function bisectf0() to
tute0026.htm get trouble. Because
tute0026.htm do not have html
element QCspanAlert11. If other 
programmer use bisectf0() will
have same trouble. 
<a name="docA050">
Function bisectf0() should have
input from function parameter and
have output from return mechanism.
Other than these two, bisectf0()
must not involve any html element.
    QCspanAlert10.innerHTML and
    QCspanAlert11.innerHTML
are not necessary for bisectf0()
LiuHH deleted related codes.

<a name="docA051">
(3) in function bisectf0() added
    var dbg7=''
For debug use.

(5) add click button <!--9901122215-->
    type="button" value="08"
For debug purpose.

<a name="docA052">
(6) add
    Box 12,<!--9901122245 use box12 in bisect00.htm-->
For debug use.

(7) add more control
    If L,M,R same sign, favor
     zero    , min/max

(8) added  drawing program

<a name="docA053">
(4) main change.
    in function bisectf0()
    xleft=xmidl; //9901122156
    xrite=xmidl; //9901122158 choose smaller
    xleft=xmidl; //9901122159
    xrite=xmidl; //9901130039 choose smaller
    xleft=xmidl; //9901130040
2010-01-13-14-03 here

<a name="docA054">
2010-01-13-15-49 start
Please goto drawing program
click [11,] [12], [13], [14] for both
graph and text explanation for
(4) main change.
(7) add more control
2010-01-13-15-52 stop

<a name="docA055">
2010-01-14-17-29 start
If you want to draw equation which 
is copied from 
[[
bisectf0() use bisection method find a function's zero points
equation [    ]
]]
<a name="docA056">
to
[[
f0(x) [    ] x1(t) 
]]
After copy equation to "f0(x) [    ] " box
Before click "Draw f0(x) to f3(x)" button
you must set drawing parameters. If not
the result may be strange.
<a name="docA057">
Settings are as following
[[
x min:  , x max:  ; y min:  , y max:  ; 
x min, x max, y min, y max is coordinate axis range 
Drawing board size W:  H:  
Graph title:  
draw f(x) must use x as independent variable, 
draw x(t),y(t) must use t as independent variable. 
Curve start x/t begin [  ]  , Curve end x/t end  [  ] 
Draw curve in [  ] steps
]]

<a name="docA058">
If previous setting at
x min: -1 , x max: +1 ; y min: -1 , y max: +1 ; 
If current curve need the setting
x min: -10 , x max: +10 ; y min: -10 , y max: +10 ; 
If you do not change setting, graph is done
but you can not see it. Because every curve
section are out of [-1, +1] range.

<a name="docA059">
Curve start x/t begin [  ]  , Curve end x/t end  [  ] 
and
x min: [  ] , x max: [  ]
are different. 
For example, if you draw tan(x). You need set
x min: -5 , x max: +5 ; y min: -5 , y max: +5 ; 
and
 x/t begin [ -1.57 ]  , x/t end  [ +1.57 ] 
to display just one period tan(x)

<a name="docA060">
If use small number in
Draw curve in [  ] steps
curve is not smooth.
If use large number (60 or more) in
Draw curve in [  ] steps
curve is smooth.

Also delete f1(x), f2(x), f3(x) function
definitions, avoid unwanted curve.
2010-01-14-17-48 stop


<a name="drawfx&xtyt"> 2010-01-06-19-12
■ Interactive f(x)_OR_x(t)&y(t) drawing
Please click "Draw 11, 12, 13, 14"
Output may contain error, Please verify first
Program environment is MSIE 6.0, please use MSIE
If you save this file jszero02.htm to your computer
and open local jszero02.htm, it can not draw figure.
You need also save http://freeman2.com/jsgraph2.js
to your computer stay in same folder as jszero02.htm.

x min: , x max: ; y min: , y max: ;
x min, x max, y min, y max is coordinate axis range
Drawing board size W: H:
Graph title:
draw f(x) must use x as independent variable,
draw x(t),y(t) must use t as independent variable.

Curve start x/t begin , Curve end x/t end
Draw curve in steps
f0(x) x1(t)
f1(x) y1(t)
f2(x) x2(t)
f3(x) y2(t)
    No drawing? x or t wrong !
Above big button run user equation. Below run program equation.
Draw f(x) ; Draw x(t),y(t)  math function



<a name="box21">
Box 21,

You can not draw other curve here. But you can
goto [Modify 606] define your equation
and click [Draw] within yellow stripe.
(Do not click [Draw 606]) 2009-10-08-17-08





Javascript index
http://freeman2.com/jsindex2.htm   local
Save graph code to same folder as htm files.
http://freeman2.com/jsgraph2.js   local


This page, Find function real zeros.
http://freeman2.com/jszero02.htm
First Upload 2010-01-11

Thank you for visiting Freeman's page. 
Freeman  2010-01-11-13-09