explicit vs numerical solution for system of equations
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Oliver Schneck
am 8 Feb. 2024
Bearbeitet: Walter Roberson
am 9 Feb. 2024
I am trying to solve a system of equations using the syms and solve functions. My code was working before, but MATLAB forced me to install an add in, and now my system is not producing any answers. When it ran well before, the command window said that it couldn't find an explicit solution, and looked for a numerical one instead, which worked. Now it only says it is looking for an explicit solution, and produces no answer. Here is my code. If anyone knows how I can get it to look for a numerical solution again, please let me know. Thanks!
syms r1 r2 theta h
%% Define Constraining Values
a = 2;
b = 5;
c = 4;
h_c = 5;
w_rectangle = h_c + ((b*c)/(a+b));
h_bar_rectangle = (a*c)/(a+b);
%% Define how to solve for constrained variables
%-------------------------------------------
%w
a1 = h*(a+b);
a2 = ((r1*(1-cos(theta)))*(a+b))-((theta*r1*r1)-(.5*a*r1*cos(theta)));
a3 = ((theta*r2*r2)-(.5*b*r2*cos(theta)));
w_circles = (a1+a2+a3)/(a+b);
%-------------------------------------------
% h_bar
b_rectangle = b*r2;
b_slice = theta*r2*r2;
b_triangles = .5*b*r2*cos(theta);
a_slice = theta*r1*r1;
a_triangles = .5*a*r1*cos(theta);
a_rectangle = a*((r1*cos(theta))-(r2*(1-cos(theta))));
area_a = a_slice + a_triangles - a_rectangle;
area_b = b_rectangle - b_slice - b_triangles;
area_tot = area_a + area_b;
h_bar_circles = area_tot/(a+b);
%-------------------------------------------
eqns = [a==2*r1*sin(theta), b==2*r2*sin(theta),w_rectangle==w_circles,h_bar_rectangle==h_bar_circles];
vars = [r1 r2 theta h];
[r1, r2, theta, h] = solve(eqns,vars)
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 8 Feb. 2024
syms r1 r2 theta h
%% Define Constraining Values
a = 2;
b = 5;
c = 4;
h_c = 5;
w_rectangle = h_c + ((b*c)/(a+b));
h_bar_rectangle = (a*c)/(a+b);
%% Define how to solve for constrained variables
%-------------------------------------------
%w
a1 = h*(a+b);
a2 = ((r1*(1-cos(theta)))*(a+b))-((theta*r1*r1)-(.5*a*r1*cos(theta)));
a3 = ((theta*r2*r2)-(.5*b*r2*cos(theta)));
w_circles = (a1+a2+a3)/(a+b);
%-------------------------------------------
% h_bar
b_rectangle = b*r2;
b_slice = theta*r2*r2;
b_triangles = .5*b*r2*cos(theta);
a_slice = theta*r1*r1;
a_triangles = .5*a*r1*cos(theta);
a_rectangle = a*((r1*cos(theta))-(r2*(1-cos(theta))));
area_a = a_slice + a_triangles - a_rectangle;
area_b = b_rectangle - b_slice - b_triangles;
area_tot = area_a + area_b;
h_bar_circles = area_tot/(a+b);
%-------------------------------------------
eqns = [a==2*r1*sin(theta), b==2*r2*sin(theta),w_rectangle==w_circles,h_bar_rectangle==h_bar_circles].'
vars = [r1 r2 theta h]
Sol1 = solve(eqns(1:3),[r1 r2 h]);
eqns4 = subs(eqns(4:end), Sol1);
theta_sol = solve(eqns4, theta);
Sol = subs(Sol1, theta, theta_sol);
Sol.theta = theta_sol
%cross-check
vpa(subs(eqns, Sol))
3 Kommentare
Walter Roberson
am 9 Feb. 2024
I broke the solution into two parts: first solving for r1, r2, and h, and then solving for theta.
Walter Roberson
am 9 Feb. 2024
Bearbeitet: Walter Roberson
am 9 Feb. 2024
Hah, you can find solutions more directly by just changing the order of the variables you solve for!
Notice that the solution is different than the above.
syms r1 r2 theta h
%% Define Constraining Values
a = 2;
b = 5;
c = 4;
h_c = 5;
w_rectangle = h_c + ((b*c)/(a+b));
h_bar_rectangle = (a*c)/(a+b);
%% Define how to solve for constrained variables
%-------------------------------------------
%w
a1 = h*(a+b);
a2 = ((r1*(1-cos(theta)))*(a+b))-((theta*r1*r1)-(.5*a*r1*cos(theta)));
a3 = ((theta*r2*r2)-(.5*b*r2*cos(theta)));
w_circles = (a1+a2+a3)/(a+b);
%-------------------------------------------
% h_bar
b_rectangle = b*r2;
b_slice = theta*r2*r2;
b_triangles = .5*b*r2*cos(theta);
a_slice = theta*r1*r1;
a_triangles = .5*a*r1*cos(theta);
a_rectangle = a*((r1*cos(theta))-(r2*(1-cos(theta))));
area_a = a_slice + a_triangles - a_rectangle;
area_b = b_rectangle - b_slice - b_triangles;
area_tot = area_a + area_b;
h_bar_circles = area_tot/(a+b);
%-------------------------------------------
eqns = [a==2*r1*sin(theta), b==2*r2*sin(theta),w_rectangle==w_circles,h_bar_rectangle==h_bar_circles];
vars = [r1 r2 h theta];
sol = solve(eqns,vars)
%cross check
subs(eqns, sol)
vpa(ans)
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Number Theory finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!