Solve system of equations using numeric substitutions

I have a system of 4 equations with 9 variables, 6 of which are known. Obviously this is enough information to find a single, exact solution for the variable I'm looking for but for the life of me I can't figure out MATLAB's complex syntax between numerical, symbolic, polynomial, etc solvers to just set the variables plug the equations into each other and solve for a specific variable. I'm trying to learn how to do this so I can start putting in large sets of complex equations and solve for a given variable without having to manually rearrange and plug and chug. Can someone help me figure out how to solve for "r_fit" here? This is my attempt at setting up everything in preparation to tell MATLAB to solve for r_fit but I'm getting confused between the different solvers' syntax.
c = 34500;
f_p = 190;
V_b = 86;
L_eff = 7.24;
D_min = 1;
A_min = pi*(D_min/2)^2;
syms r_fit A_eff L_act
e1 = A_eff == A_min*(1+0.576*L_act/(2*r_fit));
e2 = L_eff == L_act + D_min;
e3 = A_min == pi*(D_min/2)^2;
e4 = f_p == (c/(2*pi))*sqrt(A_eff/(L_eff*V_b));

 Accepted Answer

In your code,
A_min = pi*(D_min/2)^2;
syms r_fit A_eff L_act
e1 = A_eff == A_min*(1+0.576*L_act/(2*r_fit));
e2 = L_eff == L_act + D_min;
e3 = A_min == pi*(D_min/2)^2;
e4 = f_p == (c/(2*pi))*sqrt(A_eff/(L_eff*V_b));
This e3 repeats as a test the definition you gave for A_min. Therefore e3 is always true, and contributes no information. This reduces you to 3 equations in 3 unknowns, which is something you can solve() for. Always solve for the same number of variables as equations; if you are not interested in the other values you do not need to examine them.
sol = solve(e1, e2, e4, r_fit, A_eff, L_act);
sol.r_fit

3 Comments

Walter, thank you very much. This is all I wanted to do. Turns out this is the classic case of me getting a few complex ideas in my head and making it much more difficult than it really was. I suspected that was the case.
You are absolutely right about A_min. Again, another mistake on my part, which I made when trying to fix the other mistake...
I also noticed that I don't need to explicitly put "r_fit, A_eff, L_act" into the solve function. It will automatically solve for each unknown and spit them into a structure.
Also, I have one followup question, if you don't mind. I noticed that when I input "sol.r_fit" it returns the numerical answer. However, if I try to view r_fit in the Variables browser by double-clicking on the "sol" variable in the Workspace then double-clicking on r_fit it shows nothing. Know why that is?
I am not familiar with how the variable browser acts for symbolic values. And note that the value is symbolic, not numeric: you can double() it to get the numeric equivalent.
I always recommend listing the variables to be solved for. It acts as documentation for those reading your code, and it makes it easier to generalize the code, such as by making one of the parameters symbolic instead of numeric: if you specifically named what you are solving for then solve() can express those in terms of the parameter, but if you rely upon solve() to figure out what to solve for then you run the risk that it will solve for the wrong thing or decide that it doesn't know how to solve for 4 variables in three equations and return [] instead.
Thanks. I guess it doesn't show anything because it's still a symbolic. Like you said, I only get a numeric output when I double() it. And, yes, listing the variables is probably better in any case. Thanks again.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!