MATLAB:在symsum中使用一个匿名函数,然后fsolve

时间:2022-05-18 22:50:20

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:

在MATLAB中,我希望定义一个包含一个确定和的匿名函数,以及其中的另一个匿名函数。这里有一个MWE,希望它能描述我正在做的事情:

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.

使用symfun>validateArgNames(第211行)时出错。第二个输入必须是唯一符号变量的标量或向量。

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

symfun中的错误(第45行)y。var = validateArgNames(输入);

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

sym/subsasgn中的错误(第762行)C = symfun(B,[inds{:}]);

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

logninv(第60行)p(p < 0 | 1 < p) = NaN;

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

错误@(x)logninv(x,a,b)

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

@误差(x)0.5 -symsum(Finv(j * x),j,1,n + 1)

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

fsolve中的错误(第217行)fuser = feval(funfcn{3},x,varargin{:});

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

原因:初始用户提供的目标函数评估失败。FSOLVE无法继续。

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

对于Finv的这个特殊选择,我使用eval和feval求解:

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'.

它在这种特殊情况下产生了答案,因为Finv=@(x) logninv(x,a,b),但这违背了这一点,即我希望能够将Finv定义为我选择的单变量函数,而不是像'logninv'这样预定义的MuPAD表达式。

Any advice would be most appreciated.

如有任何建议,我们将不胜感激。

1 个解决方案

#1


0  

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.

试着强制第二个变量(即, j)作为具有标量(数值)数据类型的符号变量。注意,在他的代码中,只有一个变量没有被初始化。

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

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

或者,您可以检查每个变量的假设。例如,

assumptions(j)

#1


0  

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.

试着强制第二个变量(即, j)作为具有标量(数值)数据类型的符号变量。注意,在他的代码中,只有一个变量没有被初始化。

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

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

或者,您可以检查每个变量的假设。例如,

assumptions(j)