Write a function with the header [s] = myNewton 3 (f1, f2, f3, x, eps) which sol
ID: 2082092 • Letter: W
Question
Write a function with the header [s] = myNewton 3 (f1, f2, f3, x, eps) which solves a system of three non-linear equations (given by function handles f1, f2, and f3) given an initial guess vector x, and returns the root vector s. f1, f2 and f3 should be handles to functions of x (and x will be a column vector in R^3). Use a convergence criteria on the incrementor (delX) such that the algorithm keeps going as long as abs (delX) > eps. CHECK YOUR ALGORITHM FOR INFINITE LOOPS! Test case: f_1 (x, y, z) = x^5 + y^3 z^4 + 1 f_2 (x, y, z) = x^2 yz f_3 (x, y, z) = z^4 - 1 Try your function with these initial guess vectors: x = {-100 0 -100} x = {-100 0 100} >> f1 = @ (x) x(1).^5 + x(2).^3*x(3).^4 + 1; >> f2 = @ (x) x(1).^2*x(2)*x(3); >> f3 = @ (x) x(3).^4 - 1; >> x = [-100; 0; -100]; >> s = myNewton3 (f1, f2, f3, x, 1e - 7) s = -1 0 1 >> x = [-100; 0; 100]; >> s = myNewton3 (f1, f2, f3, x, 1e - 7) s = -1 0 1Explanation / Answer
function [x,iter]=newton(x0,f,fp) % newton-raphson algorithm
N = 100; eps = 1.e-7; % define max. no. iterations and error
f1 = @(x) x(1).^5 + x(2).^3*x(3).^4 + 1;
f2 = @(x) x(1).^2*x(2)*x(3);
f3 = @(x) x(3).^4-1;
x = [-100; 0; -100];
Jacobian J
function [x,iter] = newtonm(x0,f,J)
% Newton-Raphson method applied to a
% system of linear equations f(x) = 0,
% given the jacobian function J, with
% J = del(f1,f2,...,fn)/del(x1,x2,...,xn)
% x = [x1;x2;...;xn], f = [f1;f2;...;fn]
% x0 is an initial guess of the solution
N = 100; % define max. number of iterations
epsilon = 1e-7; % define tolerance
maxval = 10000.0; % define value for divergence
xx = x0; % load initial guess
while (N>0)
JJ = feval(J,xx);
if abs(det(JJ))<epsilon
error('newtonm - Jacobian is singular - try new x0');
abort;
end;
xn = xx - inv(JJ)*feval(f,xx);
if abs(feval(f,xn))<epsilon
x=xn;
iter = 100-N;
return;
if abs(feval(f,xx))>maxval
iter = 100-N;
disp(['iterations = ',num2str(iter)]);
error('Solution diverges');
abort;
end;
N = N - 1;
xx = xn;
end;
error('No convergence after 100 iterations.');
abort; % end function
function [f] = f2(x)
% f2(x) = 0, with x = [x(1);x(2)]
% represents a system of 2 non-linear equations
f1(x,y,z) = x^5 + y^3 z^4 + 1;
f2(x,y,z) = x^2 y z;
f3(x,y,z) = z^4 - 1;
f = [f1;f2;f3];
% end function
function [J] = jacob3x3(x)
% Evaluates the Jacobian of a 3x3
% system of non-linear equations
J(1,1) = 2*x(1);
J(1,2) = 2*x(2);
J(1,3) = x(2);
J(2,2) = x(1);
J(2,1) = x(1);
J(2,2) = x(1);
J(2,3) = x(1);
J(3,1) = x(1);
J(3,2) = x(1);
J(3,3) = x(1);
% end function
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.