Asked by Nikolas Haimerl
on 28 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!

Answer by Stephan
on 28 Oct 2019

Edited by Stephan
on 28 Oct 2019

Accepted Answer

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.

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
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.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Stephan (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487888-solve-overspecified-equation-system#comment_760937

## Nikolas Haimerl (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/487888-solve-overspecified-equation-system#comment_760943

Sign in to comment.