You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Solving system of nonlinear equations
1 view (last 30 days)
Show older comments
Hello all. I want to solve 10 nonlinear equations in 10 variables. I used 'solve' function but it didn't give me a result " it takes too much time and nothing happened". Is there any other way to do that? The equations are generated within the program.
24 Comments
Star Strider
on 2 Aug 2012
Posting the relevant parts of your code will help.
Since you are using ‘solve’ you are obviously using the Symbolic Math Toolbox. How are the equations generated within the program? What are the equations? What do you eventually want to do with them?
omnia
on 2 Aug 2012
for y=1:10;
taw_w_E=((1-f_ES(y))/taw_w_E_0)^-1;
taw_G_E=((1-f_ES(y))/taw_G_E_0)^-1;
taw_E_G=((1-f_GS(y))/taw_E_G_0)^-1;
gES=const*Nm(y)*ES_deg*(2*f_ES(y)-1)*lorntezES(y)/E_ES(y);
gGS=const*Nm(y)*GS_deg*(2*f_GS(y)-1)*lorntezGS(y)/E_GS(y);
Rstim_ES1=gammay*delta_z/(2*ES_deg*Nm(y)) * (gES*(Ss_fw+Ss_bw)+sumterm_ES); %gGS same as gain of sp
Rstim_GS1=gammay*delta_z/(2*GS_deg*Nm(y)) * (gGS*(Ss_fw+Ss_bw)+sumterm_GS);
eqn_17=[eqn_17 (rho_k/(NQD_k*2*ES_deg))*(f_wl1/taw_w_E)-f_ES(y)/taw_E_w1(y)+(GS_deg/ES_deg)*(f_GS(y)/taw_G_E)-f_ES(y)/taw_E_G-f_ES(y)/taw_r-Rstim_ES1];
eqn_18=[eqn_18 (ES_deg/GS_deg)*(f_ES(y)/taw_E_G)-f_GS(y)/taw_G_E-f_GS(y)/taw_r-Rstim_GS1];
end
soln=solve(eqn_17(1),eqn_17(2),eqn_17(3),eqn_17(4),eqn_17(5),eqn_18(1),eqn_18(2),eqn_18(3),eqn_18(4),eqn_18(5));
f_ES is a vector of symbols containing 10 symbols, the same for f_GS. All other variables are constants
Star Strider
on 2 Aug 2012
I am still lost. If you do:
eqn_17(1) = simplify(collect(expand(eqn_17(1))))
and such for every equation in:
soln=solve(eqn_17(1),eqn_17(2),eqn_17(3),eqn_17(4),eqn_17(5),eqn_18(1),eqn_18(2),eqn_18(3),eqn_18(4),eqn_18(5));
what do you get?
It will not be necessary to post them all. Posting those results for ‘eqn_17(1)’ and ‘eqn_18(1)’ will likely give us enough information for now.
Walter Roberson
on 5 Aug 2012
Algebraically solving 10 non-linear equations in 10 unknowns usually takes a number of days, if you do not run out of memory first. The practical "overnight" limit is typically 5 to 7 simultaneous equations.
Star Strider
on 5 Aug 2012
Omnia —
Can you at least post the results of:
eqn_17(1) = simplify(collect(expand(eqn_17(1))))
and
eqn_18(1) = simplify(collect(expand(eqn_18(1))))
for us to see and comment on? If even running those two statements take too much time and do not produce any results, we probably cannot help you.
Walter Roberson
on 5 Aug 2012
How many distinct symbolic variables are there? If f_ES and F_GS each have 10 variables and the variables are not duplicated between the two, then you have 10 equations in 20 unknowns, and you will not be able to get any solution for that unless you specify which 10 of the variables to solve for. The default would be to try to solve for all the variables in the equations, and that would fail because there are not enough equations for 20 variables. But it would go through all the trouble of trying to solve them in case it found that some of variables canceled out.
omnia
on 6 Aug 2012
Edited: omnia
on 6 Aug 2012
to Star:it does not take much time but the expressions are too long. I posted it. The letters you will see are symbols which I solve for the results are
eqn_17(1) = simplify(collect(expand(eqn_17(1))))
116243572939000356925466995496181050988756417015500885967487300260565376098142383/2177451841535983880665681550360714342535490171501907325019394840461312-24106759612069861098695551225927499594865947904174090038632442275946499123708671346269682748552178550535529851103/43107481922375677281777785455889721866265008398046483650146424074809414892368321343246831913348890624*f_ES1+154742504910672534362390528/4629590355721305*f_GS1+10696062763240755492666588545566651870871552/22924621067347399351776919804905*f_GS1*f_ES1, 93129274647902536614837597442612252815992836100743339980078558169111014628930927/1744481186063737167486721053675299996653326609223580508099867270184960-195847093856089006364361094420662118260978494755606202856229994533339797215516721848650859618163/3290821285825280737063640556430009151658106510784804981101685951280027293745435115520*f_ES2+1/2*f_GS2*(309485009821345068724781056/4629590355721305-309485009821345068724781056/4629590355721305*f_ES2)-f_ES2*(2475880078570760549798248448/4951760157141521-2475880078570760549798248448/4951760157141521*f_GS2), 103626330145294271399547488616985491803143759292759825718002655074554624519894497/1941112109887430256045440963402253910309178979161816840204687408889856-103352611187346639948378249632190388870641417975316839551018028528483331966147584241340798478019/1727754924111110048732731841372233941756597751011194640559834018976810935588654940160*f_ES3+1/2*f_GS3*(309485009821345068724781056/4629590355721305-309485009821345068724781056/4629590355721305*f_ES3)-f_ES3*(2475880078570760549798248448/4951760157141521-2475880078570760549798248448/4951760157141521*f_GS3), 58373828299758524181672916154167152209897980238592151808655183299124894834982973/1093450382333523308371920425280473151189497220792306127667856291135488-220953296391003716827357847410059942870457627330863931535429522687776030286816520365244366808413/3673788080589517484187456074632233658881567029912045270631252603543983570878560468992*f_ES4+1/2*f_GS4*(309485009821345068724781056/4629590355721305-309485009821345068724781056/4629590355721305*f_ES4)-f_ES4*(2475880078570760549798248448/4951760157141521-2475880078570760549798248448/4951760157141521*f_GS4), 467662793138478911821560987337953367919454653776277233636551467144233509114019979/8760201622842936781746242469177427822640650126448722261764607318163456-420003102751321346695470874575112574093115874719657380524279107518459792945690928182057590829947/6943727017956516935151655477936956138318706341290652161933110028950528087933717577728*f_ES5+1/2*f_GS5*(309485009821345068724781056/4629590355721305-309485009821345068724781056/4629590355721305*f_ES5)-f_ES5*(2475880078570760549798248448/4951760157141521-2475880078570760549798248448/4951760157141521*f_GS5)]
eqn_18(1) = simplify(collect(expand(eqn_18(1)))) 4951760157141521099596496896/4951760157141521*f_ES1-21392125526481510985333177091133303741743104/22924621067347399351776919804905*f_GS1*f_ES1-49834416484629072470448893625577185780599771768551555604709144025064283487553/732393097481956459657749157751051292376114585562437784347229552640*f_GS1+30680057561882358670358695147569069162356107380204523997993/316396502155684688073895940102234179999449896452096, 2*f_ES2*(2475880078570760549798248448/4951760157141521-2475880078570760549798248448/4951760157141521*f_GS2)-f_GS2*(309485009821345068724781056/4629590355721305-309485009821345068724781056/4629590355721305*f_ES2)-21284357112504429712330240150093692483987070136032523715989753/17111913822378649269067506399307264442678019262251008*f_GS2+4172443290125780443262733750786428041309050873781515715989753/34223827644757298538135012798614528885356038524502016, 2*f_ES3*(2475880078570760549798248448/4951760157141521-2475880078570760549798248448/4951760157141521*f_GS3)-f_GS3*(309485009821345068724781056/4629590355721305-309485009821345068724781056/4629590355721305*f_ES3)-22125439385870456757216457977084860588799301724437220220677455/17138416528350325382144632033093883165385744116088832*f_GS3+4987022857520131375071825943990977423413557608348388220677455/34276833056700650764289264066187766330771488232177664, 2*f_ES4*(2475880078570760549798248448/4951760157141521-2475880078570760549798248448/4951760157141521*f_GS4)-f_GS4*(309485009821345068724781056/4629590355721305-309485009821345068724781056/4629590355721305*f_ES4)-3764854543803885230713906370532807563582630288577620767608609/2860819872387000249203626277813416981348911494987776*f_GS4+904034671416884981510280092719390582233718793589844767608609/5721639744774000498407252555626833962697822989975552, 2*f_ES5*(2475880078570760549798248448/4951760157141521-2475880078570760549798248448/4951760157141521*f_GS5)-f_GS5*(309485009821345068724781056/4629590355721305-309485009821345068724781056/4629590355721305*f_ES5)-44844049642880379924936339571152042071536339847931206168893117/34382843880587355216597766601334241221602387647528960*f_GS5+10461205762293024708338572969817800849933952200402246168893117/68765687761174710433195533202668482443204775295057920]
omnia
on 6 Aug 2012
to Walter:I have two vectors each of 5 symbols
f_ES =[ f_ES1, f_ES2, f_ES3, f_ES4, f_ES5]
f_GS =[ f_GS1, f_GS2, f_GS3, f_GS4, f_GS5]
then I write 10 equations using these symbols. 'eqn_17' contains 5 equations and 'eqn_18' contains the other 5. The for loop is done 5 times not 10 as I posted before (I am sorry error in typing but it is right in the program)
for y=1:5;
taw_w_E=((1-f_ES(y))/taw_w_E_0)^-1;
taw_G_E=((1-f_ES(y))/taw_G_E_0)^-1;
taw_E_G=((1-f_GS(y))/taw_E_G_0)^-1;
gES=const*Nm(y)*ES_deg*(2*f_ES(y)-1)*lorntezES(y)/E_ES(y);
gGS=const*Nm(y)*GS_deg*(2*f_GS(y)-1)*lorntezGS(y)/E_GS(y);
Rstim_ES1=gammay*delta_z/(2*ES_deg*Nm(y)) * (gES*(Ss_fw+Ss_bw)+sumterm_ES); %gGS same as gain of sp
Rstim_GS1=gammay*delta_z/(2*GS_deg*Nm(y)) * (gGS*(Ss_fw+Ss_bw)+sumterm_GS);
eqn_17=[eqn_17 (rho_k/(NQD_k*2*ES_deg))*(f_wl1/taw_w_E)-f_ES(y)/taw_E_w1(y)+(GS_deg/ES_deg)*(f_GS(y)/taw_G_E)-f_ES(y)/taw_E_G-f_ES(y)/taw_r-Rstim_ES1];
eqn_18=[eqn_18 (ES_deg/GS_deg)*(f_ES(y)/taw_E_G)-f_GS(y)/taw_G_E-f_GS(y)/taw_r-Rstim_GS1];
end
soln=solve(eqn_17(1),eqn_17(2),eqn_17(3),eqn_17(4),eqn_17(5),eqn_18(1),eqn_18(2),eqn_18(3),eqn_18(4),eqn_18(5));
Star Strider
on 6 Aug 2012
Edited: Star Strider
on 6 Aug 2012
I have one more request, since I had no idea your equations contained numerical constants:
eqn_17(:) = vpa(simplify(collect(expand(eqn_17(1)))),5)
eqn_18(:) = vpa(simplify(collect(expand(eqn_18(1)))),5)
That should at least make them easier to read and understand.
When I do that with the equations you posted (with the ‘syms’ declaration using the vectors of variables in your answer to Walter), I get:
eqn_17(1) = [ 3.3425e10*f_GS1 - 5.5922e11*f_ES1 + 4.6658e11*f_ES1*f_GS1 + 5.3385e10, f_ES2*(5.0e11*f_GS2 - 5.0e11) - 0.5*f_GS2*(6.6849e10*f_ES2 - 6.6849e10) - 5.9513e10*f_ES2 + 5.3385e10, f_ES3*(5.0e11*f_GS3 - 5.0e11) - 0.5*f_GS3*(6.6849e10*f_ES3 - 6.6849e10) - 5.9819e10*f_ES3 + 5.3385e10, f_ES4*(5.0e11*f_GS4 - 5.0e11) - 0.5*f_GS4*(6.6849e10*f_ES4 - 6.6849e10) - 6.0143e10*f_ES4 + 5.3385e10, f_ES5*(5.0e11*f_GS5 - 5.0e11) - 0.5*f_GS5*(6.6849e10*f_ES5 - 6.6849e10) - 6.0487e10*f_ES5 + 5.3385e10]
eqn_18(1) =[ 1.0e12*f_ES1 - 6.8043e10*f_GS1 - 9.3315e11*f_ES1*f_GS1 + 9.6967e7, f_GS2*(6.6849e10*f_ES2 - 6.6849e10) - 1.2438e9*f_GS2 - 2.0*f_ES2*(5.0e11*f_GS2 - 5.0e11) + 1.2192e8, f_GS3*(6.6849e10*f_ES3 - 6.6849e10) - 1.291e9*f_GS3 - 2.0*f_ES3*(5.0e11*f_GS3 - 5.0e11) + 1.4549e8, f_GS4*(6.6849e10*f_ES4 - 6.6849e10) - 1.316e9*f_GS4 - 2.0*f_ES4*(5.0e11*f_GS4 - 5.0e11) + 1.58e8, f_GS5*(6.6849e10*f_ES5 - 6.6849e10) - 1.3043e9*f_GS5 - 2.0*f_ES5*(5.0e11*f_GS5 - 5.0e11) + 1.5213e8]
However I cannot tell you much more than that the magnitude of your constants (most on the order of 1E+10) could cause numeric problems in the calculation.
omnia
on 6 Aug 2012
To Star
eqn_17(1) = vpa(simplify(collect(expand(eqn_17(1)))),5) [ .53385e11-.55922e12*f_ES1+.33425e11*f_GS1+.46658e12*f_GS1*f_ES1, 93129274647902536614837597442612252815992836100743339980078558169111014628930927/1744481186063737167486721053675299996653326609223580508099867270184960-195847093856089006364361094420662118260978494755606202856229994533339797215516721848650859618163/3290821285825280737063640556430009151658106510784804981101685951280027293745435115520*f_ES2+1/2*f_GS2*(309485009821345068724781056/4629590355721305-309485009821345068724781056/4629590355721305*f_ES2)-f_ES2*(2475880078570760549798248448/4951760157141521-2475880078570760549798248448/4951760157141521*f_GS2), 103626330145294271399547488616985491803143759292759825718002655074554624519894497/1941112109887430256045440963402253910309178979161816840204687408889856-103352611187346639948378249632190388870641417975316839551018028528483331966147584241340798478019/1727754924111110048732731841372233941756597751011194640559834018976810935588654940160*f_ES3+1/2*f_GS3*(309485009821345068724781056/4629590355721305-309485009821345068724781056/4629590355721305*f_ES3)-f_ES3*(2475880078570760549798248448/4951760157141521-2475880078570760549798248448/4951760157141521*f_GS3), 58373828299758524181672916154167152209897980238592151808655183299124894834982973/1093450382333523308371920425280473151189497220792306127667856291135488-220953296391003716827357847410059942870457627330863931535429522687776030286816520365244366808413/3673788080589517484187456074632233658881567029912045270631252603543983570878560468992*f_ES4+1/2*f_GS4*(309485009821345068724781056/4629590355721305-309485009821345068724781056/4629590355721305*f_ES4)-f_ES4*(2475880078570760549798248448/4951760157141521-2475880078570760549798248448/4951760157141521*f_GS4), 467662793138478911821560987337953367919454653776277233636551467144233509114019979/8760201622842936781746242469177427822640650126448722261764607318163456-420003102751321346695470874575112574093115874719657380524279107518459792945690928182057590829947/6943727017956516935151655477936956138318706341290652161933110028950528087933717577728*f_ES5+1/2*f_GS5*(309485009821345068724781056/4629590355721305-309485009821345068724781056/4629590355721305*f_ES5)-f_ES5*(2475880078570760549798248448/4951760157141521-2475880078570760549798248448/4951760157141521*f_GS5)]
eqn_18(1) = vpa(simplify(collect(expand(eqn_18(1)))),5) [ .10000e13*f_ES1-.93315e12*f_GS1*f_ES1-.68043e11*f_GS1+.96967e8, 2*f_ES2*(2475880078570760549798248448/4951760157141521-2475880078570760549798248448/4951760157141521*f_GS2)-f_GS2*(309485009821345068724781056/4629590355721305-309485009821345068724781056/4629590355721305*f_ES2)-21284357112504429712330240150093692483987070136032523715989753/17111913822378649269067506399307264442678019262251008*f_GS2+4172443290125780443262733750786428041309050873781515715989753/34223827644757298538135012798614528885356038524502016, 2*f_ES3*(2475880078570760549798248448/4951760157141521-2475880078570760549798248448/4951760157141521*f_GS3)-f_GS3*(309485009821345068724781056/4629590355721305-309485009821345068724781056/4629590355721305*f_ES3)-22125439385870456757216457977084860588799301724437220220677455/17138416528350325382144632033093883165385744116088832*f_GS3+4987022857520131375071825943990977423413557608348388220677455/34276833056700650764289264066187766330771488232177664, 2*f_ES4*(2475880078570760549798248448/4951760157141521-2475880078570760549798248448/4951760157141521*f_GS4)-f_GS4*(309485009821345068724781056/4629590355721305-309485009821345068724781056/4629590355721305*f_ES4)-3764854543803885230713906370532807563582630288577620767608609/2860819872387000249203626277813416981348911494987776*f_GS4+904034671416884981510280092719390582233718793589844767608609/5721639744774000498407252555626833962697822989975552, 2*f_ES5*(2475880078570760549798248448/4951760157141521-2475880078570760549798248448/4951760157141521*f_GS5)-f_GS5*(309485009821345068724781056/4629590355721305-309485009821345068724781056/4629590355721305*f_ES5)-44844049642880379924936339571152042071536339847931206168893117/34382843880587355216597766601334241221602387647528960*f_GS5+10461205762293024708338572969817800849933952200402246168893117/68765687761174710433195533202668482443204775295057920]
Star Strider
on 6 Aug 2012
Edited: Star Strider
on 6 Aug 2012
Or a bit more clearly, since ‘eqn_17(1)’ and ‘eqn_18(1)’ are each actually [1 x 5] symbolic vectors:
eqn_17(1,1) = 3.3425e10*f_GS1 + f_ES1*(4.6658e11*f_GS1 - 5.5922e11) + 5.3385e10
eqn_18(1,1) = 9.6967e7 - f_ES1*(9.3315e11*f_GS1 - 1.0e12) - 6.8043e10*f_GS1
eqn_17(1,2) = 3.3425e10*f_GS2 + f_ES2*(4.6658e11*f_GS2 - 5.5951e11) + 5.3385e10
eqn_18(1,2) = 1.2192e8 - f_ES2*(9.3315e11*f_GS2 - 1.0e12) - 6.8093e10*f_GS2
eqn_17(1,3) = 3.3425e10*f_GS3 + f_ES3*(4.6658e11*f_GS3 - 5.5982e11) + 5.3385e10
eqn_18(1,3) = 1.4549e8 - f_ES3*(9.3315e11*f_GS3 - 1.0e12) - 6.814e10*f_GS3
eqn_17(1,4) = 3.3425e10*f_GS4 + f_ES4*(4.6658e11*f_GS4 - 5.6014e11) + 5.3385e10
eqn_18(1,4) = 1.58e8 - f_ES4*(9.3315e11*f_GS4 - 1.0e12) - 6.8165e10*f_GS4
eqn_17(1,5) = 3.3425e10*f_GS5 + f_ES5*(4.6658e11*f_GS5 - 5.6049e11) + 5.3385e10
eqn_18(1,5) = 1.5213e8 - f_ES5*(9.3315e11*f_GS5 - 1.0e12) - 6.8154e10*f_GS5
However I am still convinced that the magnitude of many of the constants is causing numeric problems with the solution. There may also be linear dependence problems, since many of the coefficients of different variables appear to be the same.
omnia
on 6 Aug 2012
ok Star. Now it is clear but how can I use fsolve to solve them simultaneously?
when I use different values as a start of my program I get eqn_17(1,1) and eqn_18(1,1) not function of f_ES1 and f_GS1 only but combination of all variables (f_ES1,....,f_ES5,f_GS1,... ,f_GS5). I just start with the simplest case
Walter Roberson
on 6 Aug 2012
In order to use fsolve(), use matlabFunction() to convert the symbolic expressions into function handles. fsolve() accepts function handles on input.
Walter Roberson
on 7 Aug 2012
Did R14 have anonymous functions?
omnia
on 9 Aug 2012
Edited: Walter Roberson
on 9 Aug 2012
I tried to solve the equations by the following commands:
eqns=[eqn_17; eqn_18]; %10 equations written as colomn
x0=ones(10,1); %initial values for the 10 unknowns
options=optimset('Display','iter');
[soln,fval]=fsolve(myfun1(eqns),x0,options);
where
function F = myfun1(eqns)
var=findsym(eqns);
F=double(subs(eqns,var,ones(1,10))) ;
but I have the following error
??? Error using ==> optim\private\lsqfcnchk
FUN must be a function, a valid string expression,
or an inline function object.
Error in ==> fsolve at 169
funfcn = lsqfcnchk(FUN,'fsolve',length(varargin),funValCheck,gradflag);
Error in ==> try_GS_ES_prob at 41
[soln,fval]=fsolve(myfun1(eqns),x0,options);
How can I solve it? Any help will be useful
Thanks
Walter Roberson
on 9 Aug 2012
findsym() returns all symbolic variables, so you are substituting ones for all symbolic variables in "eqns" and so your "F" will be a numeric value (a vector likely.) You are not leaving anything to vary to be solved by fsolve()
Alan Weiss
on 9 Aug 2012
Edited: John Kelly
on 2 Mar 2015
You might want to use matlabFunction to convert your symbolic variables to a function handle that will give your objective function. It will be faster than repeatedly using subs. See the example here.
Alan Weiss
MATLAB mathematical toolbox documentation
Walter Roberson
on 9 Aug 2012
The poster is using R14. I don't know if matlabFunction() was available back then?
omnia
on 11 Aug 2012
matlabfunction is not available in my version which I think makes this issue tedious for me.
To Walter: I do not understand what do you mean by "You are not leaving anything to vary to be solved by fsolve()". I think what I wrote is the syntax expression to fsolve. I passed the equations to 'myfun1' (symbolic equations). Then I specified the symbols to be substituted with the initial guess (ones for all the symbols).
Answers (1)
Honglei Chen
on 2 Aug 2012
Do you have Optimization Toolbox? If so, you can try fsolve
6 Comments
Image Analyst
on 2 Aug 2012
So, did you try casting your data to double? If not, what did you think to do when you saw this error?
Honglei Chen
on 2 Aug 2012
Solve is part of Symbolic Computation Toolbox. Since you said the result is not satisfying, I'm suggesting you to try the numerical way to solve it, i.e. fsolve. You may need to redefine your variable and rewrite your equations to fit the syntax.
Walter Roberson
on 5 Aug 2012
Use matlabFunction() to convert the symbolic equations into something that fsolve() can deal with.
See Also
Categories
Find more on Symbolic Computations in MATLAB in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)