Too many inputs to inline function
2 views (last 30 days)
Show older comments
I am trying to create a program that takes two nonlinear equations and finds their roots x and y by using the newton-raphson method that gives a convergence criteria of less than .01%. The program works fine when I use certain example equations, but when I use what I am supposed to for homework it comes back with the error in the title. Here is my code:
nao = .333;
nco = .333;
ndo = .333;
neo = 0;
nbo = 0;
nto = 1;
syms x;
syms y;
%f1 = .1071*((nao - 2*x)^2)*(nto + x) - ((nbo + 2*x)^2)*(nco + x -y)
%f2 = .01493*(nco+x-y)*(ndo-y)-(neo+2*y)^2
f1 = 2*x+y-(x+y)^(1/2)-3;
f2 = 4-y-5/(x+y);
g1 = inline(f1);
g2 = inline(f2);
a11x = diff(g1(x,y)); %differentiate with respect to x
a11 = inline(a11x);
%display ('F1 differentiated with respect to x, then y: ')
a12y = diff(g1(y,x)); %differentiate with repect to y
a12 = inline(a12y);
%disp (a11x)
%disp (a12y)
a21x = diff(g2(x,y)); %differentiate with respect to x
a21 = inline(a21x);
%display ('F2 differentiated with respect to x, then y: ')
a22y = diff(g2(y,x)); %differentiate with repect to y
a22 = inline(a22y);
%disp (a21x)
%disp (a22y)
%Formulas are
%g1, g2 For origninal formulas
%a11, a12 For derivatives of f1 with respect to x, then y.
%a21, a22 For derivatives of f2 with respect to x, then y.
%Initial guesses for x and y values
xg(1) = 2;
yg(1) = 2;
i=1;
xchange = 100;
ychange = 100;
d(1,1) = 1;
while (xchange && ychange > .1)
disp('Iteration number: ')
disp(i)
g1t = -g1(xg(i), yg(i));
g2t = -g2(xg(i), yg(i));
a11t = a11(xg(i), yg(i));
a12t = a12(xg(i), yg(i));
a21t = a21(xg(i), yg(i));
a22t = a22(xg(i), yg(i));
b = [g1t; g2t];
a = [a11t, a12t; a21t, a22t];
x=a\b;
d(i,1) = x(1);
d(i,2) = x(2);
disp ('di,1: ')
disp (d(i,1))
disp('di,2: ')
disp (d(i,2))
xg(i+1) = xg(i) + d(i,1);
yg(i+1) = yg(i) + d(i,2);
disp('New values are: ')
disp ('xg(i+1): ')
disp (xg(i+1))
disp('yg(i+1): ')
disp (yg(i+1))
disp ('Percent changes for x, then y: ')
iter(i, 1) = abs(100*(xg(i+1) - xg(i))/(xg(i+1)));
iter(i, 2) = abs(100*(yg(i+1) - yg(i))/(yg(i+1)));
disp (iter(i,1))
disp (iter(i,2))
xchange = iter(i,1);
ychange = iter(i,2);
i = i+1;
end
disp('Final value of x and y: ')
disp (xg(i))
disp (yg(i))
disp('Percent change between final iteration of x and y: ')
disp(xchange)
disp(ychange)
disp('Number of iterations: ')
disp(i-1)
The functions on lines 9 and 10 are the ones I am trying to make work, while the functions on lines 11 and 12 are the ones that already work. This code should run fine and find the right roots, but when you remove the %'s from the first f1 and f2 and delete the second f1 and f2, it gives the error in the title. When changing f1 and f2, be sure to put .1 for both xg(1) and yg(1) on lines 37 and 38 for the initial guess, we were given this in our problem statement. I don't know what is wrong, and I'm sure this is very messy code so I apologize, but it should be fairly straightforward. Thanks!
0 Comments
Answers (1)
Walter Roberson
on 22 Feb 2012
Specify the variables (and the order of the variables) explicitly when you use inline()
2 Comments
Walter Roberson
on 22 Feb 2012
See the bottom example in the reference page:
http://www.mathworks.com/help/techdoc/ref/inline.html
See Also
Categories
Find more on Function Creation 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!