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)
Warning: Unable to find explicit solution. For options, see help.
r1 = Empty sym: 0-by-1 r2 = Empty sym: 0-by-1 theta = Empty sym: 0-by-1 h = Empty sym: 0-by-1

采纳的回答

Walter Roberson
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].'
eqns = 
vars = [r1 r2 theta h]
vars = 
Sol1 = solve(eqns(1:3),[r1 r2 h]);
eqns4 = subs(eqns(4:end), Sol1);
theta_sol = solve(eqns4, theta);
Warning: Unable to solve symbolically. Returning a numeric solution using vpasolve.
Sol = subs(Sol1, theta, theta_sol);
Sol.theta = theta_sol
Sol = struct with fields:
r1: 1.0168302148332778712297374853393 r2: 2.5420755370831946780743437133482 h: 6.0859276769813730178286811266925 theta: 1.3886013259302617605177017206272
%cross-check
vpa(subs(eqns, Sol))
ans = 
  3 个评论
Walter Roberson
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
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)
Warning: Unable to solve symbolically. Returning a numeric solution using vpasolve.
sol = struct with fields:
r1: 1.7243829566874918163903975668524 r2: 4.3109573917187295409759939171309 h: -1.9521821316142319464867823923284 theta: 2.5229651294811754224220225058909
%cross check
subs(eqns, sol)
ans = 
vpa(ans)
ans = 

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Number Theory 的更多信息

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by