Newton's method for 2 dimension vectors
Mostra commenti meno recenti
I want to write a code which gives me the roots of a vector function with sing the Newton's method.
that's what I wrote:
%%file newton2d.m
function [out] = newton2d (f,Jf,x,y, eps)
k=0;
Jf = jacobian(f, [x, y]) %error
h = f(x,y)./Jf(x,y); %error
while abs(h)< eps && (k < 1000)
h = f(x,y)./Jf(x,y);
[x,y] = [x,y] - h;
k = k+1;
end
out = [x,y];
end
But there is a problem with the jacobian: i don't know where to insert it?
What can I do to make newton2d function work?
Thank you !
Risposte (1)
LIke this?
% Functions
f = @(XY) [XY(1).^3 - 3*XY(1).*XY(2).^2 - 1;
3*XY(1).^2.*XY(2) - XY(2).^3];
J = @(XY) [3*XY(1).^2 - 3*XY(2).^2, -6*XY(1)*XY(2);
6*XY(1).*XY(2), 3*XY(1).^2 - 3*XY(2).^2];
% Initial guesses
XY = [2; 1];
tol = 10^-6;
err = 1;
maxits = 100;
its = 0;
while (err>tol) && (its<maxits)
XYold = XY;
XY = XYold - J(XYold)\f(XYold);
err = norm(XY-XYold);
its = its+1;
end
x = XY(1); y = XY(2);
disp('parameter values:')
disp(['x = ',num2str(x), ' y = ', num2str(y)])
disp('function values:' )
disp(f(XY))
2 Commenti
Kevser Cifci
il 19 Set 2021
More like this then:
% Functions
f = @(xy) [xy(1).^3 - 3*xy(1).*xy(2).^2 - 1;
3*xy(1).^2.*xy(2) - xy(2).^3];
% Jacobian of f
J = @(xy) [3*xy(1).^2 - 3*xy(2).^2, -6*xy(1)*xy(2);
6*xy(1).*xy(2), 3*xy(1).^2 - 3*xy(2).^2];
% Initial guesses
xy = [2; 1];
xy = newton2d(f,J,xy);
x = xy(1); y = xy(2);
disp('parameter values:')
disp(['x = ',num2str(x), ' y = ', num2str(y)])
disp('function values:' )
disp(f(xy))
function xy = newton2d(f,J,xy)
tol = 10^-6;
err = 1;
maxits = 100;
its = 0;
while (err>tol) && (its<maxits)
xyold = xy;
xy = xyold - J(xyold)\f(xyold);
err = norm(xy-xyold);
its = its+1;
end
end
Categorie
Scopri di più su Get Started with MATLAB in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!