0
votes

In MATLAB, I wish to define an anonymous function which has a definite sum in it, and another anonymous function in that. Here's a MWE which hopefully describes what I am trying to do:

clear; n=1; syms j; a=0; b=sqrt(0.5);
Finv = @(x) logninv(x,a,b);
fun = @(x) 0.5-symsum(Finv(j*x), j, 1, n+1);
fsolve(fun,0.1)

The error returned is:

Error using symfun>validateArgNames (line 211) Second input must be a scalar or vector of unique symbolic variables.

Error in symfun (line 45) y.vars = validateArgNames(inputs);

Error in sym/subsasgn (line 762) C = symfun(B,[inds{:}]);

Error in logninv (line 60) p(p < 0 | 1 < p) = NaN;

Error in @(x)logninv(x,a,b)

Error in @(x)0.5-symsum(Finv(j*x),j,1,n+1)

Error in fsolve (line 217) fuser = feval(funfcn{3},x,varargin{:});

Caused by: Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.

For this particular choice of Finv I solved it using eval and feval as follows:

clear; n=1; syms j; a=0; b=sqrt(0.5);
Finv = @(x) logninv(x,a,b);
fun = @(x) 0.5-eval(symsum(feval(symengine,'logninv',j*x,a,b), j, 1, n+1));
fsolve(fun,0.1)

which produces the answer in this special case because Finv=@(x) logninv(x,a,b), but this defeats the point, which is that I want to be able to define Finv as a univariate function of my choice, not necessarily a predefined MuPAD expression like 'logninv'.

Any advice would be most appreciated.

1
Is there any reason to use symbolic math? Use sum instead of symsumDaniel
Argh! You are right. This solves it easily: fun = @(x) 0.5-sum(Finv([1:n+1]*x)); Thanks.monkey-dart

1 Answers

0
votes

Try to force the second variable (i.e., j) as being a symbolic variable with a scalar (numerical) data type. Note in his code, that is only variable is not being initialized.

clear; n=1; syms j integer; a=0; b=sqrt(0.5);

Alternatively, you can check assumptions on each variable. For example,

assumptions(j)