[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

LOGO-L> Integrate (was: GUIs versus functions)



>         to integrate :func :start.x :end.x
>         localmake "result 0
> 	localmake "deltax (:end.x - :start.x)/1000
> 	for [x :start.x :end.x :deltax] ~
> 	    [make "result :result + (invoke :func :x)]
> 	output :result
> 	end
> 

What about this routine to integrate. It's adaptive. It's recursive.


In the line that says:

ifelse :difInt < (power 10 (:difInd - 10)) [

The number 10 controls about how many significant digits you want in the answer.

"eval" evaluates the function :func with respect to :dep using the value :depv

"intParab" evaluates the integral of :func with repect to :dep from :ini to :fin
as if the function were a parabola

"numInt" evaluates the integral of :func with repect to :dep from :ini to :fin
The method is: evaluate the integral as if it were a parabola, if this integral
coincides with the sum of the integrals of the two halves of the function 
as if it were parabolas, then the answer is the sum. Otherwise, the integrals of
both halves are evaluated recursively.

I tried to avoid evaluating the function on the same points, that's way I used
variable numbers of inputs to a procedure.

TEST:

to tst
make "a 5
make "b 10
show numint [(radsin :a) + (:a*:b)] "a 0 (pi/4)
show numint [(radsin :a) + (:a*:b)] "b 0 (pi/4)
show :a
show :b
end

OUTPUT:

3.37714459419078
0.788988323512553
5
10


One more thing. Nobody answered me about derivatives and integrals.
Do you think this stuff is no suitable to be taught with LOGO?
Do you think these routines won't be too useful in a LOGO learning environment?
Do you think these routines shouldn't be made into a library but be
developed by students themselves?
What is it?


***********

to eval :func :dep :depv
localmake :dep :depv
op run :func
end 

to intParab :func :dep :ini :fin [:indIni eval :func :dep :ini] [:indFin eval :func :dep :fin]
localmake "indMid eval :func :dep ((:ini + :fin) / 2)
op ((:indIni + (4 * :indMid) + :indFin) * (:fin - :ini)) / 6
end

to numInt :func :dep :ini :fin [:indIni eval :func :dep :ini] [:indFin eval :func :dep :fin]

local [mid indMid prtInt difInt difInd]

make "mid (:ini + :fin) / 2
make "indMid eval :func :dep :mid

make "prtInt (intParab :func :dep :ini :mid :indIni :indMid) + (intParab :func :dep :mid :fin :indMid :indFin)
make "difInt abs((intParab :func :dep :ini :fin :indIni :indFin) - :prtInt)

make "difInd abs(:indIni) + abs(:indFin)
if (not :difInd = 0) [make "difInd log10 :difInd]

ifelse :difInt < (power 10 (:difInd - 10)) [
	op :prtInt
] [
	op (numInt :func :dep :ini :mid :indIni :indMid) + (numInt :func :dep :mid :fin :indMid :indFin)
]

end

---------------------------------------------------------------
Please post messages to the Logo forum to logo-l@gsn.org.  Mail
questions about the list administration to logofdn@gsn.org.  To
unsubscribe send    unsubscribe logo-l    to majordomo@gsn.org.



Global SchoolNet Foundation - Linking Kids Around the World!
Copyright GSN - All Rights Reserved - Comments & Questions
Visit GSN's Global Schoolhouse for more exciting learning resources!
Search our Site - Home