Newton's Method for nonlinear system vector operation.
Ältere Kommentare anzeigen
when i was doing newton's method for nonlinear system, when I entered following code it tells me that it could not do subtraction between two vectors with different dimension. The thing is F is a 2x1 vector, and J is jacobian matrix of F which is 2x2. so I dont know what is going on with my code. the following is the code.
a = newton(@F, @J, [1,1])
=========================================
function y = newton(f,j,x)
step = 1;
finalstep = 10;
y = zeros (2,1);
while step < finalstep
d = j(x)\(-f(x));
y = x + d;
disp(y);
x = y;
end
end
==========================================
function f = F(x)
x1 = x(1);
x2 = x(2);
f = zeros(2,1);
f(1) = x1^2-x2^2+2*x2; % f1(x1,x2)
f(2) = 2*x1+x2^2-6; % f2(x1,x2);
end
==========================================
function j = J(x)
x1 = x(1);
x2 = x(2);
j = zeros(2,2);
j(1,1) = 2*x1; % df1x1
j(1,2) = -2*x2+2; % df1x2
j(2,1) = 2; % df2x1
j(2,2) = 2*x2; % df2x2;
end
Akzeptierte Antwort
Weitere Antworten (1)
Andrei Bobrov
am 25 Sep. 2014
Bearbeitet: Andrei Bobrov
am 25 Sep. 2014
function x = newtonf(x,finalstep)
function out = F(x)
out = [x(1).^2-x(2).^2+2*x(2);
2*x(1)+x(2).^2-6];
end
function out = J(x)
out = [2*x(1),-2*x(2)+2;
2, 2*x(2) ];
end
step = 1;
y = [];
while step < finalstep
d = -J(x)\F(x);
x = x + d;
y = [y, x];
step = step + 1;
end
disp(y);
end
Kategorien
Mehr zu Symbolic Math Toolbox finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!