Message Boards Message Boards


Call Mathematica from matlab script?

Posted 8 months ago
7 Replies
1 Total Likes

I have a Matlab script that generates data and I want to find the numerical derivative of the data. I want to call mathematica function D[] to do the numerical derivative within my matlab script. Is there a way to call the D[] from matlab. Please note that I am new to mathematica and started using it today

7 Replies

Have you considered running the MATLAB script from Mathematica and then using D[] on its output? I find MATLink very useful for this kind of interaction.

Posted 8 months ago

Thank you Gustavo. I need to use the output of D[] in the remaining part of my matlab script. To make it clearer , my matlab script needs to do numerical differentiation then i should use that output of D[] in other calculations in matlab.

(1) Why not use gradient within MATLAB?

(2) D[expr, t] is for symbolic derivatives, not numerical ones. You cannot use it to differentiate data, unless you construct a symbolic expression that depends on some variable(s) (e.g. t, etc.), such as Interpolation[data][t]. So I think there's something you have to figure out.

(3) Since you're new to Mathematica, here's an example:

Suppose you've manage to import your data into Mathematica. For the sake of an example, here's some Mathematica code to generate data that approximates the function $\sin t$.

t = Subdivide[0., 2 Pi, 100];  (* Subdivide[] ~ linspace() in MATLAB *)
data = Sin[t];

One can plot data with ListLinePlot[data] (like plot(data)) or ListLinePlot[Transpose@{t, data}] (like plot(t, data)). The prefix notation func@arg, which is the same as func[arg], can be used on single-argument functions to reduce the number of nested brackets []. Whether this makes code easier to work with is a matter of taste and habits.

Mathematica has a function to differentiate this sort of data, but it's somewhat hidden inside the numerical differentiation contexts. It is called NDSolve`FiniteDifferenceDerivative and may be found in this tutorial.

grad = NDSolve`FiniteDifferenceDerivative[Derivative[1], t, data];
ListLinePlot[Transpose@{t, grad}]

enter image description here

By default NDSolve`FiniteDifferenceDerivative will compute a 4th-order, finite-difference approximation to the derivative. This can be controlled by the option "DifferenceOrder":

grad = NDSolve`FiniteDifferenceDerivative[Derivative[1], t, data, "DifferenceOrder" -> 2];

This will yield nearly the same result as MATLAB's gradient (specifying appropriate spacing). The only difference is that at the endpoints of data, MATLAB uses an order-1 difference (i.e., the slopes between the first two points and the last two points) and Mathematica uses an order-2 formula.

You can also substitute Derivative[2] for Derivative[1] to approximate the second derivative, and so forth. As you may know, the higher the derivative, the more the noise is amplified.

As Gustavo has said, you can use MATLink to do all the necessary communication between Mathematic and MATLink for this.

Posted 8 months ago

Thank you Michael. 1- I tested gradient on a simple function in matlab but it did not give an accurate result 2- I want to construct a symbolic function using interpolation in mathematica, then I can evaluate the derivative using D[].

I have a question about MatLink, It seems that MatLink is good for calling matlab script from within mathematica script, however, I want to call a mathematica script from within matlab. Is this possible?

I have a question about MatLink, It seems that MatLink is good for calling matlab script from within mathematica script, however, I want to call a mathematica script from within matlab. Is this possible?

No, this is not possible with MATLink. MATLink is strictly for the reverse.

There are other software packages that claim to make this possible, but I have never used them and I cannot tell you how well they work. Here's one example: Before you try these, make sure that MATLAB really cannot do what you want on its own, and that Mathematica can. As Michael said, D is for symbolic differentiation, and I cannot see how calling it can be useful from MATLAB: MATLAB doesn't usually operate with symbolic data. (And if you do have the MATLAB symbolic toolbox, then it can already do symbolic differentiation anyway.)

I do not know of a way to call Mathematica from MATLAB.

For completeness we should mention that you can use MATLAB's dos command to call any program. And there is WolframScript:

>> cd('C:\Program Files (x86)\Wolfram Research\WolframScript\')
>> [status,cmdout]=dos('wolframscript.exe -code "N[D[Sin[x],x]/.x->Pi]"')

status =


cmdout =

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract