47 lines
1.2 KiB
Scala
47 lines
1.2 KiB
Scala
function [N, x] = quasi_newton(x0);
|
|
// Function quasi_newton
|
|
// In: Start value
|
|
// Out: number of iteration steps, vector of solution
|
|
N=1;
|
|
xCur = x0;
|
|
xOld = x0;
|
|
epsilon = 10^(-8);
|
|
A = [3,4;1,6];
|
|
|
|
function [y] = f(x)
|
|
// eye(A) muss in matlab durch eye(2) ersetzt werden
|
|
yTemp = (A - x(3)*eye(A))*[x(1);x(2)];
|
|
lambda = x(1)^2+x(2)^2 - 1;
|
|
y = [ yTemp ; lambda];
|
|
endfunction
|
|
|
|
function [y] = jacobi(x)
|
|
y1 = [A(1,1)-x(3) , A(1,2) , -x(1)];
|
|
y2 = [A(2,1) , A(2,2)-x(3) , -x(2)];
|
|
y3 = [2*x(1) , 2*x(2) , 0];
|
|
y = [y1;y2;y3];
|
|
endfunction
|
|
|
|
function [y] = newton_next(x)
|
|
y = x + getDeltaX(x);
|
|
endfunction
|
|
|
|
function [y] = getDeltaX(x)
|
|
y = jac\(-f(x));
|
|
endfunction
|
|
|
|
jac = jacobi(x0);
|
|
xCur = newton_next(xOld);
|
|
x(:,N) = xCur;
|
|
while(abs(norm(xCur)-norm(xOld)) > epsilon & N < 60)
|
|
N = N + 1;
|
|
xOld = xCur;
|
|
// anpassen der angenäherten jacobi matrix
|
|
delta = getDeltaX(xOld); //schritt 1
|
|
xCur = newton_next(xOld); //schritt 2
|
|
//delta'*delta in matlab evtll durch norm(delta,2) austauschen
|
|
jac = jac + (1/(delta'*delta))*(f(xCur) - f(xOld) - jac*delta) * delta'; //schritt 4
|
|
x(:,N) = xCur;
|
|
end
|
|
endfunction
|