How to solve this system of nonlinear equations composed of symbolic variables

Hi
I want to solve 3 nonlinear equations with 2 unknowns. The equations are composed of symbolic variables. When I run the code it gives the error " too many input arguments". I have spent lots of hours but could not resolve it. Any help or other solutions are really appreciated.
BTW, this code is one of several functions of the major code(i.e. A(i), beta(i), l(i) and j(i) are calculated in a separate function.
eqn1 = sumf == 0;
eqn2 = sumff == 0;
eqn3 = summ == 0;
F=@(x,y)[eqn1;eqn2;eqn3];
fun=@(u) F(u(1),u(2),u(3));
x0=[1 .5 ;1 .5; 1 .5];
[u1,fval1]=fsolve(fun,x0),

 Réponse acceptée

F=@(x,y)[eqn3;eqn4;eqn5];
Okay, F takes two arguments and ignores them and returns an array of items based on symbolic x and y and possibly other things (we cannot see the code for eqn4 or eqn5.)
fun=@(u) F(u(1),u(2),u(3));
fun takes a vector of 3 or more items and passes the first 3 one by one to the function F that is only expecting two parameters.
Let me emphasize that the parameters you pass to F are going to be ignored. When you construct the array for fun then the values that are going to be captured in the anonymous function will be the x and y that are symbolic variables. if you want the x and y padded in to be used in the expression then subs() them into the vector of equations.
But you are passing the function to fsolve. fsolve does not expect symbolic results. It also cannot proceed properly with == expressions
You should either use vpasolve or else you should leave out the ==0 should use matlabFunction() to construct numeric functions to fsolve

7 commentaires

So, I used vpa solve but I got the error " Error using mupadengine/feval_internal (line 172) More equations than variables is only supported for polynomial systems." as There are 3 equations and 2 unknowns.
sumf=0;
sumff=0;
summ=0;
syms x y
for i=1:n
sumf=sumf+((((C*l(i)/x)+(rho*9.81*A(i)*(cos(beta(i)))*(tan(phi*pi/180))/x)-(rho*9.81*A(i)*(sin(beta(i)))))*(cos(y*j(i))))/(((cos(beta(i)-(j(i)*y)))+(((sin(beta(i)-(j(i)*y)))*(tan(phi*pi/180))/x)))));
sumff=sumff+((((C*l(i)/x)+(rho*9.81*A(i)*(cos(beta(i)))*(tan(phi*pi/180))/x)-(rho*9.81*A(i)*(sin(beta(i)))))*(sin(y*j(i))))/(((cos(beta(i)-(j(i)*y)))+(((sin(beta(i)-(j(i)*y)))*(tan(phi*pi/180))/x)))));
summ=summ+((((C*l(i)/x)+(rho*9.81*A(i)*(cos(beta(i)))*(tan(phi*pi/180))/x)-(rho*9.81*A(i)*(sin(beta(i)))))*(cos(beta(i)-(j(i)*y))))/((cos(beta(i)-(j(i)*y)))+((sin(beta(i)-(j(i)*y)))*(tan(phi*pi/180))/x)));
end
eqn1 = sumf == 0;
eqn2 = sumff == 0;
eqn3 = summ == 0;
equations=[eqn1 eqn2 eqn3];
vars=[x y];
range=[0.1 5; 0 1.57];
[z1,z2] = vpasolve(equations,vars,range);
on the other hand, If I use matlabFunction, it gives this error:"The number of outputs should match the number of inputs."
sumf=0;
sumff=0;
summ=0;
syms x y
for i=1:n
sumf=sumf+((((C*l(i)/x)+(rho*9.81*A(i)*(cos(beta(i)))*(tan(phi*pi/180))/x)-(rho*9.81*A(i)*(sin(beta(i)))))*(cos(y*j(i))))/(((cos(beta(i)-(j(i)*y)))+(((sin(beta(i)-(j(i)*y)))*(tan(phi*pi/180))/x)))));
sumff=sumff+((((C*l(i)/x)+(rho*9.81*A(i)*(cos(beta(i)))*(tan(phi*pi/180))/x)-(rho*9.81*A(i)*(sin(beta(i)))))*(sin(y*j(i))))/(((cos(beta(i)-(j(i)*y)))+(((sin(beta(i)-(j(i)*y)))*(tan(phi*pi/180))/x)))));
summ=summ+((((C*l(i)/x)+(rho*9.81*A(i)*(cos(beta(i)))*(tan(phi*pi/180))/x)-(rho*9.81*A(i)*(sin(beta(i)))))*(cos(beta(i)-(j(i)*y))))/((cos(beta(i)-(j(i)*y)))+((sin(beta(i)-(j(i)*y)))*(tan(phi*pi/180))/x)));
end
x=[x;y]
fh = matlabFunction(sumf,sumff,summ,'vars',{x});
x0=[1 .5 ;1 .5; 1 .5];
[u,fval]=fsolve(fh,x0),
What should I do?
It is worthy to note that the number of equations is 3 while there are 2 unknowns
fh = matlabFunction([sumf,sumff,summ], 'vars', {x});
x0 = [1 .5];
[u, fval] = fsolve(fh, x0);
I suggest you consider replacing your x in the calculation with 1/X and then at the end take [1./u(1), u(2)] . This can help avoid singularities.
I have gotten this error"Index in position 1 exceeds array bounds (must not exceed 1)."
sumf=0;
sumff=0;
summ=0;
syms x y
for i=1:n
sumf=sumf+((((C*l(i)/x)+(rho*9.81*A(i)*(cos(beta(i)))*(tan(phi*pi/180))/x)-(rho*9.81*A(i)*(sin(beta(i)))))*(cos(y*j(i))))/(((cos(beta(i)-(y*j(i))))+(((sin(beta(i)-(y*j(i))))*(tan(phi*pi/180))/x)))));
sumff=sumff+((((C*l(i)/x)+(rho*9.81*A(i)*(cos(beta(i)))*(tan(phi*pi/180))/x)-(rho*9.81*A(i)*(sin(beta(i)))))*(sin(y*j(i))))/(((cos(beta(i)-(y*j(i))))+(((sin(beta(i)-(y*j(i))))*(tan(phi*pi/180))/x)))));
summ=summ+((((C*l(i)/x)+(rho*9.81*A(i)*(cos(beta(i)))*(tan(phi*pi/180))/x)-(rho*9.81*A(i)*(sin(beta(i)))))*(cos(beta(i)-(y*j(i)))))/((cos(beta(i)-(y*j(i))))+((sin(beta(i)-(y*j(i))))*(tan(phi*pi/180))/x)));
end
x=[x;y];
fh = matlabFunction([sumf,sumff,summ], 'vars', {x});
x0=[1 0.5];
[u, fval] = fsolve(fh, x0);
How about this? is the arrangement fine? why did you consider x0 as [1;0.5]?;.... Am I wrong?
fh = matlabFunction(sumf,sumff,summ,'File','myfile','Vars',{x},'Outputs',{'name1','name2','name3'})% you should use matlabFunction's 'vars' option to convert the variables into a vector.
x0=[1 1 1 ;.5 .5 .5];
options = optimoptions('lsqnonlin','Display','iter');
[x,resnorm,residual,exitflag,output]=lsqnonlin(fh,x0,[],[],options)
I could get some results but one of the outputs is different from sth that I am comparing. I think it is probably due to the initial x0. Is there anyway that I could do a better initial guess?
you have two variables, x and y. You should not be passing in 6 initial conditions.
Note that fsolve does not permit passing in ranges of values for the variables. fzero permits a range of values for its single variable, and vpasolve permits ranges of values for each variable, but fsolve only permits initial conditions.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Mathematics dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by