Solve overspecified equation system
2 次查看(过去 30 天)
显示 更早的评论
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
编辑: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'...
'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 个评论
Stephan
2019-10-29
编辑: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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Systems of Nonlinear Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!