linsolve A*X=B where B is a vector?

조회 수: 3 (최근 30일)
fsgeek
fsgeek 2017년 2월 7일
댓글: antlhem 2018년 8월 29일
Hi guys,
I've searched the documentation and the forums about this but haven't found an answer.
I have the following system of linear equations:
syms Exx Eyy Exy
eqn1 = 0.5*(Exx + Eyy) + 0.5*(Exx - Eyy)*cosd(2.0*theta1) + (0.5*Exy)*sind(2.0*theta1) == B1;
eqn2 = 0.5*(Exx + Eyy) + 0.5*(Exx - Eyy)*cosd(2.0*theta2) + (0.5*Exy)*sind(2.0*theta2) == B2;
eqn3 = 0.5*(Exx + Eyy) + 0.5*(Exx - Eyy)*cosd(2.0*theta3) + (0.5*Exy)*sind(2.0*theta3) == B3;
theta1, theta2 and theta3 are constants. I set up the system into a matrix and solve for Exx, Eyy and Exy:
[A, B] = equationsToMatrix([eqn1, eqn2, eqn3], [Exx ,Eyy, Exy]);
X = linsolve(A, B);
This works fine if B1, B2 and B3 are 1x1 constants. However, for my application B can be a 1xN vector. Using B in this context does not produce a solution.
Currently I have the above code under a FOR loop where I solve the system for each value of B1,2,3, and assign the values of Exx, Eyy and Exy into a pre-allocated buffer. This can be very slow, so I'm wondering if there's a way to successfully vectorize linsolve such that it can solve the system for N Exx,yy,xy values based on N B1,2,3 values?
Best regards,
Louis

채택된 답변

Torsten
Torsten 2017년 2월 7일
syms Exx Eyy Exy
eqn1 = 0.5*(Exx + Eyy) + 0.5*(Exx - Eyy)*cosd(2.0*theta1) + (0.5*Exy)*sind(2.0*theta1) == 0;
eqn2 = 0.5*(Exx + Eyy) + 0.5*(Exx - Eyy)*cosd(2.0*theta2) + (0.5*Exy)*sind(2.0*theta2) == 0;
eqn3 = 0.5*(Exx + Eyy) + 0.5*(Exx - Eyy)*cosd(2.0*theta3) + (0.5*Exy)*sind(2.0*theta3) == 0;
A = equationsToMatrix([eqn1, eqn2, eqn3], [Exx ,Eyy, Exy]);
B = [B11 B12 B13 B14 ... B1N;
B21 B22 B23 B24 ... B2N;
B31 B32 B33 B34 ... B3N];
X = linsolve(A,B);
Best wishes
Torsten.
  댓글 수: 2
fsgeek
fsgeek 2017년 2월 8일
Hi Torsten,
This is also a good solution. I get the same results and it's much faster. Thanks!
antlhem
antlhem 2018년 8월 29일
guys is there any way to solve the linear equations where the variables are present in both A and B arrays? please take a look to my question with more detail: https://uk.mathworks.com/matlabcentral/answers/416689-linear-system-of-equations

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2017년 2월 7일
syms B1 B2 B3
syms Exx Eyy Exy
syms theta1 theta2 theta3
sind = @(D) sin(D*pi/180)
cosd = @(D) cos(D*pi/180);
eqn1 = 0.5*(Exx + Eyy) + 0.5*(Exx - Eyy)*cosd(2.0*theta1) + (0.5*Exy)*sind(2.0*theta1) == B1;
eqn2 = 0.5*(Exx + Eyy) + 0.5*(Exx - Eyy)*cosd(2.0*theta2) + (0.5*Exy)*sind(2.0*theta2) == B2;
eqn3 = 0.5*(Exx + Eyy) + 0.5*(Exx - Eyy)*cosd(2.0*theta3) + (0.5*Exy)*sind(2.0*theta3) == B3;
sol = solve([eqn1, eqn2, eqn3], [Exx, Eyy, Exy]);
X = simplify([sol.Exx, sol.Eyy, sol.Exy], 'step', 20)
This gives a general structure. For any one group of theta1, theta2, theta3, subs() those values in.
Then, if you know the entire group of B values, you can subs() those in to get a simultaneous answer.
If not, if you are running for a while with the same theta1, theta2, theta3, then use matlabFunction() to generate a vectorized numeric function that can be used on batches. You might want to pay attention to the 'vars' option of matlabFunction() as that can allow you to bundle several inputs up into a single function parameter. If you happen to be using this for optimization purposes, then a trick I have found is to provide a column vector of names, such as
matlabFunction(F, 'vars', {[B1; B2; B3]})
as that arranges the variables in the order most suitable for use as objective functions for minimizers such as fmincon
  댓글 수: 1
fsgeek
fsgeek 2017년 2월 8일
Hi Walter,
Nice solution, thanks!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by