Is there any native function in Mathematica (not ToMatlab) that I can export a function from Mathematica to MATLAB, to Paraview or to any plotting software?

Clarification: I want to export something like f[x_,y_,z_]:=Sin[x]Sin[y z] e^z;

Edit: Here why I want to move to MATLAB. I will have a 4D system and use ListPlot3D, SliceListPlot in Mathematica and I need to evaluate the function.

f[x_, y_, z_] := 
Cos[Pi*x^3] + Cos[Pi*x^3]*Cos[Pi*x^3] + Cos[Pi*z^3] + 
Sin[x + z] x^2 + y^2 + z^2 + 
Exp[Cos[Pi*x^3] Cos[Pi*x^3] + Cos[Pi*z^3] + Sin[x + z] x^2 + y^2 + 
data = Table[
Evaluate@N[f[x, y, z]], {x, 0, 1, 0.0025}, {y, 0, 1, 0.0025}, {z, 
 0, 1, 0.0025}];]

and Matlab version

y=x; z=x;
tic;[X,Y,Z] = meshgrid(x,y,z);F = cos(pi*X.^3) + 
cos(pi*X.^3).*cos(pi*X.^3) + cos(pi*Z.^3) + sin(X + Z).*X.^2 + Y.^2 
Z.^2 + exp(cos(pi*X.^3).*cos(pi*X.^3) + cos(pi*Z.^3) + sin(X + Z).*X.^2 
+ Y.^2 + Z.^2); 

Mathematica takes 37 seconds and MATLAB takes 14 seconds. I am aware the 37 seconds nothing but my function is much longer, it is 4D. enter image description here Thanks. Erdem

Ps: My functions are generally really long trigonometric functions.

  • 2
    $\begingroup$ How about you post an example of a slow plot, because Mathematica shouldn't be slower than Matlab for plotting. There are a number of ways you can speed up plots, including within a Dynamic environment. $\endgroup$ – KraZug Nov 30 '18 at 7:42
  • 2
    $\begingroup$ "ToMatlab is not an option for me". Did you look at what that package does? While the question is not very clearly phrased, as far as I can tell it does exactly what you want. It converts expressions from Mathematica syntax to MATLAB syntax. $\endgroup$ – Szabolcs Nov 30 '18 at 10:29
  • 2
    $\begingroup$ If I compile your function, the table generation time goes from 34s to 6s. $\endgroup$ – KraZug Nov 30 '18 at 12:12
  • 3
    $\begingroup$ f4 = Compile[{{x, _Real}, {y, _Real}, {z, _Real}}, Cos[Pi*x^3] + Cos[Pi*x^3]*Cos[Pi*x^3] + Cos[Pi*z^3] + Sin[x + z] x^2 + y^2 + z^2 + Exp[Cos[Pi*x^3] Cos[Pi*x^3] + Cos[Pi*z^3] + Sin[x + z] x^2 + y^2 + z^2], CompilationTarget -> "C", Parallelization -> True, RuntimeOptions -> "Speed"] gives me 5.7s instead of 34s. $\endgroup$ – KraZug Nov 30 '18 at 12:20
  • 2
    $\begingroup$ @KraZug Parallelization is redudant here, it won't have any effect without RuntimeAttributes -> {Listable}. $\endgroup$ – xzczd Dec 1 '18 at 8:42

Whether Mathematica is slow or fast does not really bother You. So the demand is to the HeavysidePi function. You ansatz is about alright. Continue use Export MatLab Matrix. Matlab is fast in importing and you can use the function values in 3D, 4D whatever You like. You friend has Matlab and can use the precompiled function value.

I am not deep into MatLab, but the name is short form of Matrix Laboratorium or similar. To be fast in functions the already use precompiled function definitions. So this path is simply following the strategies already used extensively in MatLab.

HeavysidePi is available in Mathematica in one, two and three dimensional input and probably can be used in even higher dimensionality.


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.