I am trying to use the fsolve function to solve a non linear equation .In the function i created all the constants are matrices.and i expect a vector of the unknown.I keep getting error of not enough input argument.Iwould much appreciate your help.

function F = TRIAL3(x, V1,W1 ,W, w, z, Z1,Z, p, p1, q1, q, c1, V, v, j)
a=x(1); r = x(2); s = x(3); b=x(4); g = x(5); d = x(6); h = x(7);
F = [(V1*((c1- W*r - Z*s)+(p*b- W*r*b^2 -Z*s*b.^2 - j*d*b) +(q*g- W*r*g^2- Z*s*g.^2 - j*h*g)))-1*a- a*b^2 - a*g.^2;
(W1*((c1- V*a - Z*s)+(p*b- V*a*b^2 -Z*s*b.^2 - j*d*b) +(q*g- V*a*g^2 - Z*s*g.^2 - j*h*g)))-1*r- r*b^2 - r*g.^2;
(Z1*((c1- V*a- W*r)+ (p*b- V*r*b^2 -W*r*b.^2 - j*d*b) +(q*g- V*a*g^2 - W*r*g.^2 - j*h*g)))-1*s- s*b^2 - s*g.^2;
(inv(a'*v*V*a + a'*v*W*r +a'*v*Z*s + r'*w*V*a + r'*w*W*r+ r'*w*Z*s + s'*z*V*a+ s'*z*W*r +s'*z*Z*s)*(p1*V*a+ p1*W*r + p1*Z*s -a'*v*j*d - r'*W'*j*d +s'*Z'*j*d))-b;
(inv(a'*v*V*a + a'*v*W*r +a'*v*Z*s + r'*w*V*a + r'*w*W*r+ r'*w*Z*s + s'*z*V*a+ s'*z*W*r +s'*z*Z*s)*(q1*V*a+ q1*W*r + q1*Z*s -a'*v*j*h - r'*W'*j*h +s'*Z'*j*h))-g;
(0.2*p1*j - 0.2*a'*v*j*b - 0.2*r'*w*j*b - 0.2*s'*z*j*b)- d;
(0.2*q1*j - 0.2*a'*v*j*b - 0.2*r'*w*j*g - 0.2*s'*z*j*g)- h];
%below is my call function;
fun=@TRIAL3; Xo=[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]; X=fsolve(fun,Xo)
%my error message Error using TRIAL3 (line 5) Not enough input arguments.
Error in fsolve (line 217) fuser = feval(funfcn{3},x,varargin{:});
Caused by: Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.

 Risposta accettata

fun = @(x)TRIAL3(x, V1,W1 ,W, w, z, Z1,Z, p, p1, q1, q, c1, V, v, j);
Xo = [1 1 1 1 1 1 1];
X = fsolve(fun,Xo)

9 Commenti

i am still getting an error .which i presume is from the x uknowns.These a=x(1); r = x(2); s = x(3); b=x(4); g = x(5); d = x(6); h = x(7); are all supposed to be matrices.How do i define the intialial matrices for solve.I would be glad for your assitance.
%the new error Error using - Matrix dimensions must agree.
Error in TRIAL3 (line 5) F=[(V1*((c1- W*r - Z*s)+(p*b- W*r*b^2 -Z*s*b.^2 - j*d*b) +(q*g- W*r*g^2- Z*s*g.^2 - j*h*g)))-1*a- a*b^2 - a*g.^2;
Error in @(x)TRIAL3(x,V1,W1,W,w,z,Z1,Z,p,p1,q1,q,c1,V,v,j)
Error in fsolve (line 217) fuser = feval(funfcn{3},x,varargin{:});
Caused by: Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
You must save and concatenate the unknown matrices in one long vector of unknowns.
E.g. if a and r are 3x3 matrices, you can save them in an 18-element x-vector as
x=(a(1,1),a(1,2),a(1,3),a(2,1),a(2,2),a(2,3),a(3,1),a(3,2),a(3,3),r(1,1),r(1,2),r(1,3),r(2,1),r(2,2),r(2,3),r(3,1),r(3,2),r(3,3))
Best wishes
Torsten.
Thank you for your quick response. I am glad. I would like to know whether the unknown vectors need to be defined inside the function or outside the function and how do i also initialize the initial solutions.
The vector of initial guesses x0 must be created outside the function before calling "fsolve".
Now "fsolve" calls your function with a vector x of the same length as the vector of initial guesses. From this vector, you have to reconstruct your matrices
(e.g. a and r as
a = [x(1) x(2) x(3);x(4) x(5) x(6); x(7) x(8) x(9)];
r = [x(10) x(11) x(12); x(13) x(14) x(15) ; x(16) x(17) x(18)];
)
and calculate F.
Best wishes
Torsten.
I am finding it difficult to understand.from my code above I have assigned 7 unknown variables to x which all happen to be a matrix. The first problem is where to put the new assigned unknown vector.I tried fixing it both inside the function argument and outside but i keep getting errors. I would be glad if you could also explain the initialization bit further for me My a,r ,s are each 5x1 matrices with b,d ,h,g being a scalar . x=[a(1,1),a(2,1),a(3,1),a(4,1),a(5,1) ,r(1,1),r(2,1),r(3,1),r(4,1),r(5,1), s(1,1),s(2,1),s(3,1),s(4,1),s(5,1),b(1,1),g(1,1),d(1,1),h(1,1)] Undefined function 'a' for input arguments of type 'double'.
function F= TRIAL3(x,V1,W1,W,w,z,Z1,Z,p,p1,q1,q,c1,V,v,j)
a=[x(1);x(2);x(3);x(4);x(5)];
r=[x(6);x(7);x(8);x(9);x(10)];
s=[x(11);x(12);x(13);x(14);x(15)];
b=x(16);
g=x(17);
d=x(18);
h=x(19);
F = ...
and F must be a 1x19 vector.
Best wishes
Torsten.
fun = @(x)TRIAL3(x, V1,W1 ,W, w, z, Z1,Z, p, p1, q1, q, c1, V, v, j); Xo = [1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]; X = fsolve(fun,Xo) I am getting error message evertything seems ok too
Error: File: TRIAL3.m Line: 4 Column: 10 Expression or statement is incorrect--possibly unbalanced (, {, or [.
Error in @(x)TRIAL3(x,V1,W1,W,w,z,Z1,Z,p,p1,q1,q,c1,V,v,j)
Error in fsolve (line 217) fuser = feval(funfcn{3},x,varargin{:});
Caused by: Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
I think it worked. Thank you so much. May I have your email address if you don't mind. You saved me after a week of trying to read soo many Matlab previous questions. I am glad for your assistance.
Hello Tornsten,I would like to implement this same nonlinear equation with genetic algorithm.I keep getting not enough input argument,I would be glad if you could assist.Thanks for your time.

Accedi per commentare.

Più risposte (0)

Categorie

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by