none
Algorithm to calculate the value of a function f (x) RRS feed

  • Question

  • Hi.

    I have a generic function f (x) for example (x + 1) ^ (3 + log (x) ^ 2).

    Is there an algorithm that allows me to calculate the function for a certain x value ?

    In other words, small basic must recognize the function.

    Thanks


    Tuesday, August 8, 2017 10:21 AM

Answers

  • For use with  LDMath.Evaluate(2)  you'll have to transform your syntax (eg. ^ operator, log) to JScript or C# (2).

    And in the function string replace your variable eg. 'x'  with  ' "+ x + " '.

    x = 3.14
    str = "Math.Pow("+ x +" + 1, 3 + Math.Pow(Math.Log10("+ x +"), 2))"  ' base 10 lg
    TextWindow.WriteLine("f(x) = "+ str)
    TextWindow.WriteLine("Eval  f("+ x +") = "+ LDMath.Evaluate(str))
    TextWindow.WriteLine("Eval2 f("+ x +") = "+ LDMath.Evaluate2(str))
    TextWindow.WriteLine("")

    PS: Update

    x = 3.14
    fx = "Math.Pow(x + 1, 3 + Math.Pow(Math.Log10(x), 2))"  ' base 10 lg
    TextWindow.WriteLine("f(x) = "+ fx)

    str = LDText.Replace(fx, "x", x)
    TextWindow.WriteLine("f("+ x +") = "+ str)
    TextWindow.WriteLine("")

    TextWindow.WriteLine("Eval:  f("+ x +") = "+ LDMath.Evaluate(str))
    TextWindow.WriteLine("Eval2: f("+ x +") = "+ LDMath.Evaluate2(str))
    TextWindow.WriteLine("")

    Edit:

    * For base 10 logarithm 'log'  use  Math.Log10

    * For base e logarithm 'ln' use Math.Log

    in the expression string, Math class

    Tuesday, August 8, 2017 1:29 PM
    Answerer
  • In clumsy english:

    Parsing is what happens in the samples (from LitDev and NaochanON) linked from here Calculation of the value in a variable

    To evaluate a math. expression  like "2 + 3"  you parse through that string with eg. Text commands, find and pick out all the numers (2, 3) , operators (+, -, /, *. ^, %) , brackets (, ) ,  ev. function names (log, ln, tan etc),  break it into pieces, recombine them to calculate the result step by step  in correct math. priority  like ^, *, /, %, +,  -  from the inner to outer level.  Order_of_operations (Wiki)

    Better read here: Parsing (Wiki)

    Math.Pow    is the command for the '^-operator'  in C#  and/or  JScript   (like  Math.Power in SB)


    Tuesday, August 8, 2017 4:50 PM
    Answerer

All replies

  • I think you have to write those few lines yourself, e.g

    x=3.14
    lg=Math.Log(x)
    exp=3+lg*lg
    answer= Math.Power(x+1,exp)

    If you need to do this for more functions, you can try to write a parser to do this ;-)

    EDIT:  look at the formula calculator in https://social.msdn.microsoft.com/Forums/en-US/cb8c8e01-dd26-4260-822f-b9fa4fbd67a6/calculator-using-formula?forum=smallbasic for ways to do this.


    Jan [ WhTurner ] The Netherlands


    Tuesday, August 8, 2017 12:41 PM
    Answerer
  • For use with  LDMath.Evaluate(2)  you'll have to transform your syntax (eg. ^ operator, log) to JScript or C# (2).

    And in the function string replace your variable eg. 'x'  with  ' "+ x + " '.

    x = 3.14
    str = "Math.Pow("+ x +" + 1, 3 + Math.Pow(Math.Log10("+ x +"), 2))"  ' base 10 lg
    TextWindow.WriteLine("f(x) = "+ str)
    TextWindow.WriteLine("Eval  f("+ x +") = "+ LDMath.Evaluate(str))
    TextWindow.WriteLine("Eval2 f("+ x +") = "+ LDMath.Evaluate2(str))
    TextWindow.WriteLine("")

    PS: Update

    x = 3.14
    fx = "Math.Pow(x + 1, 3 + Math.Pow(Math.Log10(x), 2))"  ' base 10 lg
    TextWindow.WriteLine("f(x) = "+ fx)

    str = LDText.Replace(fx, "x", x)
    TextWindow.WriteLine("f("+ x +") = "+ str)
    TextWindow.WriteLine("")

    TextWindow.WriteLine("Eval:  f("+ x +") = "+ LDMath.Evaluate(str))
    TextWindow.WriteLine("Eval2: f("+ x +") = "+ LDMath.Evaluate2(str))
    TextWindow.WriteLine("")

    Edit:

    * For base 10 logarithm 'log'  use  Math.Log10

    * For base e logarithm 'ln' use Math.Log

    in the expression string, Math class

    Tuesday, August 8, 2017 1:29 PM
    Answerer
  • Excuse ignorance.

    What is it:

    • Parser
    • Math.Pow

    Tuesday, August 8, 2017 3:21 PM
  • In clumsy english:

    Parsing is what happens in the samples (from LitDev and NaochanON) linked from here Calculation of the value in a variable

    To evaluate a math. expression  like "2 + 3"  you parse through that string with eg. Text commands, find and pick out all the numers (2, 3) , operators (+, -, /, *. ^, %) , brackets (, ) ,  ev. function names (log, ln, tan etc),  break it into pieces, recombine them to calculate the result step by step  in correct math. priority  like ^, *, /, %, +,  -  from the inner to outer level.  Order_of_operations (Wiki)

    Better read here: Parsing (Wiki)

    Math.Pow    is the command for the '^-operator'  in C#  and/or  JScript   (like  Math.Power in SB)


    Tuesday, August 8, 2017 4:50 PM
    Answerer
  • Excuse ignorance.

    What is it:

    • Parser
    • Math.Pow

    Hello BluStars, I've written an article about Parser.

    Small Basic: Parser Generator


    Nonki Takahashi

    Wednesday, August 9, 2017 6:22 AM
    Moderator