Unrecognized function or variable 'datachk'?

10 次查看(过去 30 天)
Soryy for the long Description. I have prepared a target function for optimization and am using it to build contours for now. Here is a function (a shorten version) to pass parameters to the target function:
function y = OptimizeFilter(A0, f, x0)
A0Val = vpa(A0);
fVal = vpa(f);
xC1 = linspace(0.1, 1.9);
yR1 = linspace(0.1, 1.9);
[XC1, YR1] = meshgrid(xC1, yR1);
fun=@(xx,yy) Target([xx,1,yy,1]);
Z=arrayfun(fun, XC1, YR1,'uniformoutput',false); % 2 last params I put in w/o much understanding on MATLAB's hint
contour(XC1, YR1, Z);
function y = Target(x)
y = 0;
for i = 1:length(fVal)
aCurrent = AmplAndDers(x0(1), x0(2), x0(3), x0(4), fVal(i), x(1), x(2), x(3), x(4));
dev = aCurrent - A0Val(i);
y = y + dev^2;
end
end
end
-----------------------
AmplAndDers() was generated by MATLAB out for objective function and its derivatives (1st and 2nd). It starts with (94 lines long):
function Ampl = AmplAndDers(C1_0,C2_0,R1_0,R2_0,w,x1,x2,x3,x4)
t2 = C1_0.^2;
t3 = C2_0.^2;
t4 = R1_0.^2;
t5 = R2_0.^2;
t6 = w.^2;
t8 = x1.^2;
t9 = x2.^2;
t10 = x3.^2;
t11 = x4.^2;
t12 = C2_0.*R1_0.*x3;
...
It behaves reasonably if I call from Command Window:
OptimizeFilter([1.12511646 3.8376244], [2089.296131 7585.77575], [0.1e-6 1.5e-9 16000 11000]);
but not when I call:
OptimizeFilter(A_Init, f, [0.1e-6 1.5e-9 16000 11000]);
Instead of prompt response in first case, it took 11 mins before it failed:
Unrecognized function or variable 'datachk'.
Error in contour (line 46)
[pvpairs, ~, ~, errmsg, warnmsg] = matlab.graphics.chart.internal.contourobjHelper('parseargs', false,
args{:});
Error in OptimizeFilter (line 28)
contour(XC1, YR1, Z);
I have a suspicion that the problem might be some remnants of symbolic business persisting in AmplAndDers() because terminating the run I would see some references to symbolic scope like:
In ./ (line 366)
X = privBinaryOp(A, B, 'symobj::zipWithImplicitExpansion', 'symobj::divide');
In AmplAndDers (line 53)
Ampl = 1.0./sqrt(t46);
In OptimizeFilter/Target (line 58)
aCurrent = AmplAndDers(x0(1), x0(2), x0(3), x0(4), fVal(i), x(1), x(2), x(3), x(4));
In OptimizeFilter>@(xx,yy)Target([xx,1,yy,1]) (line 21)
fun=@(xx,yy) Target([xx,1,yy,1]);
In OptimizeFilter (line 24)
Z=arrayfun(fun, XC1, YR1,'uniformoutput',false);
Admittedly C1_0, C2_0, ... and x are syms in Workspace, but I do pass numeric values to AmplAndDers(). Is that a problem?
  3 个评论
Valeri Aronov
Valeri Aronov 2021-8-19
编辑:Valeri Aronov 2021-8-19
All of them are not declared explicitly. The output for class() and size() for them:
XC1Class = 'double'
XC1Size = 100 100
YR1Class = 'double'
YR1Size = 100 100
The above sizes are set by meshgrid() itself.
ZClass = 'cell'
ZSize = 100 100
Lookng forward. Thanks.
Adam Danz
Adam Danz 2021-8-19
I see. Walter Roberson addressed this below. Matlab's error message is certainly not helpful in understanding that the inputs are unexpected.

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2021-8-19
Z=arrayfun(fun, XC1, YR1,'uniformoutput',false); % 2 last params I put in w/o much understanding on MATLAB's hint
You use 'uniformoutput', false, which tells arrayfun that the output is a cell array.
ZClass = 'cell'
and that confirms it.
contour(XC1, YR1, Z);
so you are passing a cell array as the third parameter to contour(). However,
there are no syntaxes for contour() that permit you to pass a cell array as the third parameter.
If I read the code correctly it looks to me as if each of the outputs from the function would be a scalar, so I would expect it to work if you set 'uniformoutput', true
  10 个评论
Walter Roberson
Walter Roberson 2021-8-20
Yes, you should start a different Question about the slow arrayfun() . When you do, you should post all your current code.
The profile information you showed cannot be explained based upon the code you claim to be executing in this current Question, and every time I press you on that, you say that the parts I am noticing are no longer part of your code, so you need to present clean information to fresh eyes.
It will have to be a different volunteer that looks through it, though, as I am caught up on internal evidence that your code is not what you claim it to be, and you no longer trust my analysis.
Valeri Aronov
Valeri Aronov 2021-8-20
"... , and you no longer trust my analysis." Walter, that's unexpected and regrettable because I do not trust anyone on MATLAB as much as I trust you. My apologies.

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by