# Solving a system of equations in a loop efficiently

8 views (last 30 days)
Carlos Acasuso on 30 Nov 2020
Commented: Carlos Acasuso on 2 Dec 2020
Hello,
I am trying to solve a system of 14 equations (14 unknowns) for three given, known variables:
lDamperFL = 219;
lDamperFR = 220;
xSteerRack = 0;
The three given, known variables above are just an example of an instance from telemetry data from a racecar, logged at 500Hz. This means that for a session of data I can be looking at >50,000 data points, where I have to solve the system of equations in a "for loop" for each data instance, and then substituting my 14 unknowns to find the solution to my problem.
At the moment my solver is capable of solving the system of equations using vpasolve as shown below:
syms TzChassis RxChassis TxWheelLH TyWheelLH RxWheelLH RyWheelLH RzWheelLH TxWheelRH TyWheelRH RxWheelRH RyWheelRH RzWheelRH BellcrankRot_LH BellcrankRot_RH;
eqn1 = Length.FLTWB_FWD == SRHLength.FTWB_FWD;
eqn2 = Length.FLTWB_RWD == SRHLength.FTWB_RWD;
eqn3 = Length.FLLWB_FWD == SRHLength.FLWB_FWD;
eqn4 = Length.FLLWB_RWD == SRHLength.FLWB_RWD;
eqn5 = Length.FLPRod == SRHLength.FPRod;
eqn6 = Length.FLTRod == SRHLength.FTRod;
eqn7 = Length.FRTWB_FWD == SRHLength.FTWB_FWD;
eqn8 = Length.FRTWB_RWD == SRHLength.FTWB_RWD;
eqn9 = Length.FRLWB_FWD == SRHLength.FLWB_FWD;
eqn10 = Length.FRLWB_RWD == SRHLength.FLWB_RWD;
eqn11 = Length.FRPRod == SRHLength.FPRod;
eqn12 = Length.FRTRod == SRHLength.FTRod;
eqn13 = Length.LH_FSD == lDamperFL;
eqn14 = Length.RH_FSD == lDamperFR;
eqns = [ eqn1, eqn2, eqn3, eqn4, eqn5, eqn6, eqn7, eqn8, eqn9, eqn10, eqn11, eqn12, eqn13, eqn14 ];
vars = [ TzChassis RxChassis TxWheelLH TyWheelLH RxWheelLH RyWheelLH RzWheelLH TxWheelRH TyWheelRH RxWheelRH RyWheelRH RzWheelRH BellcrankRot_LH BellcrankRot_RH ];
digits(5)
Ssym = vpasolve(eqns,vars);
Please note that each of the equations above are as well a function of the unknown variables as well.
This takes around 0.4s to solve at each instance of the loop, taking several hours to complete.
I wonder if there is any more efficient way of solving a system of equations as such.
C

Ameer Hamza on 30 Nov 2020
Edited: Ameer Hamza on 30 Nov 2020
vpasolve() uses the symbolic toolbox, which is inherently slow. I guess you can use several-fold speed gain if you use a numerical function such as fsolve(). However, you will need to rewrite your equations
Following shows an example. Consider a system of 2 equations Using symbolic toolbox:
syms x y
eq = [x + 2*y == 3; x - y == 0];
sol = solve(eq);
See the timing
>> timeit(@() solve(eq))
ans =
0.0635
fsolve():
eq = @(x, y) [x + 2*y - 3; x - y - 0];
opts = optimoptions('fsolve', 'Display', 'none');
sol = fsolve(@(x) eq(x(1), x(2)), rand(2,1), opts)
Timing
>> timeit(@() fsolve(@(x) eq(x(1), x(2)), rand(2,1), opts))
ans =
8.5870e-04
Carlos Acasuso on 2 Dec 2020
That was super useful, thank you!

### Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

R2019b

### Community Treasure Hunt

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

Start Hunting!