Why am I getting this error and how can I fix it?
Mostra commenti meno recenti
*the following is saved as a file/script f1.m
function F = f1(x)
F(5) = x(5)^3+2*x(-1)^2-10;
F(-1) = x(5)*x(-1)-2;
*when I type the following in the command window,
x0 = [1 1];
options=optimset('Display','iter');
x = fsolve('f1',x0,options)
*But I get the following error
??? Attempted to access x(5); index out of bounds because
numel(x)=2.
Error in ==> f1 at 9
F(5) = x(5)^3+4*x(-1)^2-10;
Error in ==> fsolve at 254
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function
evaluation. FSOLVE cannot continue.
-- * Any help at all is much appericated, thank you.
Risposta accettata
Più risposte (1)
Walter Roberson
il 29 Set 2013
Modificato: Walter Roberson
il 29 Set 2013
1 voto
You set x0 to [1 1], so a vector of length 2 will be passed in to f1. But in f1, you expect the vector to be length 5.
You are also going to have problems because your code includes x(-1) which is a request to access location negative 1 of the array "x". Arrays must be indexed by non-negative integers.
Notice too that you assign your outputs to F(5) and F(-1) . fzero() expects a single scalar as output.
Are you possibly trying to code a recursion equation ?
4 Commenti
Sabbir
il 29 Set 2013
Walter Roberson
il 29 Set 2013
Please write out the equations you are trying to solve and indicate which parts are known and which parts are unknown.
Sabbir
il 29 Set 2013
Categorie
Scopri di più su Mathematics and Optimization in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!