|
Post by bencooper on Jun 16, 2020 4:20:23 GMT -5
For a demo solution of function FNX(x) by Method of Bisection program I want the User to input his own equation. In LB is it possible to pass a bespoke mathematical function into a called function or even a main program (as a string variable perhaps)? Say User wants to replace with 2*x^2-x+4 for example, or even a*(e^(x/a)+e^(-x/a))/2 for a catenary.
'function FNX(x) starts here
'---------------------------
FUNCTION FNX(a)
FNX=a^3-a^2+1 END FUNCTION
|
|
|
Post by tenochtitlanuk on Jun 16, 2020 8:01:02 GMT -5
A natural for the 'eval()' function. But you'll need to give input in LB syntax for the exponentials, and beware functions that will get 'divide by zero' crashes.
There's a range of relevant graphic stuff on my LB site.. or here's a quick demo....
nomainwin
WindowWidth =550 WindowHeight =550
open "Demo" for graphics_nsb as #wg
#wg "trapclose quit" #wg "down"
prompt "Your function "; fn$ if fn$ ="" then fn$ ="( exp( x) +exp( 0 -x)) /2"
for x =-1 to 1 step 0.001 y =eval( fn$) #wg "set "; 10 +250 +250 *x; " "; 250 -100 *y next x
wait
sub quit h$ close #wg end end sub
|
|
|
Post by Carl Gundel on Jun 16, 2020 9:50:50 GMT -5
A natural for the 'eval()' function. But you'll need to give input in LB syntax for the exponentials, and beware functions that will get 'divide by zero' crashes. ON ERROR GOTO is your friend. on error goto [err] print eval("3+4/0")
[err] print Err$
|
|
|
Post by bencooper on Jun 17, 2020 2:06:40 GMT -5
thank you for your helpful comments, John and Carl !
|
|