I'm tryin to solve two non-linear equations with two unknowns with the Newton-Raphson method in MATLAB. Here is my matlab code:
f = @(x)[ x(1)^2+x(2)^2;
2*x(1)-x(2)];
J = @(x)[ 2*x(1), 2*x(2);
2, -1];
tol = 1e-4; % Or some other tolerance
err = 1000; % Any value larger than tol
x = 0.01; % However this is defined.
iter = 1; max_iter = 30; % Or whatever.
while (err > tol)
delta_x = J(x)\(-f(x)); % Compute x_{n+1}-x_n
err = norm(delta_x);
x = x + delta_x;
iter = iter + 1;
[iter x']; % This line simply outputs the current iteration and the solution. You can dress this up by using sprintf if you like.
if (iter > max_iter)
disp 'Failed to converge';
break;
end
end
Why does MATLAB show “Index exceeds matrix dimensions.”?
Jandfusex(2), while you call them with a scalarx. There is nox(2). - Andras Deak