"Error using indexing Not enough inputs to inline function; Error in NRM (line 93) INJAC=JAC(x1,x2)" showing in a multivariable Newton Raphson program.
Show older comments
I'm using a code that I learned from school to try to solve a three variable non-linear system, but after inputing my three functions the program shows me the Jacobian and then the error messages show. The exact same code did worked to my professor, so I don't know what may gone wrong. Here are the equations:
3*x1-cos(x2*x3)-0.5
x1^2 - 81*(x2+.1)^2 + sin(x3) + 1.06
exp(-x1*x2) + 20*(x3) + (10*3.141592 -3)/3
and is the code:
clear, clc
while true
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
clc
syms x1 x2
A1 = input('Ingrese ecuacion 1 ');
A2 = input('Ingrese ecuacion 2 ');
tol= input('ingrese toleracia ');
J=jacobian([A1,A2,],[x1 x2]);
fprintf('\n\t Matriz Jacobiana \n\n')
pretty(J)
JAC = inline(J);
B = inline([-A1,-A2]');
x1=1;
x2=1;
INJAC=JAC(x1,x2);
INB=B(x1,x2);
SOLUCION=INJAC\INB;
h1=INJAC(:,1);
h2=INJAC(:,2);
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,igual,INB,SOLUCION);
disp(T)
format
n=0;
err1=100;
err2=100;
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
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)
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
Answers (1)
Torsten
on 22 Nov 2022
For the 3-variable case, the line
INJAC=JAC(x1,x2);
has to be changed to
INJAC=JAC(x1,x2,x3);
Categories
Find more on Dates and Time in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!