Skip to content

Instantly share code, notes, and snippets.

@asw456
Created August 11, 2013 10:09
Show Gist options
  • Save asw456/6204293 to your computer and use it in GitHub Desktop.
Save asw456/6204293 to your computer and use it in GitHub Desktop.
emth 271 lab 3
function results = mymultinewtfunction(x, func, niter, tol)
for i = 1:niter
n = max(size(x));
%f = [(log(x(1)) - x(2)) , ((x(1)^2+2*x(1)-24)/25 - x(2))];
%J = [1/x(1), -1 ; 2*x(1)/25+2/25 , -1];
f = func(x);
J = zeros(n);
for k = 1:n
for l = 1:n
e = zeros(n,1);
e(l) = 1;
h = 1e-8;
pd = (func(x+h*e) - func(x))/h;
J(k,l) = pd(k);
end
end
deltax = J\-f';
xnew = x + deltax;
relativeError = norm(xnew - x,Inf)/norm(xnew,Inf);
if relativeError <= tol
break
end
verbose = 0;
if verbose && i < 6
f
J
xnew
relativeError
end
x = xnew;
if i == niter
results = 1;
fprintf('\nmax iterations reached\n\n')
return
end
end
fprintf('\ninterations = %i\n\n',i)
results = x;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment