error in fsolve solving nonlinear equations

Hi I'm trying to solve nonlinear equations with the following code:
F = myfun(x)
F = [ x(1)-((500+0.85*(((0.711+0.56*15/100+0.73*(15/100)^2)*5.67*10^(-8)*(25+273.15)^4*(1-cos(30*pi/180))/2)-5.67*10^(-8)*(25+273.15)^4))*1.6*0.8*(1-0*2)*1-1.6*((32+6.4*2)*(x(5)-25)));
x(2)-(2068.5072425+(-20.1259571429)*x(4)+0.0701428571*x(4)^2+(56.5615102375+(-0.0892494524)*x(4)+(-0.0013878571)*x(4)^2)*x(5)+(0.669960831250007+(-0.001477845238096)*x(4)+0.000000357142857*x(4)^2)*x(5)^2);
x(3)-(373.160515+(-0.7728)*x(4)+0.0378095238*x(4)^2+((-4.03604873)+0.2014116*x(4)+0.0005034286*x(4)^2)*x(5)+((-0.22703617375)+0.003774373809524*x(4)+(-0.000001214285714)*x(4)^2)*x(5)^2);
x(2)-(70*(x(4)-40));
x(2)-(x(3)+x(1)-12*(x(4)-20))];
Then I run the follwing:
x0 = [-5; -5]; % Make a starting guess at the solution
options = optimoptions('fsolve','Display','iter'); % Option to display output
[x,fval] = fsolve(@myfun,x0,options) % Call solver
I got the following errors in workspace:
Attempted to access x(5); index out of bounds because numel(x)=2.
Error in myfun (line 2)
F = [
x(1)-((500+0.85*(((0.711+0.56*15/100+0.73*(15/100)^2)*5.67*10^(-8)*(25+273.15)^4*(1-cos(30*pi/180))/2)-5.67*10^(-8)*(25+273.15)^4))*1.6*0.8*(1-0*2)*1-1.6*((32+6.4*2)*(x(5)-25)));
Error in fsolve (line 217)
fuser = feval(funfcn{3},x,varargin{:});
Error in testes_auxiliares_eqnonlinear_v2 (line 3)
[x,fval] = fsolve(@myfun,x0,options) % Call solver
Can someone help me.

 Accepted Answer

Well, you gave x0 = [-5;5], but your equation has components x(1) through x(5). You either need to have equations involving only x(1) and x(2), or you need to give a five-element initial vector x0.
Alan Weiss
MATLAB mathematical toolbox documentation

3 Comments

Thanks Alan Weiss it works
x =
1.0e+03 *
1.4298
1.4800
0.5439
0.0611
0.0076
fval =
1.0e-11 *
0
-0.0227
-0.0568
0.0682
-0.3638
It's mandatory to use a myfun.m function in other file?
No, the objective function can be the handle of an anonymous function or the handle of a function in the same .m file.

Sign in to comment.

More Answers (0)

Categories

Products

Community Treasure Hunt

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

Start Hunting!