I'm trying to use LU method to solve Ax=B, and when I do start doing so, using:
(P, L, U := LUDecomposition(A))
to create my three different matrices (P, L, U) I get the error
Error, cannot split rhs for multiple assignment
which doesn't make sense since LUDecomposition creates 3 matrices, which correspond to my P, L, U.
If you do either of the following then the call will return an expression sequence of containing three Matrices, and it will work for input A a Matrix.
with(LinearAlgebra):
P, L, U := LUDecomposition(A);
Or,
P, L, U := LinearAlgebra:-LUDecomposition(A);
But if you don't load the LinearAlgebra package and you also don't use the long-form name of that package's export then you're simply making a function call to an undefined name.
And, if you are in fact accidentally simply calling the undefined name LUDecomposition (because you have not used one of the mentioned methods for calling the package export of that name), then the result will be an unevaluated function call. And you cannot assign that single thing to three names on the left-hand-side of an assignment statement.
restart;
A := Matrix([[1,2],[3,5]]):
# Look, this next one does no computation.
# The return value is a single thing, an unevaluated
# function call.
LUDecomposition(A);
/[1 2]\
LUDecomposition|[ ]|
\[3 5]/
P, L, U := LUDecomposition(A);
Error, cannot split rhs for multiple assignment
But this would return the actual result,
LinearAlgebra:-LUDecomposition(A);
[1 0] [1 0] [1 2]
[ ], [ ], [ ]
[0 1] [3 1] [0 -1]
And so those three Matrices could be assigned to three names.
P, L, U := LinearAlgebra:-LUDecomposition(A);
[1 0] [1 0] [1 2]
P, L, U := [ ], [ ], [ ]
[0 1] [3 1] [0 -1]
Another way to do it is to first load the whole package, so that you can utilize the short-form command name,
with(LinearAlgebra):
LUDecomposition(A);
[1 0] [1 0] [1 2]
[ ], [ ], [ ]
[0 1] [3 1] [0 -1]
P, L, U := LUDecomposition(A);
[1 0] [1 0] [1 2]
P, L, U := [ ], [ ], [ ]
[0 1] [3 1] [0 -1]
Related
Why won't the following run?
if (map [ [a b c] -> a + b = c ] [1] [2] [3]) [show 100]
The following produces 'true' as an output:
show (map [ [a b c] -> a + b = c ] [1] [2] [3])
so I expected the first statement above to be the same as:
if true [show 100]
(P.S. in my full version the lists are longer but are collapsed into a single true/false using reduce.)
Thanks.
To elaborate on ThomasC's comment, map always produces a list, even if it only has one element. So
(map [ [a b c] -> a + b = c ] [1] [2] [3])
does produce [true]. Thus
(map [ [a b c] -> a + b = c ] [1 2] [2 3] [3 5])
will produce [true true]. reduce is helpful here,
reduce AND (map [ [a b c] -> a + b = c ] [1 2] [2 3] [3 5])
will produce a simple true by "anding" all the elements of the map output, and
reduce AND (map [ [a b c] -> a + b = c ] [1 2] [2 3] [3 6])
will produce a simple false.
I have created a function containing symbolic expressions. The expressions make use of a symbolic matrix. I want to solve the function passing the numeric matrix which replaces the symbolic matrix and provides a numeric answer. I can't seem to understand how to pass the numeric matrix.
function example
R= sym('R',[3 3])
r_b= R([1 2], [3])
R_bar= R([1 2], [1 2])
R_til=R([2 3],[2 3])
syms alpha_bar beta_bar
a_bar= [1;alpha_bar]
b_bar= [beta_bar;1]
P= sym(R_bar*a_bar)
Q= sym(R_til*b_bar)
syms E_a E_b
u_bar= [1; 0]
v_bar= [0;1]
W = sym(E_a*u_bar)
X= sym(E_b*v_bar)
C= sym(P==W)
D= sym(Q==X)
[alpha_bar_, E_a_] = solve(P==W,[alpha_bar,E_a])
[beta_bar_, E_b_] = solve(Q==X,[beta_bar,E_b])
a_bar= [1;alpha_bar_]
b_bar=[beta_bar_;1]
delta= (a_bar)'*r_b
gamma_b = delta/E_b_
gamma_a= delta/E_a_
a= [a_bar;0]-(gamma_b*[0;b_bar])
b= [0;b_bar]-gamma_a*[a_bar;0]
end
My R is R = [1 1 0; 1 3 2; 0 2 3].
I have a symbolic vector y which I know contains linear expressions of the variables contained in a symbolic vector theta. Is there a way to compute the symbolic expression of A, where y = A * theta? I tried y*pinv(theta) but it doesn't seem to work.
Example:
syms a b real
theta = [a;b];
y = [2*a;2*b];
y*pinv(theta) gives
ans = [ (2*a^2)/(a^2 + b^2), (2*a*b)/(a^2 + b^2)]
[ (2*a*b)/(a^2 + b^2), (2*b^2)/(a^2 + b^2)]
and
y/theta gives
ans = [ 2, 0]
[ (2*b)/a, 0]
along with a warning that the solution is not unique.
I want to eradicate the symbolic variables from my result i.e. I want
ans = [ 2, 0]
[ 0, 2]
The equationsToMatrix function seems to be doing the job!
Try equationsToMatrix(y,theta)
I have A, b, f matrices as follows :
A = [ 2 1
1 2 ]
B = [ 4; 3 ]
F = [ 1; 1 ]
LB = [ 0; 0 ]
[X, fval, exitflag, output, lambda] = linprog(F, A, B, [], [], LB)
After that, the solution provided by the MATLAB is surprising. It says the value of fval is 1.2169e-013. This is not the solution. Can you help me by identifying the fault?
I would like to verify (in a function), that the size of a matrix A is a x b x c, where potentially c=1.
So far I had:
if size(A) ~= [ a b c ]
error('bad size');
end
However, if c=1 (and A has the desired size):
size(A) = [ a b ]
since size removes trailing singleton dimensions. Therefore, the comparison will result in an error, as the vectors ([a b] and [a b c]) do not have the same size.
Is there anything nicer than
if c == 1 && size(A) ~= [ a b ] || c ~= 1 && size(A) ~= [ a b c ]
error('bad size');
end
?
validateattributes does the job:
validateattributes(A, {'numeric'}, {'size', [ a b c ]});
will not fail if c=1 and A has the correct dimensions.
The output of size(A) varies with the manner in which it is invoked. For your case you could simply use:
[a1,b1,c1] = size(A);
if(any([a1,b1,c1]~=[ a b c ]))
error('bad size');
end
An alternate solution is this:
if (size(A) ~= [ a b c ](1:ndims(A)))
error('bad size');
end
Which will work so long as A does not have more than 3 dimensions. If A does have more than 3 dimensions, then you're going to need to have a condition beforehand:
if (ndims(A)>3 || size(A)~=[a b c](1:ndims(A)))
error('bad size');
end
Obviously, if the dimensions of A beyond the third one are singleton dimensions, this will consider it a bad size... but that may be acceptable.