fminunc giving correct optimized values for anonymously defined function, BUT wrong value for .m function
1 次查看(过去 30 天)
显示 更早的评论
I have a function at first defined anonymously inside the same script. After performing optimization through fminunc, it provides acceptable optimized parameter values.
f=@(A)0;
for i=1:1:size(c,1)
disp(i)
v(i,1)=(p2(i,1)-p1(i,1))/(sqrt(((p2(i,1)-p1(i,1)).^2)+((p2(i,2)-p1(i,2)).^2)+((p2(i,3)-p1(i,3)).^2)));
v(i,2)=(p2(i,2)-p1(i,2))/(sqrt(((p2(i,1)-p1(i,1)).^2)+((p2(i,2)-p1(i,2)).^2)+((p2(i,3)-p1(i,3)).^2)));
v(i,3)=(p2(i,3)-p1(i,3))/(sqrt(((p2(i,1)-p1(i,1)).^2)+((p2(i,2)-p1(i,2)).^2)+((p2(i,3)-p1(i,3)).^2)));
x=c(i,24)*((n-k+2)/127.59);
N11=BSpline1(k,t,x,n,periodic);
N22=BSpline2(k,t,x,N11,periodic);
N33=BSpline3(k,t,x,N22,periodic);
fx=@(A)0;fy=@(A)0;fz=@(A)0;
for j=1:1:(n+1)
fx=@(A)(fx(A)+A(j,1)*N33(j,1));
fy=@(A)(fy(A)+A(j,2)*N33(j,1));
fz=@(A)(fz(A)+A(j,3)*N33(j,1));
end
f=@(A)(f(A)+((p1(i,1)-fx(A))^2+(p1(i,2)-fy(A))^2+(p1(i,3)-fz(A))^2-(((p1(i,1)-fx(A))*v(i,1))+((p1(i,2)-fy(A))*v(i,2))+((p1(i,3)-fz(A))*v(i,3)))^2));
end
A0=ones(n+1,3);
[Aopt,fopt]=fminunc(f,A0);
But this implementation is very slow and to make it run faster I have separatly defined the function and also instead of for loops I used vector and matrix definition directly.
for i=1:1:size(c,1)
disp(i)
v(i,1)=(p2(i,1)-p1(i,1))/(sqrt(((p2(i,1)-p1(i,1)).^2)+((p2(i,2)-p1(i,2)).^2)+((p2(i,3)-p1(i,3)).^2)));
v(i,2)=(p2(i,2)-p1(i,2))/(sqrt(((p2(i,1)-p1(i,1)).^2)+((p2(i,2)-p1(i,2)).^2)+((p2(i,3)-p1(i,3)).^2)));
v(i,3)=(p2(i,3)-p1(i,3))/(sqrt(((p2(i,1)-p1(i,1)).^2)+((p2(i,2)-p1(i,2)).^2)+((p2(i,3)-p1(i,3)).^2)));
x=c(i,24)*((n-k+2)/128.689);
N11(:,i)=BSpline1(k,t,x,n,periodic);
N22(:,i)=BSpline2(k,t,x,N11,periodic);
N33(:,i)=BSpline3(k,t,x,N22,periodic);
end
% function
function f=myObjective(A,N33,p1,v)
fxyz=N33.'*A;
D=p1-fxyz;
E1=D.^2;
E2=E1';
E3=sum(E2);
F1=(D.*v);
F2=F1';
F3=sum(F2);
F4=F3.^2;
G=E3-F4;
f=sum(G);
end
%%%%%%%
A0=ones(n+1,3);
[Aopt,fopt]=fminunc(@(A)myObjective(A,N33,p1,v),A0);
At same initial values in two cases (anonymous function and .m function) the objective function gives same values which means the function definition in two cases are same.
But after performing optimization in last case (.m function case), I get totally wrong values of input matrix.
n=3 % anonymous function case
Aopt=
-1.53685543885162 6.36319472017170 0.0251668191421513
13.1109550026546 -54.9314786364682 -0.248200752927807
37.2709897604245 49.3077792319788 -0.169381729039393
50.0220902870506 2.38624594683487 0.217368669572959
n=3 % .m function case
Aopt=
0.599435929547115 -26.1229467554614 -0.286717780987037
26.0699535504464 -0.642922198673024 -0.0939110197500202
1.00105690382098 1.00025671501624 0.999976771178033
1 1 1
How do I correct it?
0 个评论
采纳的回答
Matt J
2021-9-26
编辑:Matt J
2021-9-26
At same initial values in two cases (anonymous function and .m function) the objective function gives same values which means the function definition in two cases are same.
To be certain the definitions are the same, you probably need to compare the objective function implementations at more than just one A. Also, have you ruled out that both solutions may be valid? How different are the objective function values at the two solutions?
Finally, if the second implementation is correct, then fminunc is unnecessary. There is an analytical solution to your problem, which you could also compare with the other two implementations.
w=v.^2;
g=N33*p1- N33*(w.*p1);
C=N33*N33.';
for i=1:3
H = C - N33*(w(:,i)*N33.');
A(:,i)=H\g(:,i);
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Problem-Based Optimization Setup 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!