%Matlab code for system of equations
clear all
close all
%All initial guess for x1
x1=[5;4;3;1];
%solution using newton systems
x = newtonsys(@(x1) f(x1),x1);
fprintf('For initial guess x=%2.2f , y=%2.2f ,z= %2.2f and
lambda=%2.2f\n',...
x1(1),x1(2),x1(3),x1(4))
fprintf('\nroot of the equation is x=%2.2f , y=%2.2f ,z= %2.2f
and lambda=%2.2f\n',...
x(1,end),x(2,end),x(3,end),x(4,end))
x1=[50;40;30;100];
%solution using newton systems
x = newtonsys(@(x1) f(x1),x1);
fprintf('\n\nFor initial guess x=%2.2f , y=%2.2f ,z= %2.2f and
lambda=%2.2f\n',...
x1(1),x1(2),x1(3),x1(4))
fprintf('\nroot of the equation is x=%2.2f , y=%2.2f ,z= %2.2f
and lambda=%2.2f\n',...
x(1,end),x(2,end),x(3,end),x(4,end))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Matlab function for Newton system
function x = newtonsys(f,x1)
funtol = 1000*eps; xtol = 1000*eps;
maxitr=40;
x =x1(:);
[y,j]=f(x1);
dx=Inf;
k=1;
while (norm(dx) > xtol) &&
(norm(y)> funtol) && (k< maxitr)
dx= -(j\y);
x(:,k+1) = x(:,k) +
dx;
k=k+1;
[y, j]= f(x(:,k));
end
if k==maxitr
warning('Maximum number
of iterations reached.')
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%function for jacobian and residual
function [y,j]= f(x1)
y(1,1) = x1(1)-5-(x1(4)*x1(1))/25;
y(2,1) = x1(2)-4-(x1(4)*x1(2))/16;
y(3,1) = x1(3)-3-(x1(4)*x1(3))/9;
y(4,1) =
(((x1(1)).^2)/25)+(((x1(2)).^2)/16)+(((x1(3)).^2)/9)-1;
j=[1-((x1(4))/25) 0 0 (-x1(1))/25;...
0 1-((x1(4))/16) 0
(-x1(2))/16;...
0 0 1-((x1(4))/9)
(-x1(3))/9;...
(2*x1(1))/25
(2*x1(2))/16 (2*x1(3))/9 0];
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
in matlab sthat this n is hardly different from the scalar version in Section 4.3 . The root estimates are stored as columns in an array . The Newton step is calculated using a backslash. The fu...
in matlab -Consider the equation f(x) = x-2-sin x = 0 on the interval x E [0.1,4 π] Use a plot to approximately locate the roots of f. To which roots do the fol- owing initial guesses converge when using Function 4.3.1? Is the root obtained the one that is closest to that guess? )xo = 1.5, (b) x0 = 2, (c) x.-3.2, (d) xo = 4, (e) xo = 5, (f) xo = 27. Function 4.3.1 (newton) Newton's method...