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

2 次查看(过去 30 天)
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

类别

Help CenterFile Exchange 中查找有关 General Applications 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by