Clear Filters
Clear Filters

fsolve function not solving nonlinear equations, I had observed "Invalid datatype. Options argument must be created with OPTIMOPTIONS"; could you assist me, pse

7 views (last 30 days)
DESCRIPTIO OF TASK
function [FN, Q, y] = mag2(Q0, y0)
ee=0.04; vs=0.00000131; D=[0.220, 0.097, 0.110, 0.220, 0.247, 0.0247, 0.353]; L=[900,400,400,800,500,900,400];
FUN = @(Q,y)[Q(1)-Q(2)-0.03; Q(2)+Q(6)-Q(3)-0.04; Q(3)+Q(4)-0.035; Q(5)-Q(4)-0.01; Q(7)-Q(6)-Q(5)-0.015; ...
1/(y(1)).^0.5+4*log(ee./(3.7*D(1,1))+2.54*3.14*D(1,1)*vs./(4*Q(1)*(y(1)).^0.5)); 1/(y(2)).^0.5+4*log(ee./(3.7*D(1,2))+2.54*3.14*D(1,2)*vs./(4*Q(2)*(y(2)).^0.5));...
1/(y(3)).^0.5+4*log(ee./(3.7*D(1,3))+2.54*3.14*D(1,3)*vs./(4*Q(3)*(y(3)).^0.5)); 1/(y(4)).^0.5+4*log(ee./(3.7*D(1,4))+2.54*3.14*D(1,4)*vs./(4*Q(4)*(y(4)).^0.5));...
1/(y(5)).^0.5+4*log(ee./(3.7*D(1,5))+2.54*3.14*D(1,5)*vs./(4*Q(5)*(y(5)).^0.5)); 1/(y(6)).^0.5+4*log(ee./(3.7*D(1,6))+2.54*3.14*D(1,6)*vs./(4*Q(6)*(y(6)).^0.5));...
1/(y(7)).^0.5+4*log(ee./(3.7*D(1,7))+2.54*3.14*D(1,7)*vs./(4*Q(7)*(y(7)).^0.5));...
Q(1)*abs(y(1)*8*L(1,1)*Q(1)./(9.81*3.14.^2*D(1,1).^5))+Q(2)*abs(y(2)*8*L(1,2)*Q(2)./(9.81*3.14.^2*D(1,2).^5)) ...
-Q(6)*abs(y(6)*8*L(1,6)*Q(6)./(9.81*3.14.^2*D(1,6).^5))+Q(7)*abs(y(7)*8*L(1,7)*Q(7)./(9.81*3.14.^2*D(1,7).^5)); ...
Q(3)*abs(y(3)*8*L(1,3)*Q(3)./(9.81*3.14.^2*D(1,3).^5))-Q(4)*abs(y(4)*8*L(1,4)*Q(4)./(9.81*3.14.^2*D(1,4).^5))...
+Q(6)*abs(y(6)*8*L(1,6)*Q(6)./(9.81*3.14.^2*D(1,6).^5))-Q(5)*abs(y(5)*8*L(1,5)*Q(5)./(9.81*3.14.^2*D(1,5).^5))];
options = optimoptions('fsolve','Display','iter');
[Q,y]=fsolve(FUN,Q0,y0,options);
end
Q0=[-0.15, 0.15]; y0=[0.00001, 0.1]; mag2(Q0, y0)
THE OUTPUT, I GOT
Error using fsolve
Invalid datatype. Options argument must be created with OPTIMOPTIONS.
Error in mag2 (line 15)
[Q,y]=fsolve(FUN,Q0,y0,options);
  3 Comments
Torsten
Torsten on 15 Apr 2024
Edited: Torsten on 15 Apr 2024
You have 14 unknowns (Q(1)-Q(7), y(1)-y(7)), but only 12 equations. Is this intentionally ?
Star Strider
Star Strider on 15 Apr 2024
@Eng.
First, I do not know what ‘fsolve can’t solve’ means. What code are you using? Did you make the changes that I suggested (or something similar)?
Second, note @Torsten’s Comment. If there are fewer equations than parameters, the estimated parameters will not be unique.

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 15 Apr 2024
Edited: Torsten on 15 Apr 2024
One problem is that fsolve wants the initial parameter estimates as a single vector, and the third argument is the ‘options’ structure. I initially combined ‘Q0’ and‘y0’ into a single vector to successfully solve the ‘options’ problem. However, then the indexing of ‘Q’ became an issue. Note that ‘Q0’ and‘y0’ only have two elements, however your code addresses at least six elements in each.
You probably need to do sometning like this:
Q0 = rand(1,6)
y0 = rand(1,6);
Qy = [Q0; y0];
[Q,y]=fsolve(FUN,Qy,options);
so that ‘Q1’ becomes ‘Qy(1,1)’ , ‘Q(2)’ becomes ‘Qy(1,2)’ , ‘y(1)’ becomes ‘Qy(2,1)’ and so for the rest. I leave that to you. If there are no other problems in your code, that should work.
Q0=[-0.15, 0.15]; y0=[0.00001, 0.1]; mag2(Q0, y0)
Index exceeds the number of array elements. Index must not exceed 4.

Error in solution>@(Q,y)[Q(1)-Q(2)-0.03;Q(2)+Q(6)-Q(3)-0.04;Q(3)+Q(4)-0.035;Q(5)-Q(4)-0.01;Q(7)-Q(6)-Q(5)-0.015;1/(y(1)).^0.5+4*log(ee./(3.7*D(1,1))+2.54*3.14*D(1,1)*vs./(4*Q(1)*(y(1)).^0.5));1/(y(2)).^0.5+4*log(ee./(3.7*D(1,2))+2.54*3.14*D(1,2)*vs./(4*Q(2)*(y(2)).^0.5));1/(y(3)).^0.5+4*log(ee./(3.7*D(1,3))+2.54*3.14*D(1,3)*vs./(4*Q(3)*(y(3)).^0.5));1/(y(4)).^0.5+4*log(ee./(3.7*D(1,4))+2.54*3.14*D(1,4)*vs./(4*Q(4)*(y(4)).^0.5));1/(y(5)).^0.5+4*log(ee./(3.7*D(1,5))+2.54*3.14*D(1,5)*vs./(4*Q(5)*(y(5)).^0.5));1/(y(6)).^0.5+4*log(ee./(3.7*D(1,6))+2.54*3.14*D(1,6)*vs./(4*Q(6)*(y(6)).^0.5));1/(y(7)).^0.5+4*log(ee./(3.7*D(1,7))+2.54*3.14*D(1,7)*vs./(4*Q(7)*(y(7)).^0.5));Q(1)*abs(y(1)*8*L(1,1)*Q(1)./(9.81*3.14.^2*D(1,1).^5))+Q(2)*abs(y(2)*8*L(1,2)*Q(2)./(9.81*3.14.^2*D(1,2).^5)),-Q(6)*abs(y(6)*8*L(1,6)*Q(6)./(9.81*3.14.^2*D(1,6).^5))+Q(7)*abs(y(7)*8*L(1,7)*Q(7)./(9.81*3.14.^2*D(1,7).^5));Q(3)*abs(y(3)*8*L(1,3)*Q(3)./(9.81*3.14.^2*D(1,3).^5))-Q(4)*abs(y(4)*8*L(1,4)*Q(4)./(9.81*3.14.^2*D(1,4).^5)),+Q(6)*abs(y(6)*8*L(1,6)*Q(6)./(9.81*3.14.^2*D(1,6).^5))-Q(5)*abs(y(5)*8*L(1,5)*Q(5)./(9.81*3.14.^2*D(1,5).^5))] (line 5)
FUN = @(Q,y)[Q(1)-Q(2)-0.03; Q(2)+Q(6)-Q(3)-0.04; Q(3)+Q(4)-0.035; Q(5)-Q(4)-0.01; Q(7)-Q(6)-Q(5)-0.015; ...

Error in fsolve (line 270)
fuser = feval(funfcn{3},x,varargin{:});

Error in solution>mag2 (line 16)
[Q,y]=fsolve(FUN,[Q0,y0],options);

Caused by:
Failure in initial objective function evaluation. FSOLVE cannot continue.
function [FN, Q, y] = mag2(Q0, y0)
ee=0.04; vs=0.00000131; D=[0.220, 0.097, 0.110, 0.220, 0.247, 0.0247, 0.353]; L=[900,400,400,800,500,900,400];
FUN = @(Qy)[Q(1)-Q(2)-0.03;...
Q(2)+Q(6)-Q(3)-0.04;...
Q(3)+Q(4)-0.035;...
Q(5)-Q(4)-0.01; Q(7)-Q(6)-Q(5)-0.015; ...
1/(y(1)).^0.5+4*log(ee./(3.7*D(1,1))+2.54*3.14*D(1,1)*vs./...
(4*Q(1)*(y(1)).^0.5));...
1/(y(2)).^0.5+4*log(ee./(3.7*D(1,2))+2.54*3.14*D(1,2)*vs./...
(4*Q(2)*(y(2)).^0.5));...
1/(y(3)).^0.5+4*log(ee./(3.7*D(1,3))+2.54*3.14*D(1,3)*vs....
/(4*Q(3)*(y(3)).^0.5));...
1/(y(4)).^0.5+4*log(ee./(3.7*D(1,4))+2.54*3.14*D(1,4)*vs./ ...
(4*Q(4)*(y(4)).^0.5));...
1/(y(5)).^0.5+4*log(ee./(3.7*D(1,5))+2.54*3.14*D(1,5)*vs./...
(4*Q(5)*(y(5)).^0.5));...
1/(y(6)).^0.5+4*log(ee./(3.7*D(1,6))+2.54*3.14*D(1,6)*vs./ ...
(4*Q(6)*(y(6)).^0.5));...
1/(y(7)).^0.5+4*log(ee./(3.7*D(1,7))+2.54*3.14*D(1,7)*vs./...
(4*Q(7)*(y(7)).^0.5));...
Q(1)*abs(y(1)*8*L(1,1)*Q(1)./(9.81*3.14.^2*D(1,1).^5))+...
Q(2)*abs(y(2)*8*L(1,2)*Q(2)./(9.81*3.14.^2*D(1,2).^5))-...
Q(6)*abs(y(6)*8*L(1,6)*Q(6)./(9.81*3.14.^2*D(1,6).^5))+...
Q(7)*abs(y(7)*8*L(1,7)*Q(7)./(9.81*3.14.^2*D(1,7).^5)); ...
Q(3)*abs(y(3)*8*L(1,3)*Q(3)./(9.81*3.14.^2*D(1,3).^5))-...
Q(4)*abs(y(4)*8*L(1,4)*Q(4)./(9.81*3.14.^2*D(1,4).^5))+...
Q(6)*abs(y(6)*8*L(1,6)*Q(6)./(9.81*3.14.^2*D(1,6).^5))-...
Q(5)*abs(y(5)*8*L(1,5)*Q(5)./(9.81*3.14.^2*D(1,5).^5))];
options = optimoptions('fsolve','Display','iter');
% [Q,y]=fsolve(FUN,Q0,y0,options);
Q0 = rand(1,6)
y0 = rand(1,6);
Qy = [Q0; y0];
[Q,y]=fsolve(FUN,Qy,options);
end
.
  7 Comments
Alex Sha
Alex Sha on 17 Apr 2024
refer to the results below:
q1: 0.0469485401123028
q2: 0.0169485400700552
q6: 0.000149927973562496
q3: -0.0229015319578391
q4: 0.0579015318964189
q5: 0.0679015321342485
q7: 0.0830514600277715
y1: 0.00689798266403546
y2: 0.012995928970676
y3: 0.0116007205080187
y4: 0.00689539239643874
y5: 0.00639537063426373
y6: 0.0922685733214674
y7: 0.00515822449799586
Star Strider
Star Strider on 17 Apr 2024
@Eng. — As always, my pleasure!
I am happy that it works for you!
For our interest, would it be possible fo you to post the working code?

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!