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

6 vues (au cours des 30 derniers jours)
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!

Réponse acceptée

Walter Roberson
Walter Roberson le 14 Nov 2019
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

Plus de réponses (0)

Catégories

En savoir plus sur Systems of Nonlinear Equations dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by