Michael Scholz 0fe686245c CE update
2013-02-20 16:20:56 +01:00

47 lines
939 B
Scala
Executable File

function [N, matrixX] = newton(x0)
// Function newton
// In: Start value
// Out: number of iteration steps, vector of solution
// Todo: Replace below code with Newton method
bool=1;
N=1;
x=x0;
xTmp = x0;
e = 10^(-8);
matrixX = []; //matrix we use for iterations
matrixA = [3 4;1 6];
//define function f(x) as vectorfunction:
function [y] = f(x)
y = [(matrixA(1,1)-x(3))*x(1) + matrixA(1,2)*x(2) ;
matrixA(2,1)*x(1) + (matrixA(2,2)-x(3))*x(2) ;
(x(1)^2-1) + (x(2)^2 -1)];
endfunction
while(bool == 1)
//calculate x (of current iteration)
jac = numdiff(f, x); //jacobi-matrix
deltaX = jac\(-f(x));
x = x + deltaX;
//add x to output matrix
matrixX(:,N) = x;
//check criteria
if(N == 60 | (abs(norm(xTmp)-norm(x)) < e & N >1))
bool = 0; //-> will cancel the while-loop
end
N = N+1; //increase N
xTmp = x;
end
N = N-1;
endfunction