MATLAB Answers

Multiple equations solving in MATLAB

32 views (last 30 days)
Golam Ismot Ara taposhe
Golam Ismot Ara taposhe on 24 Jan 2020
I have 10 equations and 10 variables. 6 of them are non-linear and 4 of then are linear . I have 2 constraints. I have tried several programming .nothing is working out for me.
i was wondering if anyone can tell me how to solve it in MATLAB


Show 1 older comment
Golam Ismot Ara taposhe
Golam Ismot Ara taposhe on 31 Jan 2020
Hi walter
thank you for responding
Yes, i need numerical solutions
approximate answers are ok too even it is not exact solution.
Golam Ismot Ara taposhe
Golam Ismot Ara taposhe on 31 Jan 2020
Hi walter
Is there any way I can solve it in MATLAB?
I was wondering if you can give any suggestion here?
Thank you for your time
Golam Ismot Ara taposhe
Golam Ismot Ara taposhe on 14 Feb 2020
Hi walter
I have solved it, thank you so much for your constant support

Sign in to comment.

Answers (2)

Alex Sha
Alex Sha on 28 Jan 2020
Hi, Golam, post out your problem, we may try.


Walter Roberson
Walter Roberson on 10 Feb 2020
The flow was something like this.
X = sym('x', [1 44]);
F = root2d(X);
vars = symvar(F);
nvars = length(vars);
F10fun = matlabFunction(F(1:10), 'vars', {vars});
x0 = 0.01 * ones(1, nvars);
%unfortunately some entries from fsolve are negative
F10sol = fsolve(F10fun, x0);
%so we pop that into vpasolve with a bounds restriction
%we did not just go directly to vpasolve because it is too slow without a decent guess
F10_restricted_struct = vpasolve(F10fun, abs(F10sol), vars, repmat([0 1], nvars, 1));
%vpasolve returns a struct, we need a vector
F10_restricted = double(cell2mat(struct2cell(F10_restricted_struct)));
%now we have a vector of initial values
%proceed to minimize residue for the full set of equations
residue11 = matlabFunction( sum(F.^2), 'vars', {vars});
F11min = fmincon(residue11, F10sol, [], [], [], [], zeros(1,nvar)); %zeros is lb
%F11min should in theory now be a list of values that give a good fit for the entire
%set of 11 equations. Unfortunately in practice some of the values went towards .9
%when they should have been about 1e-2 -- perhaps an ub parameter is called for
Golam Ismot Ara taposhe
Golam Ismot Ara taposhe on 11 Feb 2020
Hi Walter
From vapsolve it took almost 5 hours to get some values.
I got several warning values like below and multiple values of each vaiables (I have given one of them to show you).
{Warning: Solution '[x11 = -0.38750367826943393346203707487446, x22 =
-0.00000060583325942090259608703097820327, x33 =
-0.000038650066836096378956219616167947, x44 = -0.0000048908827834088449488126789731625,
x12 = 0.85110289647020175449525875148945, x13 =
0.37292297133288576681275288927368, x14 = 0.0027533507357803688041407060780671, x24 =
0.16090647871265852412483387878892, x23 =
0.000010135483658517192456190547613278, x34 = -0.00014800768287208782214890388479803]'
seems to be affected by some numeric instability.
Inserting this solution into equation 'x12^2 - (6626207954368463*x11*x22)/2147483648 = 0'
produces the residue
3.6665527467324870120747140518347e+69 + 4.3567273814115433908181896380624e+70i
3.6665527467324870120747140518347e+69 - 4.3567273814115433908181896380624e+70i
2.8254024107173870504811031811197e+70 - 5.9311882667731407548267433191443e+70i
2.8254024107173870504811031811197e+70 + 5.9311882667731407548267433191443e+70i
1.7685725013650819705301561897085e+70 + 1.8063164266990928662426900108303e+70i
Golam Ismot Ara taposhe
Golam Ismot Ara taposhe on 11 Feb 2020
Hi Walter
like you said fsolve() gave me a good result but one variable is negative. I tried to use your flow this restricted_structure i am unfamilier and giving me error.
I was wondering if you can help me to figure this out
thank you so much for your time
>>F10_restricted_struct = vpasolve(F10fun, abs(F10sol), vars, repmat([0 1], nvars, 1));
Error using sym.getEqnsVars (line 32)
Arguments must be symbolic objects.
Error in sym/vpasolve (line 132)
[eqns,vars] = sym.getEqnsVars(varargin{1:N});
>> F10_restricted = double(cell2mat(struct2cell(F10_restricted_struct)));
Unrecognized function or variable 'F10_restricted_struct'.
>> F11min = fmincon(residue11, F10sol, zeros(1,nvar));
Unrecognized function or variable 'residue11'.
Did you mean:
>> F11min = fmincon(residue, F10sol, zeros(1,nvar));
Not enough input arguments.
Error in residue (line 81)
u = u(:).'; v = v(:).'; k =[];

Sign in to comment.

Samatha Aleti
Samatha Aleti on 28 Jan 2020
You may use “fsolve” and “solve” functions to solve system of non-linear equations and linear equations respectively. Refer the following documentation links on detailed explanation on using “fsolve” and “solve”:


Show 3 older comments
Golam Ismot Ara taposhe
Golam Ismot Ara taposhe on 13 Feb 2020
Hi Walter,
Thank you for your time
fmincon() has some [ ], [ ] . what should i input inside this [ ] . sorry for asking lame question. I am not familier with this optimity tolerance/ fmincon
thanks again
Walter Roberson
Walter Roberson on 13 Feb 2020
[F11min, F11fval] = fmincon(residue11, F10_restricted, [], [], [], [], zeros(1,nvars), [], [], options); %zeros is lb
empty = zeros(0,0);
A = empty;
b = empty;
Aeq = empty;
beq = empty;
lb = zeros(1,nvars);
ub = empty;
nlcon = empty;
[F11min, F11fval] = fmincon(residue11, F10_restricted, A, b, Aeq, beq, lb, ub, nlcon, options);
That is exactly equivalent for calling purposes, but it documents more clearly what the various parameters are standing for.
To clarify: the [] in the function call were not placeholders indicating values that you had to fill in: instead [] is the empty array, 0 x 0, and using it signals to MATLAB that you do not want to supply a value for the parameter. For example, [] in the A and b locations indicates that you have no linear constraints to work with.
In some contexts, [] in a MATLAB call indicates that you want the default to be supplied, instead of indicating that you want to skip the parameter. For example, if you had
x = rand(6,5);
reshape(x, 2, [], 5)
would signal to MATLAB that MATLAB should calculate how many elements are in x, and divide by 2 and 5, and fill in what is left in place of the [] -- so 30 / 2 / 5 giving 3, making the call equivalent to reshape(x, 2, 3, 5)
Whether [] signals to a routine to de-activate functionality (like fmincon) or to provide default functionality or values (like reshape) is up to the routine. If you mentally translate it as, "I don't want to bother putting this in" and expecting the routine to do something "sensible" then that will be pretty close.
Golam Ismot Ara taposhe
Golam Ismot Ara taposhe on 14 Feb 2020
hi walter
thank you so much for your help. will be greatful for this

Sign in to comment.

Sign in to answer this question.