0
$\begingroup$

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 + 
 z^2];
AbsoluteTiming[
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

x=0:0.0025:1;
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); 
toc;

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.

$\endgroup$
  • 1
    $\begingroup$ Do you mean to export a function definition or to export some of the outputs of executing a function ? $\endgroup$ – High Performance Mark Nov 29 '18 at 16:29
  • 1
    $\begingroup$ To any plotting software? Is there a reason the plotting in Mathematica won't work for you? If you're really set against using Mathematica to do the plotting, you could check out MATLink. It allows you to communicate between MATLab and Mathematica. $\endgroup$ – MassDefect Nov 29 '18 at 16:51
  • 1
    $\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$ 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
0
$\begingroup$

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.

$\endgroup$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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