The following MATLAB script;
function [x,fx,xx] = newtons(f,x0,TolX,MaxIter,varargin) %newtons.m to solve a set of nonlinear eqs f1(x)=0, f2(x)=0, f3(x)=0,
%f4(x)=0,..
%input: f = 1^st-order vector ftn equivalent to a set of equations
% x0 = the initial guess of the solution
% TolX = the upper limit of |x(k) - x(k-1)|
% MaxIter = the maximum # of iteration
%output: x = the point which the algorithm has reached
% fx = f(x(last))
% xx = the history of x
h = 1e-4; TolFun = eps; EPS = 1e-6; fx = feval(f,x0,varargin{:}); Nf = length(fx); Nx = length(x0); if Nf ~= Nx, error('Incompatible dimensions of f and x0!'); end if nargin < 4, MaxIter = 200; end if nargin < 3, TolX = EPS; end xx(1,:) = x0(:).'; %Initialize the solution as the initial row vector
%fx0 = norm(fx); %(1)
for k = 1: MaxIter dx = -jacob(f,xx(k,:),h,varargin{:})\fx(:);%-[dfdx]^-1*fx
%for I = 1; 3 %damping to avoid divergence %(2)
%dx = dx/2; %(3)
xx(k + 1,:) = xx(k,:) + dx.'; fx = feval(f,xx(k + 1,:),varargin{:}); fxn = norm(fx); % if fxn < fx0, break; end %(4)
%end %(5)
if fxn < TolFun | norm(dx) < TolX, break; end %fx0 = fxn; %(6)
end x = xx(k + 1,:); if k == MaxIter, fprintf('The best in %d iterations\n',MaxIter), end function g = jacob(f,x,h,varargin) %Jacobian of f(x)
if nargin < 3, h = 1e-6; end h2 = 2*h; N = length(x); x = x(:).'; I = eye(N); for n = 1:N g(:,n) = (feval(f,x + I(n,:)*h,varargin{:}) ... -feval(f,x - I(n,:)*h,varargin{:}))'/h2; endwas to solve the following system of nonlinear equations given as; y(1) = 5-x(1)*(6^x(2)*exp(6*x(3))+12^x(2)*exp(12*x(3))+18^x(2)*exp(18*x(3))+24^x(2)*exp(24*x(3))+30^x(2)*exp(30*x(3)))-2*x(1)*x(4)*(6^x(2)*exp(-x(1)*6^x(2)*exp(6*x(3))+(6*x(3)))*(1-x(4)*exp(-x(1)*6^x(2)*exp(6*x(3))))^(-1)+12^x(2)*exp(-x(1)*12^x(2)*exp(12*x(3))+(12*x(3)))*(1-x(4)*exp(-x(1)*12^x(2)*exp(12*x(3))))^(-1)+18^x(2)*exp(-x(1)*18^x(2)*exp(18*x(3))+(18*x(3)))*(1-x(4)*exp(-x(1)*18^x(2)*exp(18*x(3))))^(-1)+24^x(2)*exp(-x(1)*24^x(2)*exp(24*x(3))+(24*x(3)))*(1-x(4)*exp(-x(1)*24^x(2)*exp(24*x(3))))^(-1)+30^x(2)*exp(-x(1)*30^x(2)*exp(30*x(3))+(30*x(3)))*(1-x(4)*exp(-x(1)*30^x(2)*exp(30*x(3))))^(-1))=0; y(2) = (log(6)+log(12)+log(18)+log(24)+log(30))+((x(2)+6*x(3))^(-1)+(x(2)+12*x(3))^(-1)+(x(2)+18*x(3))^(-1)+(x(2)+24*x(3))^(-1)+(x(2)+30*x(3))^(-1))-x(1)*(6^x(2)*exp(6*x(3))*log(6)+12^x(2)*exp(12*x(3))*log(12)+18^x(2)*exp(18*x(3))*log(18)+24^x(2)*exp(24*x(3))*log(24)+30^x(2)*exp(30*x(3))*log(30))-2*x(2)*x(4)*(6^x(2)*exp(-x(1)*6^x(2)*exp(6*x(3))+(6*x(3)))*log(6)*(1-x(4)*exp(-x(1)*6^x(2)*exp(6*x(3))))^(-1)+12^x(2)*exp(-x(1)*12^x(2)*exp(12*x(3))+(12*x(3)))*log(12)*(1-x(4)*exp(-x(1)*12^x(2)*exp(12*x(3))))^(-1)+18^x(2)*exp(-x(1)*18^x(2)*exp(18*x(3))+(18*x(3)))*log(18)*(1-x(4)*exp(-x(1)*18^x(2)*exp(18*x(3))))^(-1)+24^x(2)*exp(-x(1)*24^x(2)*exp(24*x(3))+(24*x(3)))*log(24)*(1-x(4)*exp(-x(1)*24^x(2)*exp(24*x(3))))^(-1)+30^x(2)*exp(-x(1)*30^x(2)*exp(30*x(3))+(30*x(3)))*log(30)*(1-x(4)*exp(-x(1)*30^x(2)*exp(30*x(3))))^(-1))=0; y(3) = (6*(x(2)+6*x(3))^(-1)+12*(x(2)+12*x(3))^(-1)+18*(x(2)+18*x(3))^(-1)+24*(x(2)+24*x(3))^(-1)+30*(x(2)+30*x(3))^(-1))-x(1)*(6^(x(2)+1)*exp(6*x(3))+12^(x(2)+1)*exp(12*x(3))+18^(x(2)+1)*exp(18*x(3))+24^(x(2)+1)*exp(24*x(3))+30^(x(2)+1)*exp(30*x(3)))-(6+12+18+24+30)-2*x(1)*x(4)*(6^(x(2)+1)*exp(-x(1)*6^x(2)*exp(6*x(3))+(6*x(3)))*(1-x(4)*exp(-x(1)*6^x(2)*exp(6*x(3))))^(-1)+12^(x(2)+1)*exp(-x(1)*12^x(2)*exp(12*x(3))+(12*x(3)))*(1-x(4)*exp(-x(1)*12^x(2)*exp(12*x(3))))^(-1)+18^(x(2)+1)*exp(-x(1)*18^x(2)*exp(18*x(3))+(18*x(3)))*(1-x(4)*exp(-x(1)*18^x(2)*exp(18*x(3))))^(-1)+24^(x(2)+1)*exp(-x(1)*24^x(2)*exp(24*x(3))+(24*x(3)))*(1-x(4)*exp(-x(1)*24^x(2)*exp(24*x(3))))^(-1)+30^(x(2)+1)*exp(-x(1)*30^x(2)*exp(30*x(3))+(30*x(3)))*(1-x(4)*exp(-x(1)*30^x(2)*exp(30*x(3))))^(-1))=0; y(4) = 5*(1-x(4))^(-1)-2*(exp(-x(1)*6^x(2)*exp(6*x(3)))*(1-x(4)*exp(-x(1)*6^x(2)*exp(6*x(3))))^(-1)+exp(-x(1)*12^x(2)*exp(12*x(3)))*(1-x(4)*exp(-x(1)*12^x(2)*exp(12*x(3))))^(-1)+exp(-x(1)*18^x(2)*exp(18*x(3)))*(1-x(4)*exp(-x(1)*18^x(2)*exp(18*x(3))))^(-1)+exp(-x(1)*24^x(2)*exp(24*x(3)))*(1-x(4)*exp(-x(1)*24^x(2)*exp(24*x(3))))^(-1)+exp(-x(1)*30^x(2)*exp(30*x(3)))*(1-x(4)*exp(-x(1)*30^x(2)*exp(30*x(3))))^(-1))=0; end
I have tried various starting values for x0 but the output is always NaN NaN NaN NaN.Please, i need help for the starting values. Thanks.
Best Answer