I found a Mathematica notebook on the internet, which calculates the inverse kinematics of a 6DoF robot.
link to the notebook
In one part of the code it gives a symbolic expression using the Solve[] function.
(For easier writing "Th" means now the greek letter "theta" - the joint variable)
sol5 = Solve[-Cos[Th5]] == nx Cos[Th1] + ny Sin[Th1], {Th5}];
Later a Compile[] function is using it, but now with the already solved full form of the equation.
theta5 = Compile[{
{Th1, _Real},
{{nx, _Real}, {ny, _Real}},
Module[{...local variables...},
(*and here is the symbolic equation*)
{-ArcCos[-nx Cos[Th1] - ny Sin[Th1]],
ArcCos[-nx Cos[Th1] - ny Sin[Th1]]}
]];
(The exact theoretical background of the problem is not important right now.)
My question is the following:
Is there a way to make it so, that when the equation (used in Solve) changes, this modification is also reflected automatically inside the Compile function?
Conceptually, it would be similar to storing a value in a variable and having the Compile directly use that. Of course, that’s not possible as-is, since Solve works symbolically while Compile operates numerically.
Here's the WRONG way:
sol5 = Solve[-Cos[Th5] == nx Cos[Th1] + ny Sin[Th1], {Th5}]
sol5a = Th5 /. sol5[[1]];
sol5b = Th5 /. sol5[[2]];
theta5 = Compile[{
{Th1, _Real},
{{nx, _Real}, {ny, _Real}},
Module[{...local variables...},
{sol5a, sol5b} (*this part is, what I'm most interested in*)
]];
In this example it's a pretty short and easy expression. But in other part of the code it's much more complicated. So it would be with this way. It doesn't matter what the expression is, we store it somewhere and maybe modify it to be suitable for the Compile.
If it's impossible, or maybe I should start thinking about an other way, I'm happy to recieve that answer too.
Thanks! Gyuri
Solve[]fails returning nothing? Then the compiled code will also fail, possibly crashing the session? Some delicate error handling would be required I think. $\endgroup$SolvetoNSolveinstead of usingCompile. $\endgroup$