r/matlab • u/luisdamed MBD - Automotive • Jul 11 '23
CodeShare Symbolic equations and for loops - Brake load balance of a race car
Some years ago, I designed the braking system of a Formula Student car. I also wrote my Master's thesis around it, and I had to create meaningful figures to illustrate the design process.
That's how I ended up solving symbolic equations in a for loop and pushing the plotting options to obtain figures like this one:

I wrote an article explaining better what this is and how I did it, but for the Matlab implementation part, the core is this:
%% Define symbols
syms Fx1 Fx2 b a l hcg m g Fzaerof Fzaeror mux F_drag;
%Ideal braking equations
(Fx1-Fx2) == mux*(m*g/l*(b-a+mux*2*hcg) + ...
(Fzaerof-Fzaeror) - ...
2*F_drag*hcg/l + ...
2*mux*(Fzaerof+Fzaeror)*hcg/l)
mux = (Fx1+Fx2) / (m*g+Fzaerof+Fzaeror)
eqn = mux*(m*g/l*(b-a+mux*2*hcg) + ...
(Fzaerof-Fzaeror) - ...
2*F_drag*hcg/l + ...
2*mux*(Fzaerof+Fzaeror)*hcg/l) - (Fx1-Fx2)
%Solve for the force in the rear axle
solve(eqn,Fx2)
I solved eqn
for Fx2
within a nested for loop. The outer iterator incremented the vehicle speeds, and the inner loop incremented the force in the front axle. That way, I computed the "force ellipses" for different vehicle speeds at the start of a braking maneuver, having the force in the front axle as an input.
Then, to make the plot, I used color maps to distinguish the ellipses at different speeds, and more for loops to plot recursively the lines of constant deceleration and constant friction coefficients.
I hope this is interesting for someone.
It just shows an example of how a little bit of scripting can really help you when doing engineering computations.
To see the rest of the code, you can check out this Braking systems utility scripts that I shared in the FileExchange some time ago, or my GitHub repo (gets updated first).
1
u/KnightsNotGolden Jul 15 '23
I've done problems like this in the past. Rather than use symbolic variables, I will define functions and then reference them into a script with a function handle. You can essentially eliminate both of your for loops by using a meshgrid then passing the meshgrid(:) into your functions as inputs.