reshape() error in the function created by matlabFunction()

조회 수: 3 (최근 30일)
Valeri Aronov
Valeri Aronov 2021년 8월 21일
답변: Valeri Aronov 2021년 9월 3일
matlabFunction() has generated for me AmplAndDers() to be used in fminunc(). The very first objective function evaluation fails:
Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.
Error in AmplAndDers (line 93)
Hess = reshape([-t2.*t3.*t4. ... .*(3.0./4.0)],[4,4]);
The signature of the function is:
function [Ampl,GradA,Hess] = AmplAndDers(C1_0,C2_0,R1_0,R2_0,w,x1,x2,x3,x4)
I call it with a w being an array. Two other outputs are returned correctly. I do not understand what does the above suggestion mean:
Use [] as one of the size inputs to automatically calculate the appropriate size for that dimension.
If I manually change the above reshape() call to end with:
...*(3.0./4.0)],[4,4,length(w)]);
the program runs OK.
How can avoid the manual intervention?
  댓글 수: 4
Valeri Aronov
Valeri Aronov 2021년 8월 24일
편집: Valeri Aronov 2021년 8월 24일
This is the script that creates the function in question:
syms s
syms C1 C2 R1 R2 w real
syms C1_0 C2_0 R1_0 R2_0 real
syms T(s, C1, C2, R1, R2)
T(s, C1, C2, R1, R2) = 1/(C1*C2*R1*R2*s^2 + (C2*R1 + C2*R2)*s + 1);
A = abs(T(1j*w, C1, C2, R1, R2));
A = rewrite(A, 'sqrt')
syms x1 x2 x3 x4 real
x=[x1,x2,x3,x4];
A = subs(A, [C1, C2, R1, R2], [C1_0, C2_0, R1_0, R2_0] .*x);
Ampl = A;
GradA = gradient(A, x);
Hess = hessian(A, x);
x0 = [0.1e-6, 1.5e-9, 16000, 11000];
f = logspace(1,5,101);
A_Init = double(abs(T(1j*f, x0(1), x0(2), x0(3), x0(4))));
% Covert symbolic functions to the function
matlabFunction(Ampl, GradA, Hess, 'File', 'AmplAndDers');
This how I tried to follow MATLAB hint and failed:
% matlabFunction(Ampl, GradA, Hess, 'File', 'AmplAndDers', 'Vars', {C1_0,C2_0,R1_0,R2_0,w[],x1,x2,x3,x4});
Thanks
Valeri Aronov
Valeri Aronov 2021년 8월 24일
Oops. The call is here:
function [x, fval] = OptimizeFilter(A0, f, x0)
options = optimoptions('fminunc','Algorithm','trust-region','SpecifyObjectiveGradient',true, HessianFcn','objective');
[x,fval,exitflag,output] = fminunc(@Target, [1,1,1,1], options)
function y = Target(x)
[aCurrent, GradW, HessW] = AmplAndDers(x0(1), x0(2), x0(3), x0(4), f, x(1), x(2), x(3), x(4));
...
end
end

댓글을 달려면 로그인하십시오.

채택된 답변

Valeri Aronov
Valeri Aronov 2021년 9월 3일
The error is known to MATLAB and will be fixed in the future

추가 답변 (1개)

KSSV
KSSV 2021년 8월 21일
Read the documentation of reshape and understand it. The error is clear, you are trying to create extra elements than present in the array while reshaping.
Example:
A = rand(1,25) ;
B = reshae(A,5,5) ; % 5*5 = 25, no error as same elements present
C = reshape(A,6,5) ; % 6*5 = 30, error as A as 30 elements only.
  댓글 수: 1
Valeri Aronov
Valeri Aronov 2021년 8월 21일
편집: Valeri Aronov 2021년 8월 22일
The body of AmplAndDers() function and the reshape() call code in it is generated by matlabFunction(). I can't see how reading the documentation of reshape() can possibly help me. And, yes, the error is clear indeed.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by