Why am I getting this error and how can I fix it?

*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.

 채택된 답변

Youssef  Khmou
Youssef Khmou 2013년 9월 29일

1 개 추천

F is 2x1 vector so as the input x, the starting points are set in x0, and F(-5) has no sense because the index of vector starts at 1 .
function F = f1(x)
F(1) = x(1)^3+2*x(2)^2-10;
F(2) = x(1)*x(2)-2;
%-----------------------
x0=[5 -1];
options=optimset('Display','iter');
x = fsolve('f1',x0,options)

추가 답변 (1개)

Walter Roberson
Walter Roberson 2013년 9월 29일
편집: Walter Roberson 2013년 9월 29일

1 개 추천

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

Sabbir
Sabbir 2013년 9월 29일
Thank you for the quick response. I was trying to solve the 2 non-linear equations with starting points x1=5 and x2=-1. Also to find the number of iterations used to find my solution.
I am not sure where to go with this as I am stuck.
Please write out the equations you are trying to solve and indicate which parts are known and which parts are unknown.
Sabbir
Sabbir 2013년 9월 29일
편집: Sabbir 2013년 9월 29일
Solve the following non linear equations f(x1,x2)= (x1)^3+2*(x2)^2-10=0 and f(x1,X2)=(x1*x2)-2=0; Use starting points x1=5 and x2=-1. Also find the number of iterations MATLAB used to find the solution.
Sabbir
Sabbir 2013년 9월 29일
Thank you Walter as well.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Mathematics and Optimization에 대해 자세히 알아보기

제품

질문:

2013년 9월 29일

댓글:

2013년 9월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by