Metodo de Newton Sistemas no lineales
Publicado por wil (6 intervenciones) el 02/04/2020 17:35:00
Hola, copie el siguiente codigo en matlab de un articulo y no compila, donde esta el error ????
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
% Newton's Method
format long;
n=5; % set some number of iterations, may need adjusting
f = @(x) [ 3*x(1)-cos(x(2)*x(3))-0.5
x(1).^2-81*(x(2)+0.1)^2 + sin(x(3))+1.06
exp(-x(1)*x(2))+20*x(3)+((10*pi-3)/3)]; % the vector
function,3x1
% the matrix of partial derivatives
df = @(x) [3 x(3)*sin(x(2)*x(3)) x(2)*sin(x(2)*x(3))
2*x(1) -162*x(2)-16.2 cos(x(3))
-x(2)*exp(x(1)*x(2)) -x(1)*exp(x(1)*x(2)) 20];% 3x3
x = [0.1;0.1;-0.1]; % starting guess
for i = 1:n
y = -df(x)\f(x) % solve for increment, similar A\b
x = x + y % add on to get new guess
f(x) % see if f(x) is really zero
end
1
2
3
4
5
6
7
8
9
function y = F(x)
x1 = x(1);
x2 = x(2);
x3 = x(3);
y = zeros(3,1);
y(1) = 3*x(1)-cos(x(2)*x(3))-0.5; % f1(x1,x2)
y(2) = x(1).^2-81*(x(2)+0.1)^2 + sin(x(3))+1.06;
y(3) = exp(-x(1)*x(2))+20*x(3)+((10*pi-3)/3);
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
function x = NewtonMethod(funcF, JacobianF, n)
F = funcF;
J = JacobianF;
x = [0.1 0.1 -0.1]';
Iter = 1;
MaxIter = 100;
TOL = 1e-5;
while Iter < MaxIter
disp(['Iter = ' num2str(Iter)]);
y = J(x)\(-F(x));
x = x+y;
if norm(y,2)<TOL
break;
end
disp(['x = ' num2str(x')]);
end
if Iter >= MaxIter
disp('Maximum number of iteration exceeded!');
end
end
1
2
3
function newtonSol
x = NewtonMethod(@F,@J,2);
end
Valora esta pregunta
0