4
$\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$
3
  • 1
    $\begingroup$ a quick fix could be ... // ToLowerCase. But I don't know Matlab enough to assess how general it is. $\endgroup$ Commented Feb 15, 2018 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$ Commented Feb 15, 2018 at 13:37
  • $\begingroup$ Why not to export the expression in C or Fortran format, and import it to Matlab? $\endgroup$ Commented Feb 15, 2018 at 19:42

1 Answer 1

3
$\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$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.