MATLAB Answers

Solve overspecified equation system

2 ビュー (過去 30 日間)
Nikolas Haimerl
Nikolas Haimerl 2019 年 10 月 28 日
Edited: Stephan 2019 年 10 月 29 日
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 件のコメント

Stephan 2019 年 10 月 28 日
Please provide the code or at least the system.
Nikolas Haimerl
Nikolas Haimerl 2019 年 10 月 28 日
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.

サインイン to comment.


Stephan 2019 年 10 月 28 日
編集済み: Stephan 2019 年 10 月 28 日
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'...
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 件のコメント

Nikolas Haimerl
Nikolas Haimerl 2019 年 10 月 29 日
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 2019 年 10 月 29 日
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

サインイン to comment.

More Answers (0)

サインイン してこの質問に回答します。




Translated by