Find a variable as function of other variable with equations system using Symbolic ToolBox
21 views (last 30 days)
Show older comments
Evyatar Sivan
on 4 Apr 2020
Commented: Walter Roberson
on 4 Apr 2020
I have 3 equations that connects between 4 variables, let's say:
syms a b c d;
r=b+d;
e=pi/2;
eq1 = c - a*sin(d) + b*cos(d) == 0;
eq2 = r - c*tan(e) - a*cos(d) - b*sin(d) == 0;
eq3 = b == e*r + a*tan(e);
Now I would like to find c(d), means, I would like to find what is c as a function of other variable, in this case - d.
Then perform other operations on c(d) such as differntiate etc.
What would be the best/easiest way to do that using Symbolic Toolbox?
Thanks.
2 Comments
Walter Roberson
on 4 Apr 2020
10 radians is an unusual angle, but that is probably not so important for your question.
Accepted Answer
Walter Roberson
on 4 Apr 2020
syms a b c d;
r = b+d;
e = sym(pi)/10; %not pi/2, that does not have a solution!
eq1 = c - a*sin(d) + b*cos(d) == 0;
eq2 = r - c*tan(e) - a*cos(d) - b*sin(d) == 0;
eq3 = b == e*r + a*tan(e);
sol = solve([eq1,eq2,eq3], [a, b, c], 'returnconditions', true);
c = symfun(simplify(sol.c), d);
This will be valid except at four sets of d values whose values can be deduced by analyzing sol.conditions (in each case adding an integer multiple of 2*pi gets another forbidden value)
2 Comments
Walter Roberson
on 4 Apr 2020
double(rad2deg(theta_max(3))
ans =
53.0352986075461 - 6.4856523980211e-71i
The complex portion is due to round-off error.
However, I recommend that you rewrite your code to cut down on the repeated numeric constants, and that you switch into symbolic mode for greater accuracy. Using solve() with equations with floating point constants is always a category mistake: solve() is for exact solutions, and floating point constants are by definition only approximations (except for the ones that are integers.)
Z = @(v) sym(v);
% Unknown Variables
syms Pa N T theta;
% Given Variables
factor507 = Z(5.07);
factor93 = Z(0.93);
L = factor507/sin(theta);
delta = deg2rad(Z(10));
c = Z(10);
gamma = Z(20);
phi = deg2rad(Z(20));
Q = c*factor507/tan(theta);
W = ((factor507/tan(theta))*factor93 + factor507*(factor507/tan(theta))*Z(0.5))*gamma;
% Equations
eq1 = Pa-N*sin(theta)+T*cos(theta) == 0;
eq2 = Q+W-Pa*tan(delta)-N*cos(theta)-T*sin(theta) == 0;
eq3 = T == c*L + N*tan(phi);
% Solution
sol = solve([eq1,eq2,eq3], [N, T, Pa], 'returnconditions', true);
Pa_theta = symfun(simplify(sol.Pa), theta);
eq4 = diff(Pa_theta,theta) == 0;
theta_max = solve(eq4,theta);
rad2deg(theta_max)
More Answers (1)
Ameer Hamza
on 4 Apr 2020
try this example
syms a b c d;
r=b+d;
e=pi/2;
eq1 = c - a*sin(d) + b*cos(d) == 0;
eq2 = r - c*tan(e) - a*cos(d) - b*sin(d) == 0;
eq3 = b == e*r + a*tan(e);
sol = solve([eq1 eq2 eq3], [a b c]);
C = sol.c; % c as function of d
% Now find its derivative
dC_dd = diff(C,d); % dc/dd
% you can also integrate it
IC = int(C,d);
0 Comments
See Also
Categories
Find more on Assumptions in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!