Charles Pegge
23-03-2009, 08:33
An efficient way of solving difficult equations like a^3+5*a^2=20.
All you have to do is formulate a null expression: a^3+5*a^2-20 and plug it in.
'SUCCESSIVE APPROXIMATION USING NEGATIVE FEEDBACK
uses "oxygen"
'
'
dim src as string
src = "
dim as double a1,a2,i1,i2,fbr
dim as long i
'
function ff( a as double ) as double
'
'NULL EXPRESSIONS
'
function=a*a-2 ' square root of 2
'function=a*a*a-2 ' cube root of 2
'function=abs (sin(a/6)-0.5) 'pi
'function=abs((1/a)-a+1) 'phi
end function
i=0
a1=1
a2=a1+1 ' second extimate of ans
i1=ff(a1) ' first result
i2=ff(a2) ' second result
do
fbr=(a2-a1)/(i2-i1) : a1=a2 : i1=I2 : a2=a2-fbr*i2 : i2=ff(a2)
if (abs(a2-a1)<1e-16)or(i>1000) then exit do
inc i
loop
print str(a1) `
` str(i) ` iterations`
terminate
"
o2_basic src
'msgbox 0, o2_len+$cr+o2_prep "o2h "+src
if len(o2_error) then msgbox 0, o2_error : stop
o2_exec
All you have to do is formulate a null expression: a^3+5*a^2-20 and plug it in.
'SUCCESSIVE APPROXIMATION USING NEGATIVE FEEDBACK
uses "oxygen"
'
'
dim src as string
src = "
dim as double a1,a2,i1,i2,fbr
dim as long i
'
function ff( a as double ) as double
'
'NULL EXPRESSIONS
'
function=a*a-2 ' square root of 2
'function=a*a*a-2 ' cube root of 2
'function=abs (sin(a/6)-0.5) 'pi
'function=abs((1/a)-a+1) 'phi
end function
i=0
a1=1
a2=a1+1 ' second extimate of ans
i1=ff(a1) ' first result
i2=ff(a2) ' second result
do
fbr=(a2-a1)/(i2-i1) : a1=a2 : i1=I2 : a2=a2-fbr*i2 : i2=ff(a2)
if (abs(a2-a1)<1e-16)or(i>1000) then exit do
inc i
loop
print str(a1) `
` str(i) ` iterations`
terminate
"
o2_basic src
'msgbox 0, o2_len+$cr+o2_prep "o2h "+src
if len(o2_error) then msgbox 0, o2_error : stop
o2_exec