How to solve system of equations with TCL programming?
Answers
-
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):
Hope this helps,
Regards,
Michael
0 -
Michael Herve_21439 said:
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):
Hope this helps,
Regards,
Michael
Hi Michael,
I don't know why it cannot run, can you check it?
Thanks
0 -
Brian_20847 said:
Hi Michael,
I don't know why it cannot run, can you check it?
Thanks
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
0 -
Michael Herve_21439 said:
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
0 -
Brian_20847 said:
Hi Michael,
I don't know why it cannot run, can you check it?
Thanks
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"}
0 -
Ben Buchanan said:
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
0 -
Brian_20847 said:
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.
0 -
Thanks for your help.
0