What can I do if Matlab never stops calculating a system of equations?
8 次查看(过去 30 天)
显示 更早的评论
Hey guys,
I try to solve a system of equations with Matlab with solve. There are 29 Variables and 29 equations. The problem behind is finding affine transformation for mapping points . So I have 512 systems of equations, and maybe 5 of them are giving me trouble. There are three problems I am facing.
First: The programm is working for houres without giving me an output for some constellations of points, so neither "no solution" nor "solution". It is just calculating. Does it help if I reduce some equations which are trivial? Can it be, that my laptop is to weak?
Second: Sometimes it helpes if I cange the order of the variables it should solve for. So sometimes if I cange x and y which are my last two variables in the array, than the programm can finish the job. But sometimes it doesnt work.
Third. When I find solutions for some systems of equations all variables are real. However If I tell the programm "syms A11 ... C23 real" than the programm cant find the solution ecplicitly and gives a warning. Why is that? How can I solve that?
Is it an option to use x and y as parameters? In the end it would be ok if there is a familie of solutions that this family depends on x and or y.
Thanks to everybody who tries to help!
clear all
close all
clc
syms A11 A12 A13 A21 A22 A23 B11 B12 B13 B21 B22 B23 C11 C12 C13 C21 C22 C23
syms x y lambda my w z delta_xy delta_lambda1 delta_my1 delta_xywz1 delta_xywz2 positive
F = [A11 A12; A21 A22];
f = [A13; A23];
G = [B11 B12; B21 B22];
g = [B13; B23];
H = [C11 C12; C21 C22];
h = [C13; C23];
P1 = [0;0];
P2 = [1;0];
P3 = [x;y];
P4 = [0;1];
A = [0 lambda w 0;...
0 0 z my];
B = [1 x w lambda;...
0 y z 0];
C = [0 0 w x;...
1 my z y];
D = [1 2 3 4; 2 3 4 1; 3 4 1 2; 4 1 2 3;...
1 4 3 2; 2 1 4 3; 3 2 1 4; 4 3 2 1];
for i = 5
A1b=A(:,D(i,1));
A2b=A(:,D(i,2));
A3b=A(:,D(i,3));
A4b=A(:,D(i,4));
AnzahlLoesungen=0
for j = 3
B1b=B(:,D(j,1));
B2b=B(:,D(j,2));
B3b=B(:,D(j,3));
B4b=B(:,D(j,4));
for k = 4
C1b=C(:,D(k,1));
C2b=C(:,D(k,2));
C3b=C(:,D(k,3));
C4b=C(:,D(k,4));
p=D(i,:);
q=D(j,:);
r=D(k,:);
eqns = [cat(1,F*P1+f==A1b, F*P2+f==A2b, F*P3+f==A3b, F*P4+f==A4b, G*P1+g==B1b, G*P2+g==B2b, G*P3+g==B3b, G*P4+g==B4b, H*P1+h==C1b, H*P2+h==C2b, H*P3+h==C3b, H*P4+h==C4b, x+y==1+delta_xy, lambda==1-delta_lambda1, my==1-delta_my1, (w-1)*y+z*(1-x)== - delta_xywz1, w*(1-y)+(z-1)*x== - delta_xywz2)]
txt = ['Perm.F ist ' num2str(p) ' Perm.G ist ' num2str(q) ' Perm.H ist ' num2str(r)]
sol = solve(eqns,[A11 A12 A13 A21 A22 A23 B11 B12 B13 B21 B22 B23 C11 C12 C13 C21 C22 C23 lambda my w z delta_xy delta_lambda1 delta_my1 delta_xywz1 delta_xywz2 x y],'ReturnConditions',true);
A11Sol = sol.A11;
A12Sol = sol.A12;
A13Sol = sol.A13;
A21Sol = sol.A21;
A22Sol = sol.A22;
A23Sol = sol.A23;
B11Sol = sol.B11;
B12Sol = sol.B12;
B13Sol = sol.B13;
B21Sol = sol.B21;
B22Sol = sol.B22;
B23Sol = sol.B23;
C11Sol = sol.C11;
C12Sol = sol.C12;
C13Sol = sol.C13;
C21Sol = sol.C21;
C22Sol = sol.C22;
C23Sol = sol.C23;
lambdaSol = sol.lambda;
mySol = sol.my;
wSol = sol.w;
zSol = sol.z;
xSol = sol.x;
ySol = sol.y;
conditionsSol = sol.conditions
回答(1 个)
Torsten
2023-8-14
编辑:Torsten
2023-8-14
I'd suggest you don't use the symbolic toolbox for your problem, but to define an optimization problem:
min: any function you like (e.g. 0)
under
your equality and inequality constraints.
Try e.g. "fmincon" to solve.
The objective function is not important, but the solver will try to adjust your solution variables such that the constraints are satisfied.
Further, you should differ between box constraints (to be specified in lb and ub), linear constraints (to be specified in A, b, Aeq and beq) and nonlinear constraints (to be specified in function "nonlcon").
2 个评论
Torsten
2023-8-14
Will this optimization still be able to find families of solutions. Its possible that the result might be x=z1, y=1/z1, ... for z1>1 ore something like that.
No. Numerical solvers return at most one solution that may depend on the initial guess given.
What are lb, ub and so on?
It's explained in the documentation of "fmincon".
However, what is the reason why solve isnt able to do that? For 508 of 512 cases it works completely fine. It is just so annyoing because I dont understand why. Should I try reducing some equations? And do you know why the problems i described occur, when I name A11, .. C23 as real? why does that change something?
I cannot answer this - and maybe only the developpers will be able to for some questions you ask.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!