Numerical Methods - Broyden's Method. Error: Not enough input arguments

3 次查看(过去 30 天)
Here is the code for broyden's method (found online):
function [xv,it]=broyden(x,f,n,tol)
% Broyden's method for solving a system of n non-linear equations
% in n variables.
%
% Example call: [xv,it]=broyden(x,f,n,tol)
% Requires an initial approximation column vector x. tol is required
% accuracy. User must define function f, for example see page 115.
% xv is the solution vector, parameter it is number of iterations
% taken. WARNING. Method may fail, for example, if initial estimates
% are poor.
%
fr=zeros(n,1); it=0; xv=x;
%Set initial Br
Br=eye(n);
fr=feval(f, xv);
while norm(fr)>tol
it=it+1;
pr=-Br*fr;
tau=1;
xv1=xv+tau*pr; xv=xv1;
oldfr=fr; fr=feval(f,xv);
%Update approximation to Jacobian using Broydens formula
y=fr-oldfr; oldBr=Br;
oyp=oldBr*y-pr; pB=pr'*oldBr;
for i=1:n
for j=1:n
M(i,j)=oyp(i)*pB(j);
end;
end;
Br=oldBr-M./(pr'*oldBr*y);
end;
How I am calling it:
x0 = [-0.5 1.4];
f = @(x,y) [(x+3)*(y^3-7)+18; sin(y*exp(x)-1)];
[xv, it] = broyden(x0, f, 15, 1e-9);
From this, I get the error:
Not enough input arguments.
Error in hw10>@(x,y)[(x+3)*(y^3-7)+18;sin(y*exp(x)-1)]
Error in hw10>broyden (line 55)
fr=feval(f, xv);
Error in hw10 (line 9)
[xv, it] = broyden(x0, f, 15, 1e-9);
I am not sure what the issue is here. The code stated that it worked for n x n non linear system. What am I missing here?
Thank you!

采纳的回答

Walter Roberson
Walter Roberson 2019-11-14
f = @(x,y) [(x+3)*(y^3-7)+18; sin(y*exp(x)-1)];
That is a function that needs two separate parameters. You are instead passing it a single vector. You need to add
f2 = @(x) f(x(1), x(2))
And pass that instead of f

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by