fminunc giving correct optimized values for anonymously defined function, BUT wrong value for .m function

1 vue (au cours des 30 derniers jours)
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?

Réponse acceptée

Matt J
Matt J le 26 Sep 2021
Modifié(e) : Matt J le 26 Sep 2021
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
  5 commentaires
Satyajit Ghosh
Satyajit Ghosh le 1 Oct 2021
Sorry! After I checked properly, I found that two implementation are not equivalent. These are some values of two functions at various A matrices.
.m function Anonymous function
36231258.356491 39156792.515199
39225691.846561 39413498.295238
38708505.011888 39947618.013650
38711397.246001 39412672.483283
41245274.325280 39099619.819368
39044211.256773 39552230.937709
39826568.903608 39600681.258399
41745908.334861 39283223.331794
40419353.228199 39951881.566275
39374480.958563 39736514.229297
37909505.930636 38829415.026319
38869566.144146 39658157.553641
41179311.037528 38966749.459882
40062381.933204 39477309.992161
40044091.845803 39238422.148523
40352935.907771 39412283.634430
37843711.188815 38913223.366082
38911091.704537 39290172.416156
39161888.494808 39299591.369524
38974170.056678 39380423.107997
Satyajit Ghosh
Satyajit Ghosh le 1 Oct 2021
Hi, thank you for your help. I have fixed the issue. Now two implementations are equivalent. Thank you very much.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Problem-Based Optimization Setup dans Help Center et File Exchange

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by