Newton method to optimize function, error
Show older comments
Hey guys,
im trying to use the newton method for optimizing my following function. I also used the Grid-search method, this worked totaly fine.
Now here is my try with the Newton method:
Imax=100; % Anzahl an maximalen Iterationen
tol=1;
i=1; % Zähler der Schleife
f=[diff(logL,b);diff(logL,y);diff(logL,A);diff(logL,B)]; % Partielle Ableitungen von logL
J=[diff(f,b) diff(f,y) diff(f,A) diff(f,B)]; % Aufstellen der Jacobi-Matrix
X=zeros(Imax,length(J)); % Matrix erstellen
X(1,:)= [27 3 0.04 0.3]; % Startwerte für 1.Iteration
while tol>=1e-9 % Toleranz
% Einsetzen der Werte für Startwertvariablen in Gleichung
fvar=subs(f,{'b' 'y' 'A' 'B'},{X(i,1) X(i,2) X(i,3) X(i,4)});
f_var=subs(J,{'b' 'y' 'A' 'B'},{X(i,1) X(i,2) X(i,3) X(i,4)});
% Evaluieren für Werte für Startwertvariablen
fvalue=eval(fvar);
f_value=eval(f_var);
if max(max(isnan(f_value))) == 1
break
end
% Neuer X Lösungsvektor
*| |*X(i+1,:)=([X(i,1) X(i,2) X(i,3) X(i,4)]'-f_value\fvalue)';*||*
% Abweichung zwischen neuen und alten Werten
tol=abs([X(i,1) X(i,2) X(i,3) X(i,4)] - [X(i+1,1) X(i+1,2) X(i+1,3) X(i+1,4)]);
if i>Imax % Abbruchkriterium
break
end
i=i+1;
end
fprintf('Endergebnis : '); % Endergebnis
[X(i,1) X(i,2) X(i,3) X(i,4)]
Im getting the error message:
> In zwei_parametrig_ohneAB_NEWTON_VERFAHREN (line 54)
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 4.698726e-31.
> In zwei_parametrig_ohneAB_NEWTON_VERFAHREN (line 54)
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 7.517962e-30.
> In zwei_parametrig_ohneAB_NEWTON_VERFAHREN (line 54)
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.202871e-28.
> In zwei_parametrig_ohneAB_NEWTON_VERFAHREN (line 54)
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.924598e-27.
> In zwei_parametrig_ohneAB_NEWTON_VERFAHREN (line 54)
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 3.079253e-26.
> In zwei_parametrig_ohneAB_NEWTON_VERFAHREN (line 54)
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 4.926967e-25.
And after that, MATLAB gives me "wrong" solutions:
-0.0000 -102.4938 108.4393 -217.2624
By googling the error message i know, that there must be something wrong with my matrix i have marked in the code below. Where is the problem?
Your sincerly, Ronald Singer
1 Comment
Jan
on 3 Aug 2017
Note: "any(isnan(f_value(:)))" is nicer than "max(max(isnan(f_value))) == 1".
Accepted Answer
More Answers (0)
Categories
Find more on Matrix Computations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!