How to solve system of equations with TCL programming?

Brian_20847
Brian_20847 Altair Community Member
edited September 2023 in Community Q&A

Hi everyone,

I am a newbie in TCL programming. How to solve this system of equations with 0<x<9 and 0<y<9 with for loops.

Thanks

image

Answers

  • Michael Herve_21439
    Michael Herve_21439
    Altair Employee
    edited September 2023

    Hello Brian,

     

    as tcl inherently considers any variable as a string by default, its math capabilities are not so extended than other codes. Especially, it recognizes the math operations below:

    https://www.tcl.tk/man/tcl/TclCmd/mathfunc.html

    So such equation resolution with 2nd order unknown variables cannot be covered with core tcl commands. You may look for extra tcl libraries (as the one in the link below):

    https://core.tcl-lang.org/tcllib/doc/tcllib-1-18/embedded/www/tcllib/files/modules/math/calculus.html

     

    Hope this helps,

     

    Regards,

    Michael

  • Brian_20847
    Brian_20847 Altair Community Member
    edited September 2023

    Hello Brian,

     

    as tcl inherently considers any variable as a string by default, its math capabilities are not so extended than other codes. Especially, it recognizes the math operations below:

    https://www.tcl.tk/man/tcl/TclCmd/mathfunc.html

    So such equation resolution with 2nd order unknown variables cannot be covered with core tcl commands. You may look for extra tcl libraries (as the one in the link below):

    https://core.tcl-lang.org/tcllib/doc/tcllib-1-18/embedded/www/tcllib/files/modules/math/calculus.html

     

    Hope this helps,

     

    Regards,

    Michael

    Hi Michael,

    I don't know why it cannot run, can you check it?

    Thanks

    image

  • Michael Herve_21439
    Michael Herve_21439
    Altair Employee
    edited September 2023

    Hi Michael,

    I don't know why it cannot run, can you check it?

    Thanks

    image

    Hello Brian,

     

    as I state in my previous reply, tcl consider any varibale by default as a string

    So let's say you want to sum up two variables x and y

    set x 1

    set y 2

    set z [$x + $y] will return you 1 + 2, not 3

    you have to use [expr $x + $Y] to get 3

     

    That said, here you try to solve a 2 equation system, which is not supported by core tcl commands, so adding expr operator won't be enough

     

    Best Regards,

    Michael

     

     

  • Brian_20847
    Brian_20847 Altair Community Member
    edited September 2023

    Hello Brian,

     

    as I state in my previous reply, tcl consider any varibale by default as a string

    So let's say you want to sum up two variables x and y

    set x 1

    set y 2

    set z [$x + $y] will return you 1 + 2, not 3

    you have to use [expr $x + $Y] to get 3

     

    That said, here you try to solve a 2 equation system, which is not supported by core tcl commands, so adding expr operator won't be enough

     

    Best Regards,

    Michael

     

     

    Hi Michael,

    So I have to combine 2 equations system to 1 equation to solve right? Can you create this script for me?

    Thanks

  • Ben Buchanan
    Ben Buchanan
    Altair Employee
    edited September 2023

    Hi Michael,

    I don't know why it cannot run, can you check it?

    Thanks

    image

    Like Michael said you will have to use the expr command and probably an if statement to do it the way you are showing.

    https://www.tcl.tk/man/tcl8.5/TclCmd/expr.html

    https://www.tcl.tk/man/tcl8.5/TclCmd/if.html

    Something like:

    if {[expr {double($x)}] == [expr {2.0* $y}] && [expr {double($x)+$y}] == 3.0} {puts "x = $x; y = $y} {puts "unsolved"}

  • Brian_20847
    Brian_20847 Altair Community Member
    edited September 2023

    Like Michael said you will have to use the expr command and probably an if statement to do it the way you are showing.

    https://www.tcl.tk/man/tcl8.5/TclCmd/expr.html

    https://www.tcl.tk/man/tcl8.5/TclCmd/if.html

    Something like:

    if {[expr {double($x)}] == [expr {2.0* $y}] && [expr {double($x)+$y}] == 3.0} {puts "x = $x; y = $y} {puts "unsolved"}

    Hi Ben, 

    Can you check it for me? I don't know why it has errors.

    Thanks

     

  • Ben Buchanan
    Ben Buchanan
    Altair Employee
    edited September 2023

    Hi Ben, 

    Can you check it for me? I don't know why it has errors.

    Thanks

     

    You had the then and else parts of your if statement outside of the for loop. You had expr statements inside other expr statements which is unnecessary. The use of [, { and ) was all mixed up in your expr statements too.  I am not sure I got all the ) in the right spot for your statement but depending on where you put those you are trying to take a sqrt of a negative number which gives an error. I would try this with much more simple equations first and then build up the complexity of the equations once you have the coding down.

    https://www.tcl.tk/man/tcl8.5/tutorial/tcltutorial.html

    Tutorials 5, 6, and 7 are good references on how to use the different kind of braces.

    Basically [ ] are to contain a command inside another command and are needed any time the command isn't the first word in the line.  The { } can be used to group arguments but usually doesn't allow variable substitution, with the expr statement being the exception where it is recommend to use them and will still allow substitution. The ( ) are used just like you would use them in writing any equation and to hold arguments to the math functions (ex. sqrt(2)).

    Again, I would recommend starting simple and working up to make sure you understand and have each part correct before getting more complicated.

  • Brian_20847
    Brian_20847 Altair Community Member
    edited September 2023

    Thanks for your help.