0% encontró este documento útil (0 votos)
747 vistas4 páginas

Código Fuente Newton Raphson Multivariable

Este documento presenta un programa que implementa el método de Newton-Raphson para resolver sistemas de ecuaciones no lineales de 2, 3 o 4 variables. El programa solicita al usuario ingresar las ecuaciones y valores iniciales, calcula la matriz jacobiana, evalúa la matriz y función en los valores iniciales, y luego itera hallando nuevos valores que minimizan el error hasta alcanzar la tolerancia requerida. El programa imprime los resultados de cada iteración en tablas.

Cargado por

Abdullah Khan
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como PDF, TXT o lee en línea desde Scribd
0% encontró este documento útil (0 votos)
747 vistas4 páginas

Código Fuente Newton Raphson Multivariable

Este documento presenta un programa que implementa el método de Newton-Raphson para resolver sistemas de ecuaciones no lineales de 2, 3 o 4 variables. El programa solicita al usuario ingresar las ecuaciones y valores iniciales, calcula la matriz jacobiana, evalúa la matriz y función en los valores iniciales, y luego itera hallando nuevos valores que minimizan el error hasta alcanzar la tolerancia requerida. El programa imprime los resultados de cada iteración en tablas.

Cargado por

Abdullah Khan
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como PDF, TXT o lee en línea desde Scribd

% PROGRAMA "METODO DE NEWTON RAPHSON MULTIVARIABLE

% CREADO POR CID RIOS GERARDO ALEXIS


% LIMPIAR VARIABLES Y PANTALLA
clear, clc
% SE CREA MENU PARA SELECCIONAR EL TIPO DE SISTEMA
while true
%disp(' Creado por Alexis Cid')
clear
disp(' Metodo de Newton Raphson Multivariable')
fprintf('\n ')
disp(' Elija una de las opciones siguientes:')
disp(' (1) Para un sistema de 2 ecuaciones')
disp(' (2) Para un sistema de 3 ecuaciones')
disp(' (3) Para un sistema de 4 ecuaciones')
disp(' (0) Salir')
fprintf('\n ')
valor = input('Ingrese opcion: ');
switch valor
case 1
% SE LIMPIA PANTALLA
clc
% SE DECLARAN LAS VARIABLES SIMBOLICAS
syms x1 x2
% ENTRADA DE DATOS
A1 = input('Ingrese ecuacion 1 ');
A2 = input('Ingrese ecuacion 2 ');
tol= input('ingrese toleracia ');
% SE CALCULA EL JACOBIANO
J=jacobian([A1,A2,],[x1 x2]);
% SE IMPRIME EL JACOBIANO
fprintf('\n\t Matriz Jacobiana \n\n')
pretty(J)
% SE TRANSFORMA EL JACOBIANO A UNA FUNCION INLINE PARA PODERLA
% EVALUAR CADA FUNCI?N EN UNO
JAC = inline(J);
B = inline([-A1,-A2]');
x1=1;
x2=1;
INJAC=JAC(x1,x2);
INB=B(x1,x2);
% SE RESUELVE LA MATRIZ RESULTANTE DE LA EVALUACION POR EL METODO
% DE MATRIZ INVERSA
SOLUCION=INJAC\INB;
h1=INJAC(:,1);
h2=INJAC(:,2);
igual=['=' '=']';
% SE IMPRIME LA PRIMERA ITERACION
fprintf('\n\t Evaluaci?n de la matriz Jacobiana y de la funci?n negativa \n')
fprintf('\t Cuando las incognitas valen 1 \n')
fprintf('\t [J][h]=[-F] \n\n')
format long
T=table(h1,h2,igual,INB,SOLUCION);
disp(T)
format
n=0;
% SE INGRESA UN ERROR ALTO PARA PODER INICIAR EL CICLO WHILE
err1=100;
err2=100;
% SE IMPRIMEN ENCABEZADOS DE LA TABLA
fprintf('\n\t I\t\t X1\t\t\t X2\t\t\t EX1\t\t EX2\t\t\n')
fprintf('\t %d\t %+.6f\t %+.6f\t NO APLICA NO APLICA\n', n, x1, x2)
while true
%SE INICIA LA CONDICION PARA DETENER EL CICLO
if (err1<=tol) && (err2<=tol)
break
else
n=n+1;
INJAC=JAC(x1,x2);
INB=B(x1,x2);
SOL=INJAC\INB;
h1=SOL(1,1);
h2=SOL(2,1);
X1=x1+h1;
X2=x2+h2;
err1=abs(x1-X1);
err2=abs(x2-X2);
fprintf('\t %d\t %+.6f\t %+.6f\t %.4f\t %.4f\n', n, X1, X2, err1, err2)
% SE ACTUALIZNA LOS VALORES DE LAS NUEVAS X's
x1=X1;
x2=X2;
disp(INJAC)
disp(SOL)
end
end
case 2
clc
syms x1 x2 x3
A1 = input('Ingrese ecuacion 1 ');
A2 = input('Ingrese ecuacion 2 ');
A3 = input('Ingrese ecuacion 3 ');
tol= input('ingrese toleracia ');
J=jacobian([A1,A2,A3],[x1 x2 x3]);
fprintf('\n\t Matriz Jacobiana \n\n')
pretty(J)
JAC = inline(J);
B = inline([-A1,-A2,-A3]');
x1=0.5;
x2=530;
x3=530;
INJAC=JAC(x1,x2);
INB=B(x1,x2,x3);
SOLUCION=INJAC\INB;
h1=INJAC(:,1);
h2=INJAC(:,2);
h3=INJAC(:,3);
igual=['=' '=' '=']';
fprintf('\n\t Evaluaci?n de la matriz Jacobiana y de la funci?n negativa \n')
fprintf('\t Cuando las incognitas valen 1 \n')
fprintf('\t [J][h]=[-F] \n\n')
format long
T=table(h1,h2,h3,igual,INB,SOLUCION);
disp(T)
format
n=0;
err1=100;
err2=100;
err3=100;
%fprintf('\n\t I\t\t X1\t\t\t X2\t\t\t X3\t\t EX1\t\t EX2\t\t EX3\n')
%fprintf('\t %d\t %+.6f\t %+.6f\t %+.6f\t NO APLICA NO APLICA NO APLICA\n', n, x1, x2, x3)
while true
if (err1<=tol) && (err2<=tol) && (err3<=tol)
break
else
n=n+1;
INJAC=JAC(x1,x2);
INB=B(x1,x2,x3);
SOL=INJAC\INB;
h1=SOL(1,1);
h2=SOL(2,1);
h3=SOL(3,1);
X1=x1+h1;
X2=x2+h2;
X3=x3+h3;
err1=abs(x1-X1);
err2=abs(x2-X2);
err3=abs(x3-X3);
%fprintf('\t %d\t %+.6f\t %+.6f\t %+.6f\t %.4f\t %.4f\t %.4f\n', n, X1, X2, X3, err1, err2,err3)
x1=X1;
x2=X2;
x3=X3;
disp("Jacobiano")
disp(INJAC)
disp("Solucion")
disp(X1)
disp(X2)
disp(X3)
end
end
case 3
clc
syms x1 x2 x3 x4
A1 = input('Ingrese ecuacion 1 ');
A2 = input('Ingrese ecuacion 2 ');
A3 = input('Ingrese ecuacion 3 ');
A4 = input('Ingrese ecuacion 4 ');
tol= input('ingrese toleracia ');
J=jacobian([A1,A2,A3,A4],[x1 x2 x3 x4]);
fprintf('\n\t Matriz Jacobiana \n\n')
pretty(J)
JAC = inline(J);
B = inline([-A1,-A2,-A3,-A4]');
x1=1;
x2=1;
x3=1;
x4=1;
INJAC=JAC(x1,x2,x3,x4);
INB=B(x1,x2,x3,x4);
SOLUCION=INJAC\INB;
h1=INJAC(:,1);
h2=INJAC(:,2);
h3=INJAC(:,3);
h4=INJAC(:,4);
igual=['=' '=' '=' '=']';
fprintf('\n\t Evaluaci?n de la matriz Jacobiana y de la funci?n negativa \n')
fprintf('\t Cuando las incognitas valen 1 \n')
fprintf('\t [J][h]=[-F] \n\n')
format long
T=table(h1,h2,h3,h4,igual,INB,SOLUCION);
disp(T)
format
n=0;
err1=100;
err2=100;
err3=100;
err4=100;
fprintf('\n\t I\t\t X1\t\t\t X2\t\t\t X3\t\t\t X4\t\t EX1\t\t EX2\t\t EX3\t\t EX4\n')
fprintf('\t %d\t %+.6f\t %+.6f\t %+.6f\t %+.6f\t NO APLICA NO APLICA NO APLICA NO APLICA\n', n, x1, x2, x3,x4)
while true
if (err1<=tol) && (err2<=tol) && (err3<=tol) && (err4<=tol)
break
else
n=n+1;
INJAC=JAC(x1,x2,x3,x4);
INB=B(x1,x2,x3,x4);
SOL=INJAC\INB;
h1=SOL(1,1);
h2=SOL(2,1);
h3=SOL(3,1);
h4=SOL(4,1);
X1=x1+h1;
X2=x2+h2;
X3=x3+h3;
X4=x4+h4;
err1=abs(x1-X1);
err2=abs(x2-X2);
err3=abs(x3-X3);
err4=abs(x4-X4);
fprintf('\t %d\t %+.6f\t %+.6f\t %+.6f\t %+.6f\t %.4f\t %.4f\t %.4f\t %.4f\n', n, X1, X2, X3, X4, err1, err2,err3, err4)
x1=X1;
x2=X2;
x3=X3;
x4=X4;
end
end
case 0
break
otherwise
fprintf('\n\t Opcion invalida, intente de nuevo\n')
end
end

También podría gustarte