% newtn - Program to solve a system of nonlinear equations
% using Newton's method. Equations defined by function fnewt.
clear; help newtn; % Clear memory; print header
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);
subplot(121) % Plot iteration steps, x(1) vs. x(2)
plot(xplot(1,:),xplot(2,:),'o',...
xplot(1,:),xplot(2,:),'-',x(1),x(2),'*');
xlabel('x'); ylabel('y');
subplot(122) % Plot iteration steps, x(1) vs. x(3)
plot(xplot(1,:),xplot(3,:),'o',...
xplot(1,:),xplot(3,:),'-',x(1),x(3),'*');
xlabel('x'); ylabel('z');
subplot(111)