Find a variable as function of other variable with equations system using Symbolic ToolBox

21 views (last 30 days)
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.

Accepted Answer

Walter Roberson
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
Evyatar Sivan
Evyatar Sivan on 4 Apr 2020
Thank you so much Walter.
looks like it's working good, but still for some reason I get complex result which I know is not the answer.
I've been over this for hours... wish you might be able to help if there's anything wrong with the code.
my code:
clc;
clear all;
% Unknown Variables
syms Pa N T theta;
% Given Variables
L=5.07/sin(theta);
delta=deg2rad(10);
c=10;
gamma=20;
phi=deg2rad(20);
Q=50.7/tan(theta);
W=((5.07/tan(theta))*0.93+5.07*(5.07/tan(theta))*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); % gives 4 possible solutions
rad2deg(theta_max(1)) % not quite what I expected... suppose to be 53 degress.
Thank you a lot.
Walter Roberson
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)

Sign in to comment.

More Answers (1)

Ameer Hamza
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);

Tags

Community Treasure Hunt

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

Start Hunting!