% newtn - Program to solve a system of nonlinear equations
% using Newton's method. Equations defined by function fnewt.
clear; help newtn;
x = input('Enter the initial guess (row vector) - ');
a = input('Enter the parameter a - ');
nstep = 10; % Number of iterations before stopping
%%%%% MAIN LOOP %%%%%
for istep=1:nstep,
xplot(:,istep) = x(:); % Save plot variables
[f D] = fnewt(x,a); % fnewt returns value of f and D
dx = f/D; % Find dx by Gaussian elimination
x = x - dx; % Newton iteration for new x
end
xplot(:,nstep+1) = x(:); % Save plot variables
fprintf('After %g iterations the root is\n',nstep);
disp(x);
plot3(xplot(1,:),xplot(2,:),xplot(3,:),'o',...
xplot(1,:),xplot(2,:),xplot(3,:),'-',...
x(1),x(2),x(3),'*');
xlabel('x'); ylabel('y'); zlabel('z');
axis(axis); % Freeze axis settings
spinview(10,3); % Rotate plot 10 times; pause 3 seconds
axis('auto'); % Restore axis auto-scaling