How can I find a unique solution within tolerance using solve?
6 ビュー (過去 30 日間)
古いコメントを表示
Hello,
So I am using solve to find values for 3 variables while having 3 equations. The problem is that my equations dont seem to have unique solution. How can I setup tolerance in a way that the solution only exits within 20k to 2M? For example for values in 700k,1.5M and 2M the right hand side is approximately equal to the left hand side.
syms Rx Risop Rison
V1=252.79315;
V2=287.20685;
V3=496.51688;
V4=43.483124;
V5=38.272892;
V6=501.72711;
R=100000000;
R1=68220;
R2=68220;
Vx1=17.206846;
Vx2=226.51688;
Vx3=231.72711;
equ1=(V1/Risop)+(V1/R)==(Vx1/Rx)+(V2/Rison)+(V2/R);
equ2= (V3/Risop)+(V3/R)==(V4/Rison)+(V4/R1)+(V4/R)-(Vx2/Rx);
equ3= (V5/Risop)+(V5/R)+(V5/R2)==(V6/Rison)+(Vx3/Rx)+(V6/R);
[Risop,Rison,Rx]=solve([equ1,equ2,equ3],[Risop,Rison,Rx]);
vpa(Risop)
vpa(Rison)
vpa(Rx)
Risop=1500000;
Rison=2000000;
Rx=700000;
LHS=(V1/Risop)+(V1/R);
RHS=(Vx1/Rx)+(V2/Rison)+(V2/R);
vpa(LHS)
vpa(RHS)
LHS=(V3/Risop)+(V3/R);
RHS=(V4/Rison)+(V4/R1)+(V4/R)-(Vx2/Rx);
vpa(LHS)
vpa(RHS)
LHS=(V5/Risop)+(V5/R)+(V5/R2);
RHS=(V6/Rison)+(Vx3/Rx)+(V6/R);
vpa(LHS)
vpa(RHS)
2 件のコメント
Alex Sha
2024 年 7 月 28 日
Hi, how about the result below:
risop: -378351.252158428
rx: 124254.506405549
rison: -355908.211178662
回答 (2 件)
Torsten
2024 年 7 月 28 日
編集済み: Torsten
2024 年 7 月 28 日
As long as rank(A) equals 3 in the below code, your system of linear equations has a unique solution.
syms Rx Risop Rison
V1=252.79315;
V2=287.20685;
V3=496.51688;
V4=43.483124;
V5=38.272892;
V6=501.72711;
R=100000000;
R1=68220;
R2=68220;
Vx1=17.206846;
Vx2=226.51688;
Vx3=231.72711;
equ1=(V1*Risop)+(V1/R)==(Vx1*Rx)+(V2*Rison)+(V2/R);
equ2= (V3*Risop)+(V3/R)==(V4*Rison)+(V4/R1)+(V4/R)-(Vx2*Rx);
equ3= (V5*Risop)+(V5/R)+(V5/R2)==(V6*Rison)+(Vx3*Rx)+(V6/R);
[A,b] = equationsToMatrix([equ1,equ2,equ3],[Risop,Rison,Rx]);
rank(A)
sol = solve([equ1,equ2,equ3],[Risop,Rison,Rx])
Risop = 1/sol.Risop;
Rison = 1/sol.Rison;
Rx = 1/sol.Rx;
format long
double([Risop;Rison;Rx])
2 件のコメント
Torsten
2024 年 8 月 29 日
編集済み: Torsten
2024 年 8 月 29 日
If the rank is 3, there only exists one solution for your linear system of equations. If this unique solution has negative components (as is the case for your system) and you restrict the solution you want to be positive, you get an empty result.
If you want the "nearest" positive solution, use "lsqnonneg".
syms Rx Risop Rison
V1=252.79315;
V2=287.20685;
V3=496.51688;
V4=43.483124;
V5=38.272892;
V6=501.72711;
R=100000000;
R1=68220;
R2=68220;
Vx1=17.206846;
Vx2=226.51688;
Vx3=231.72711;
equ1=(V1*Risop)+(V1/R)==(Vx1*Rx)+(V2*Rison)+(V2/R);
equ2= (V3*Risop)+(V3/R)==(V4*Rison)+(V4/R1)+(V4/R)-(Vx2*Rx);
equ3= (V5*Risop)+(V5/R)+(V5/R2)==(V6*Rison)+(Vx3*Rx)+(V6/R);
[A,b] = equationsToMatrix([equ1,equ2,equ3],[Risop,Rison,Rx]);
sol1 = lsqnonneg(double(A),double(b))
Risop = 1/sol1(1);
Rison = 1/sol1(2);
Rx = 1/sol1(3);
format long
double([Risop;Rison;Rx])
Himanshu
2024 年 7 月 19 日
編集済み: Himanshu
2024 年 7 月 19 日
Hey Taiji,
To address the issue of non-unique solutions and ensure that the solutions fall within a specified range (20k to 2M), you can use MATLAB's numerical solver 'fsolve' from the Optimization Toolbox. The 'fsolve' function can be used to solve systems of nonlinear equations numerically and can handle bounds on the variables. By defining an initial guess and setting the lower and upper bounds for the variables, you can restrict the solutions to your desired range. Additionally, you can set tolerances for the function values and variables to ensure the solver's precision.
The code involves defining the equations as a function handle and specifying the initial guess for the variables. Then, we set the options for 'fsolve', including the tolerances and bounds. The 'fsolve' function is used to solve the system of equations, and the solutions are displayed along with a verification step to ensure the accuracy of the results. This approach allows you to handle the non-unique solutions and obtain results within the desired range.
You may go through the following documentaion link to know more about 'fsolve'.
% Define the variables and constants
V1 = 252.79315;
V2 = 287.20685;
V3 = 496.51688;
V4 = 43.483124;
V5 = 38.272892;
V6 = 501.72711;
R = 100000000;
R1 = 68220;
R2 = 68220;
Vx1 = 17.206846;
Vx2 = 226.51688;
Vx3 = 231.72711;
% Define the equations as functions
fun = @(x) [
(V1/x(1)) + (V1/R) - (Vx1/x(3)) - (V2/x(2)) - (V2/R);
(V3/x(1)) + (V3/R) - (V4/x(2)) - (V4/R1) - (V4/R) + (Vx2/x(3));
(V5/x(1)) + (V5/R) + (V5/R2) - (V6/x(2)) - (Vx3/x(3)) - (V6/R)
];
% Initial guess for the variables
x0 = [1.5e6, 2e6, 700e3];
% Set options for fsolve, including bounds
options = optimoptions('fsolve', 'Display', 'iter', 'TolFun', 1e-6, 'TolX', 1e-6);
lb = [20e3, 20e3, 20e3]; % Lower bounds
ub = [2e6, 2e6, 2e6]; % Upper bounds
% Solve the system of equations
[x, fval, exitflag] = fsolve(fun, x0, options);
% Display the results
disp('Solutions:');
disp(['Risop = ', num2str(x(1))]);
disp(['Rison = ', num2str(x(2))]);
disp(['Rx = ', num2str(x(3))]);
% Verify the solutions
LHS1 = (V1/x(1)) + (V1/R);
RHS1 = (Vx1/x(3)) + (V2/x(2)) + (V2/R);
disp(['Equation 1: LHS = ', num2str(LHS1), ', RHS = ', num2str(RHS1)]);
LHS2 = (V3/x(1)) + (V3/R);
RHS2 = (V4/x(2)) + (V4/R1) + (V4/R) - (Vx2/x(3));
disp(['Equation 2: LHS = ', num2str(LHS2), ', RHS = ', num2str(RHS2)]);
LHS3 = (V5/x(1)) + (V5/R) + (V5/R2);
RHS3 = (V6/x(2)) + (Vx3/x(3)) + (V6/R);
disp(['Equation 3: LHS = ', num2str(LHS3), ', RHS = ', num2str(RHS3)]);
1 件のコメント
John D'Errico
2024 年 8 月 29 日
I'm sorry, but I think you don't understand how these tools work.
You defined the variables lb and ub. Good for you. But then you NEVER used them!
lb = [20e3, 20e3, 20e3]; % Lower bounds
ub = [2e6, 2e6, 2e6]; % Upper bounds
% Solve the system of equations
[x, fval, exitflag] = fsolve(fun, x0, options);
Just because you define variables called lb, and ub, does not mean that MATLAB will know what your intent is with those variables.
What is worse, is that fsolve does not offer bound constraints!
Instead, you could have suggested using lsqnonlin. it DOES allow bound constraints on the variables.
参考
カテゴリ
Help Center および File Exchange で Linear Programming and Mixed-Integer Linear Programming についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!