Solve overspecified equation system

2 views (last 30 days)
Nikolas Haimerl
Nikolas Haimerl on 28 Oct 2019
Edited: Stephan on 29 Oct 2019
I am trying to solve a set of equations where I have 3 variables and 4 equations which are not linearly dependent.
Is there a way to solve this numerically in matlab since it is not possible to do it analytically.
Thanks for any help!
  2 Comments
Stephan
Stephan on 28 Oct 2019
Please provide the code or at least the system.
Nikolas Haimerl
Nikolas Haimerl on 28 Oct 2019
Edited: Stephan on 28 Oct 2019
syms u1 u2 up2
eqns= [cos(u1)*l1 + cos(u2)*l2 == xa,...
sin(u1)*l1 + sin(u2)*l2 == ya,...
ap0==(xpa*cos(u2))/(4*(cos(u1)*sin(u2) - cos(u2)*sin(u1))) +...
(ypa*sin(u2))/(4*(cos(u1)*sin(u2) - cos(u2)*sin(u1))),...
up2 == - (xpa*cos(u1))/(4*(cos(u1)*sin(u2) - cos(u2)*sin(u1))) -...
(ypa*sin(u1))/(4*(cos(u1)*sin(u2) - cos(u2)*sin(u1)))];
S=fsolve(eqns,[u1 u2 up2])
This is what I have tried to do so far. u1 u2 up2 are the variables. xpa,ypa,xa,ya,l1,l2 and ap0 are constants.

Sign in to comment.

Accepted Answer

Stephan
Stephan on 28 Oct 2019
Edited: Stephan on 28 Oct 2019
If you have symbolic toolbox, use:
% This part builds a system to solve with fsolve
syms u1 u2 up2 l1 l2 xa ya xpa ypa ap0
x = sym('x', [1 3]);
eqns(1) = cos(u1)*l1 + cos(u2)*l2 - xa;
eqns(2) = sin(u1)*l1 + sin(u2)*l2 - ya;
eqns(3) = ap0 -(xpa*cos(u2))/(4*(cos(u1)*sin(u2) - cos(u2)*sin(u1))) +...
(ypa*sin(u2))/(4*(cos(u1)*sin(u2) - cos(u2)*sin(u1)));
eqns(4) = up2 + (xpa*cos(u1))/(4*(cos(u1)*sin(u2) - cos(u2)*sin(u1))) -...
(ypa*sin(u1))/(4*(cos(u1)*sin(u2) - cos(u2)*sin(u1)));
eqns = subs(eqns,[u1, u2, up2],[x(1), x(2), x(3)]);
fun = matlabFunction(eqns,'vars',{x,'l1','l2','xa','ya','xpa','ypa'...
'ap0'});
fun = str2func(replace(func2str(fun),"in1","x"));
Then fun is a function handle that you can work with:
% solve the system with fantasy values - use your values
l1 = 3;
l2 = 2;
xa = 3;
ya = 6;
xpa = 0.5;
ypa = -1;
ap0 = 5.7;
% solve the system using fsolve
opts = optimoptions('fsolve','Algorithm','Levenberg-Marquardt');
sol = fsolve(@(x)fun(x,l1,l2,xa,ya,xpa,ypa,ap0),rand(1,3),opts)
% look at the results using the solution fsolve calculated
% ideally there should be 4 zeros, if it worked good - ohterwise
% your system may have a problem
test_results = fun(sol,l1,l2,xa,ya,xpa,ypa,ap0)
For my fantasy values there was not a good result - you have to check using your values and also have a look at the correct implementation of the equations in the first part. They should all be formulated as F = 0, to work with them using fsolve.
  2 Comments
Nikolas Haimerl
Nikolas Haimerl on 29 Oct 2019
Thank you for your help. Using the code you provided I get the following error:
Error using optimoptions
Invalid solver specified. Provide a solver name or handle (such as 'fmincon' or @fminunc).
Type DOC OPTIMOPTIONS for a list of solvers.
Error in set_params_simulink_simulation (line 100)
opts = optimoptions('fsolve','Algorithm','Levenberg-Marquardt');
Line 100 is the following: opts = optimoptions('fsolve','Algorithm','Levenberg-Marquardt');
Stephan
Stephan on 29 Oct 2019
Edited: Stephan on 29 Oct 2019
For me this works without any errors on R20129b - I wonder why.
But you could leave the options away also, fsolve will switch the algorithm automatically, if it detects a non square system. You will just get a warning, that algorithm is changed.
% opts = optimoptions('fsolve','Algorithm','Levenberg-Marquardt');
sol = fsolve(@(x)fun(x,l1,l2,xa,ya,xpa,ypa,ap0),rand(1,3))
leads to:
Warning: Trust-region-dogleg algorithm of FSOLVE cannot handle non-square systems;
using Levenberg-Marquardt algorithm instead.
> In fsolve (line 316)
In Untitled9 (line 27)
No solution found.
fsolve stopped because the last step was ineffective. However, the vector of function
values is not near zero, as measured by the value of the function tolerance.
<stopping criteria details>
sol =
1.0875 1.1366 -5.7022
test_results =
-0.7646 -1.5292 -0.0009 0.0000

Sign in to comment.

More Answers (0)

Categories

Find more on Systems of Nonlinear Equations in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

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

Start Hunting!