Solving a system of equations in a loop efficiently
6 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Carlos Acasuso
il 30 Nov 2020
Commentato: Carlos Acasuso
il 2 Dic 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.
Thank you for your help,
C
0 Commenti
Risposta accettata
Ameer Hamza
il 30 Nov 2020
Modificato: Ameer Hamza
il 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
About 75 time faster.
3 Commenti
Ameer Hamza
il 30 Nov 2020
It just means that fsolve() accept a function handle with a single input. For example, if you have a function with 3 inputs fun(x, y, z). fsolve() does not recognize them seperately. fsolve() is capable of solving a function with single vector input. Therefore, a define a new function fun2(X) [where X is a 3x1 vector] like this
fun = @(x, y, z) ...
fun2 = @(X) fun(X(1),X(2),X(3))
In my above code if I directly define 'eq' in term of 'x', then I can avoid creating another function handle inside fsolve()
eq = @(x) [x(1) + 2*x(2) - 3; x(1) - x(2) - 0];
opts = optimoptions('fsolve', 'Display', 'none');
sol = fsolve(eq, rand(2,1), opts)
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Equation Solving in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!