matlabFunction forces me (unnecessarily) to include a dummy integration variable as an argument of the anonymous function it creates.

In the example below, I use matlabFunction to create the anonymous function intF{2}. The function to be created has five arguments, but one (tau) is a dummy variable of integration. Since tau is not going to be assigned a value, I define intF{2} with a set of 'vars' specified which does not include tau. When evaluated, however, intF{2} returns an error, saying that tau is undefined. When I type out the anonymous function directly (i.e., intF{1}), then it evaluates fine, without including tau as an argument. It's clear from the code sample below that intF{1} and intF{2} are identical. So why should the second version fail and the first one succeed? Is this a bug in matlabFunction?
function nothing
syms x0 t T a tau
f = @(tau,x0,t,a) log(x0*exp(a*(tau-t)));
intF{2} = matlabFunction(int(f(tau,x0,t,a),tau,t,T),'vars',[x0,t,T,a]);
intF{1} = @(x0,t,T,a) int(log(x0.*exp(-a.*(t-tau))),tau,t,T);
intF{:}
x0 = 16; t = 1 ; T = 2 ; a = 0.5;
for ii=1:numel(intF);
disp(['Evaluating intF{' num2str(ii) '}']);
eval(intF{ii}(x0,t,T,a))
end;
keyboard;
~

4 Commenti

I'm not 100% sure because I'm not at my PC to check, but I believe the issue is that f is an anonymous function given your syntax. I have no idea how matlabFunction handles this object type, especially when composed of syms. It's not in the documentation.
Try getting rid of the @(tau, x0, t,a) part of your code. I think it should work at that point.
Thanks for the suggestions, jgg. I replaced the third and fourth lines with
f = log(x0*exp(a*(tau-t)));
intF{2} = matlabFunction(int(f,tau,t,T),'vars',[x0,t,T,a]);
which I believe is what you suggested, and got the same error,i.e.
Undefined function or variable 'tau'.
Error in symengine>makeFhandle/@(x0,t,T,a)int(log(x0.*exp(-a.*(t-tau))),tau,t,T)
I did the following as well: replacing lines 3-4 with
f = log(x0*exp(a*(tau-t)));
intF{2} = matlabFunction(int(f,tau,t,T),'vars',[x0,t,T,a]);
And it seems to work properly here. I get:
@(x0,t,T,a)int(log(x0.*exp(-a.*(t-tau))),tau,t,T)
without any errors. Which version of Matlab are you using? It might be a bug.
I get that too, but did you try to evaluate, say,
intF{2}(rand,rand,rand,rand)
Indeed I believe it's a bug. See my answer below.

Accedi per commentare.

 Risposta accettata

I talked to matlab support about this. The problem goes away if you set the IgnoreAnalyticConstraints flag to true.
syms x0 t T a tau
f = log(x0*exp(a*(tau-t)));
intF2 = matlabFunction(int(f,tau,t,T,'IgnoreAnalyticConstraints',true),'vars',[x0,t,T,a]);
x0 = 16; t = 1 ; T = 2 ; a = 0.5;
intF2(x0,t,T,a)
This now does not throw an error

Più risposte (0)

Categorie

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by