Need help solving this in matlab please! Thank you!!! Write a Matlab program to
ID: 3786230 • Letter: N
Question
Need help solving this in matlab please!
Thank you!!!
Write a Matlab program to implement the multidimensional version of Newton's method. For simplicity, let x = [x y] and the goal is to find an (approximate) solution to the equation f(x) = 0, where f has two components and the symbol 0 in (1) is actually 0 = [0 0]. For example, if f(x) = [y^2 - x^2 - 3 x^2 + y - 3], then a solution of (1) is x = [1 2]. Near the top of your program should be your function f, your initial guess x_0, the maximum number of iterations N, and the tolerance element. You may need to research vector norms and Matlab's associated norm command.Explanation / Answer
Matlab function Newton_multidimensional.m
function Newton_multidimensional(f,df,x0,tol,n)
iter=0; % Initilizing the iter variable
u=feval(f,x0); % evaluating f(x0)
v=feval(df,x0);% Evaluating df(x0)
err=norm(vu); % Getting error
% loop til error less then tolerence or iteration grater than n or devision
% by zeor occurs
while (err>tol) && (iter<=n) && rank(v)==length(x0)
x1=x0-vu;% The new value
err=norm(x1-x0);% Estimating the error
x0=x1;% replacing previous iteration value with new value
u=feval(f,x0); % evaluating f(x0)
v=feval(df,x0);% Evaluating df(x0)
iter=iter+1; % Increasing the iteration index
end
if (rank(v)~=length(x0)) % Checking division by zero occurs or not
disp(' division by zero')
elseif (iter>n) % Checking convergence of method
disp(' Method failed to converge')
else % reult to the screen
fprintf('Root = %f %f ',x1);
fprintf('Number of iteration tanen = %d ',iter);
end
end
Calling the function Newton_multidimensional.m
f = @(X) [X(2).^2-X(1).^2-3;X(1).^2+X(2)-3]; % The function f
df = @(X) [-2*X(1), 2*X(2);2*X(1), 1]; % Jacobian of f
x0 = [0;0]; % Initial guess
tol = 10^-6; % Tolerence
N = 100; % Maximum number of iteration
Newton_multidimensional(f,df,x0,tol,N)
OUTPUT
Root =
1.000000
2.000000
Number of iteration tanen = 5