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!