explicit vs numerical solution for system of equations
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Walter Roberson
2024-2-8
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 个评论
Walter Roberson
2024-2-9
I broke the solution into two parts: first solving for r1, r2, and h, and then solving for theta.
Walter Roberson
2024-2-9
编辑:Walter Roberson
2024-2-9
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)
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Number Theory 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!