I often need to translate results I derive in Mathematica into a MATLAB implementation. These results sometimes involve special functions (e.g. Erfc, BesselJ) whose names and argument order do not correspond with those used in MATLAB.

Are there any resources showing such correspondences?

  • $\begingroup$ Can't you just find out the function definitions? So for instance, Erfc[z] is the same as 1 - 2/Sqrt[Pi] Integrate[Exp[-t^2], {t, 0, z}] $\endgroup$
    – Feyre
    Aug 21 '16 at 21:20
  • $\begingroup$ @Feyre: I don't see that directly helps me guess what name I should be looking for in the MATLAB documentation. $\endgroup$
    – mikado
    Aug 21 '16 at 21:23
  • $\begingroup$ I thought you meant they had no equal, in which case an equivalent statement should be used. Wouldn't this otherwise be better asked at the Matlab forums? $\endgroup$
    – Feyre
    Aug 21 '16 at 21:32
  • $\begingroup$ @Feyre: this question is only of interest to people who use Mathematica (not all of them, obviously). There are other positively-rated questions on related topics here (e.g. mathematica.stackexchange.com/questions/44223/…) $\endgroup$
    – mikado
    Aug 21 '16 at 22:27
  • 2
    $\begingroup$ The elliptic integrals and elliptic functions are particularly tricky. Mathematica uses the parameter convention, while MATLAB uses the modulus convention. $\endgroup$
    – J. M.'s torpor
    Oct 2 '16 at 12:54

I would first do the necessary transformations manually using a ReplaceAll. Then I would translate the result to MATLAB syntax using tools such as ToMatlab.

For example,

trafo = Dispatch@{
   Gamma[a_] :> "gamma"[a],
   Gamma[a_, z_] :> "gamma"[a] (1 - "gammainc"[z, a])
   (* and many more transformations here *)

ToMatlab[ x Sin[x] - Gamma[x,2] /. trafo ]




You would need to create the replacement table yourself, but this shouldn't be a lot of work. Finding the corresponding special functions int the documentation is straightforward and the definitions are given on both sides (so you can see if there's a significant difference, such as with the incomplete Γ function above).

  • $\begingroup$ +1 but I would argue that finding, understanding and testing the corresponding special functions (particularly when they take multiple arguments) is non-trivial and that it would be useful to have a reliable list. $\endgroup$
    – mikado
    Aug 22 '16 at 18:10

The following is a table that I have developed, tested and found to be useful over the years, showing Mathematica functions on the left and their MATLAB equivalents on the right.

ArcTan[x,y]                   atan2(y,x)                                                   
BetaRegularized[z,a,b]        betainc(z,a,b)                                               
ExpIntegralE[1,z]             expint(z)                                                    
Gamma[a,z]                    gammainc(z,a,'upper')*gamma(a)                               
GammaRegularized[n,0,x]       gammainc(x,n)                                                
InverseErf[z]                 erfinv(z)                                                    
InverseGammaRegularized[a,s]  None found                                                   
LegendreP[n,x]                first row of: legendre(n,x)                                  
PolyGamma[z]                  psi(z)                                                       
PolyGamma[n,z]                psi(n,z)                                                     
SphericalBesselJ[n,z]         sqrt(pi/(2*z))*besselj(n+1/2,z)*sign(z)                      
SphericalHankelH2[n,z]        sqrt(pi/(2*z))*(besselj(n+1/2,z)-i*bessely(n+1/2,z))*sign(z) 

I would encourage others to add to and improve this list.


Also, there is a way to convert MeijerG function to Matlab. A ready-to-use Mupad MeijerG function caller for Matlab is available at Matlab Cental. The following line need to be modified in ToMatlab package for cell array support:

ToMatlabaux[l_List] := "[" <> ToMatlabargs[l] <> "]"

Replace this line with:

ToMatlabaux[l_List] := 
        "[" <> ToMatlabargs[l] <> "]"(*in case of Matlab vector*),
        "{" <> ToMatlabargs[l] <> "}"(*in case of Matlab cell array*)]

Also, for support of empty cells the following line

ToMatlabargs[e_] :=
    If[Length[e] === 1, 
        ToMatlabaux[e[[1]]] <> "," <>
            ToMatlabargs[ argslistdrop[e] ] ]

need to be replaced with

ToMatlabargs[e_] :=
    If[Length[e] === 1, 
        ToMatlabaux[e[[1]]] <> "," <>
            ToMatlabargs[ argslistdrop[e] ],e]]

The following lines need to be added to ToMatlab for the case of presence of additional argument r in Mathematica MeijerG:

(*Convert generalized MeijerG to usual MeijerG. MeijerGsubstitute is used in order to avoid potential recursion problem caused by automatic transformation of MeijerG*)
(*  http://functions.wolfram.com/  *)
(*  http://functions.wolfram.com/  *)
    ToMatlabaux[(2 \[Pi])^\[Xi]*r^\[Mu]*MeijerGsubstitute[{Flatten[g/@a1],Flatten[g/@a2]},{Flatten[g/@b1],Flatten[g/@b2]},r^(r(p-q))*z]]]/;Refine[r\[Element]Integers&&r>0]

Then MeijerGsubstitute converted to proper function name for call from Matlab:


P.S. ToMatlab package definitely requires an update after 18 years. Updated by me version of ToMatlab is available here. It also includes support for Piecewise, BesselJ, BesselY, BesselI, BesselK, HypergeometricPFQ, HypergeometricPFQRegularized.

Updated conversion for Piecewise and If functions in my version of ToMatlab. Values for all conditions of Piecewise are converted to Matlab cell array of strings. The element of this cell array is extracted by indexing based on the index of condition, that yields True, in array of conditions. Thus, Matlab evaluates only expression, condition of which is satisfied:

ToMatlabaux[If[test_, t_, f_]] :="evalin('caller',subsref({"<>"'"<>Assuming[test,ToMatlabaux[t]]<>

ToMatlabaux[Piecewise[z_List, f_.]]:=Module[{lastCondition=Simplify[And@@Not/@z[[All,2]]]},

For those, who do not like this ugly/reliable conversion, conversion of Piecewise and If in terms of products of conditions by corresponding values are left commented in ToMatlab file.


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.