P = [23.05376671, 2.083554005; 23.774115313,2.14486932; 25.031876523,1.7269246;...
25.956640, 2.83704978; 27.357839,4.2577748; 27.865011,4.813969; ...
29.07254042, 4.0247780; 30.0714742,4.41801357; 30.9875855,5.54587904; ...
32.1409034, 5.96687696; 33.06714971,5.36700523; 34.0717238,6.95209411;...
35.04888937,7.16387720; 36.072688,7.07862363; 37.0293871,7.335086878;...
38.08883956,7.641171957; 38.893112954,8.226200522; 39.7055715,9.57535211;...
41.0325190, 9.14802867; 42.1370298,9.215393; 42.9897757553914,10.2534211833571;...
44.0319206739166, 10.925143438655; 44.9135120082676,11.2379794815215;...
45.9835120980791, 11.9510829150115; 47.1093265669039,12.5937093190458;...
47.9136347178011, 12.6309436364522; 48.8785882956385,12.6045997034053;...
49.9993150671897, 14.1130521233139; 50.9230334086246,14.098551525104;...
51.9774415597729, 14.8469424555258; 52.8910935704948,14.863022985666;...
54.0552527021112, 15.7402440871523; 55.1544211895504,15.7843724532702;...
55.8508409689362, 15.9030792650961; 56.893841826668,17.5901828896008;...
57.9384398118533, 17.3992307134816];
a0 = 1.0; b0 = -10.5; % initial point (a,b) and h
% initialize the parameters for iterations
% create list of values of the cost function, etc.
CostFun = zeros(numMaxSteps,1); NormGrad = zeros(numMaxSteps,1);
Bval = zeros(numMaxSteps,1); Aval = zeros(numMaxSteps,1); Eps = zeros(numMaxSteps,1);
for k = 1:numMaxSteps % k = present number of descent step
eps = 0.001; % the initial step size which we try to apply at each step
S = costlin(P,a,b); % calculate the error function (defined at the end of this script)
gradS(1) = (costlin(P,a+h,b)-costlin(P,a-h,b))/(2*h); % calculate the approximation of partial derivatives
gradS(2) = (costlin(P,a,b+h)-costlin(P,a,b-h))/(2*h);
NormGrad(k) = norm(gradS);
while costlin(P,a-eps*gradS(1),b-eps*gradS(2))>S % we check if the next
% error function S is bigger than in the previous step. This happens when
eps=eps/2.5; % if this happens, we reduce the step size 'eps' at the current step
a=a-eps*gradS(1); % calculate values of (a,b) for the next step
plot(P(:,1),P(:,2),'ro',Color='red') % we plot the points P_1,...,P_N on the plane (x,y)
plot(t,a0*t+b0,Color='red',LineWidth=1.8) % we plot the line with the initial parameters (a,b)
plot(t,Aval(k)*t+Bval(k),'--',LineWidth=0.5) % we plot few more lines
plot(t,a*t+b,Color='blue',LineWidth=1.8) % we plot the approximating line with the final values (a,b)
title('line approximation')
plot(CostFun(1:numS),'.-')
title('error function (first 60 steps)')
plot(Aval(1),Bval(1),'b*')
plot(0.4475439589,-8.73456329,'ro',Color='red') % plot the absolute minimum computed above
legend('ini point','the path on (a,b)','target point = abs. min',...
'Location','northoutside')
aa = a-0.3:ha:a+0.6; bb = b-2:hb:b+2;
S=S+(A(i,j)*P(k,1)-P(k,2)+B(i,j))^2/(A(i,j)^2+1)/N;
view(8,23) %point of view
plot3(Aval(1:10), Bval(1:10), CostFun(1:10),'ro',MarkerSize=5,MarkerFaceColor='r')
plot3(Aval(1:10), Bval(1:10), CostFun(1:10),'y--')
plot3(Aval(11:500:end), Bval(11:500:end), CostFun(11:500:end),'ro',...
MarkerSize=5,MarkerFaceColor='r')
title('a-b iteration steps in 3D')
P=[226.98, 681.63; 232.86, 692.24; 233.97, 702.84; 234.02, 711.39; 244.26, 724.88;...
258.64, 708.33; 244.26, 724.88; 244.85, 734.19 ;246.94, 748.99; 262.28, 746.91;...
269.71, 759.26;290.51, 768.915;304.33,784.773; 327.881, 755.707; 348.513,761.045;...
362.37,738.539;367.863,722.165; 343.84, 713.01; 344.0, 685.99;353.68,667.17;...
333.49,665.9;316.58,648.41;308.76, 622.39; 284.845, 622.275;260.88 617.84; ...
261.284, 644.716; 233.422, 634.545;226.903,642.539;221.35, 651.76;220.16, ...
% initial point (xc,yc,a,b,alpha) and step
xc=300; yc=710; a=55; b=35; alpha=pi/3;
[Sini,Xtini,Ytini]=cost(P,xc,yc,a,b,alpha); %initial ellipse
p(1) = xc; p(2) = yc; p(3) = a; p(4) = b; p(5) = alpha; % p = the list of the 5 parameters of the ellipse
CostF = zeros(N,1); NormGr = zeros(N,1); % initialize result variables to zero
Ep=zeros(N,1); Ellipse_Param = zeros(N,5);
for k = 1:numMaxSteps % initiate the descent iterations
eps = 0.015; % the initial step size which we try to apply at each step
[S,Xt,Yt] = cost(P,p(1),p(2),p(3),p(4),p(5));
% calculate the 5 components of the gradient vector Grad (S):
gradS(1)=(cost(P,p(1)+h,p(2),p(3),p(4),p(5))-cost(P,p(1)-h,p(2),p(3),p(4),p(5)))/(2*h);
gradS(2)=(cost(P,p(1),p(2)+h,p(3),p(4),p(5))-cost(P,p(1),p(2)-h,p(3),p(4),p(5)))/(2*h);
gradS(3)=(cost(P,p(1),p(2),p(3)+h,p(4),p(5))-cost(P,p(1),p(2),p(3)-h,p(4),p(5)))/(2*h);
gradS(4)=(cost(P,p(1),p(2),p(3),p(4)+h,p(5))-cost(P,p(1),p(2),p(3),p(4)-h,p(5)))/(2*h);
gradS(5)=(cost(P,p(1),p(2),p(3),p(4),p(5)+h)-cost(P,p(1),p(2),p(3),p(4),p(5)-h))/(2*h);
while ( cost(P,p(1)-eps*gradS(1),p(2)-eps*gradS(2),p(3)-eps*gradS(3),p(4)-eps*gradS(4),p(5)-eps*gradS(5)) > S) % we check if the next
% error function S is bigger than in the previous step. This happens when
eps=eps/2.5; % if this happens, we reduce the step size 'eps' at the current step
CostF(k) = S; NormGr(k) = norm(gradS); Ep(k) = eps;
p=p-eps*gradS; % change the vector of the 5 parameters of the ellipse
plot(Xtini,Ytini, Color='red',LineWidth=2) % plotting the initial ellipse
for r = [3,7,30,50,90,120,180,240,320,380,500,1000] % we plot only few ellipses
[S,Xt,Yt]=cost(P,p(1),p(2),p(3),p(4),p(5));
plot(Xt,Yt,Color='blue',LineWidth=2) % plotting the final ellipse
title('Ellipse evolution')
plot(Xt,Yt,Color='blue',LineWidth=2) % plotting the final ellipse
plot(CostF) % plotting the graph of S against the number of iterations
plot(NormGr(1:1500)) % plotting the graph of norm(grad(S)) against the number of iterations
title('norm of Grad(S) (1500 steps)')
title('step size eps for the first 1200 steps')
Sfin=77.6994; % Specify the final value S_f of the cost function
p=[291.5469,697.6231,81.3316,56.6900,-2.1785]; % and the final values of the parameters of the final ellipse E_f
Dist=[]; Pnew=[]; % create the list of the squared distances between all the poins P_i and E_f
Di=cost(P(i,:), p(1),p(2),p(3),p(4),p(5)); % calculating the squared distance between the point P_i and E_f
Pnew=[Pnew;P(i,:)]; % create a new list of the points P_i which are not far from the ellipse E_f
plot(Dist,'ro',Color='red')
plot([1,32],[Sfin,Sfin],LineWidth=2,Color='blue')