3
$\begingroup$

I have several expressions like the two followings:

    X11 = I omega rho0 phi HankelH1[m, k0 ra];
    X12 = -b1 kp1^2 BesselJ[m, kp1 ra];

I want to covert these expressions to Matlab. To this end, I use the package ToMatlab (by the way is there somewhere available a more recent version of it?)

X11 // ToMatlab
X12 // ToMatlab
(* "sqrt(-1).*omega.*phi.*rho0.*HankelH1(m,k0.*ra);
" *)
(* "(-1).*b1.*kp1.^2.*BesselJ(m,kp1.*ra);
" *)

However, this is not the syntax that Matlab uses of the Bessel and Hankel functions. For instance BesselJ[m,x] (Mathematica) => besselj(m,x) (Matlab).

Any ideas how to overcome the problem and avoid modifying on my own the latter expressions?

$\endgroup$
  • 1
    $\begingroup$ a quick fix could be ... // ToLowerCase. But I don't know Matlab enough to assess how general it is. $\endgroup$ – Kuba Feb 15 '18 at 13:33
  • $\begingroup$ Thanks a lot! I will try it. I hove more general expressions but for the moment I don't see any problem with your fix. $\endgroup$ – Dimitris Feb 15 '18 at 13:37
  • $\begingroup$ Why not to export the expression in C or Fortran format, and import it to Matlab? $\endgroup$ – José Antonio Díaz Navas Feb 15 '18 at 19:42
2
$\begingroup$

You could modify the ToMatlab package.

Find the section starting with

(*** Symbols *****************************************************************)

then add

ToMatlabaux[BesselJ] = "besselj"
ToMatlabaux[HankelH1] = "besselh"

In these cases the translation was trivial. Only the function name needed to be changed. When the argument order needs changing too, modify the section starting with

(*** Special cases of functions **********************************************)

For example, for the two-argument form of ArcTan, add

ToMatlabaux[ArcTan[x_, y_]] := "atan2(" <> ToMatlabaux[y] <> ", " <> ToMatlabaux[x] <> ")"

This will set up a new rule for ArcTan, in addition to the existing one that handles the single-argument form.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.