MATLAB Answers

Using "solve" function to solve a system of equations in terms of two of 12 variables

8 views (last 30 days)
I am trying to solve these variables in terms of dQ3 and dQ5. (This is for structures homework, so we're dealing with virtual forces that will cancel in the end.) I am going to do it by hand, but I am still curious if there is a way to solve this in Matlab.
I looked at others' questions on Answers, but I only found examples of rearranging one equation instead of 10 simultaneously. dQ3 and dQ5 do not appear in all equations, so I am unsure how to specify that I want the solution in terms of these equations. Is this possible? Will it not work because the variables all have solutions that are 0?
syms dY1 dX1 dY3 dQ3 dQ5 dP12 dP14 dP23 dP24 dP25 dP35 dP45
eq1_v = 1.155*dY1 + dP14 == 0;
eq2_v = dX1 + dP12 + 0.5*dP14 == 0;
eq3_v = dP24 + dP25 == 0;
eq4_v = dP23 + 0.5*dP25 - dP12 - 0.5*dP24 == 0;
eq5_v = dY3 + 0.866*dP35 == 0;
eq6_v = dQ3 - 0.5*dP35 - dP23 == 0;
eq7_v = dP24 + dP14 == 0;
eq8_v = dP45 + 0.5*dP24 - 0.5*dP14 == 0;
eq9_v = dQ5 - dP25 - dP35 == 0;
eq10_v = dP35 - dP25 - 2*dP45 == 0;
eqs_v = [eq1_v eq2_v eq3_v eq4_v eq5_v eq6_v eq7_v eq8_v eq9_v eq10_v];
Sv = solve(eqs_v, [dY1, dX1, dY3, dP12, dP14, dP23, dP24, dP25,...
dP35, dP45], 'dQ3', 'dQ5');
This is just another side question; just wondering if I can be lazy:
Also, is this the neatest way to display the variables and their numerical solutions (simply outputting 'Sv.--')? Is there a way to make a table? I figured out how to sort the numerical answers into a matrix, but I can't find how to turn the variables into strings without rewriting an entire new matrix of strings. I used the "fieldnames" function to get the variable names from the solve structure, but it outputs a cell array of cell arrays. Is there a shortcut?


Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 18 Jan 2019
arrayfun(@(EE) ismember(dQ3, symvar(EE)), eqs_v)
that will tell you which equations contain the first variable . Pick one of them . solve that one for the first variable . subs() the result for the variable in all of the other equations, leaving you with one fewer active equations .
repeat with respect to the second variable .
now back substitute the solution to the second equation into the solution to the first. You now have solutions for two variables and N-2 transformed equations .


Jasmine Alvarez
Jasmine Alvarez on 21 Jan 2019
Thank you.
I also found that if you simply exclude the variables you want to remain in the equation that the solve() function will solve the rest of the variables in terms of those that aren't provided.
Walter Roberson
Walter Roberson on 21 Jan 2019
If the number of variables you ask to solve for does not match the number of equations, then most of the time MATLAB will tell you that it could not find a solution.

Sign in to comment.

More Answers (0)

Sign in to answer this question.