1
votes

I have a given matrix of size NxN (called M),a vector of Nx1 (called V), and two scalars (called a and b). I want to solve the linear system of equations for alpha. The dimensions are given as MATLAB reports them using the function size(-).

(M + a * b * 1) alpha == V

with 1 being a matrix of just 1s.

I figured the easiest way to do so would be

syms alpher; 
Mprep = (M + a * b * ones(length(M),length(M))); 
eqn = Mprep * alpher == V;
alpha = solve(eqn,alpher)

However, I get the error

Error using ==
Matrix dimensions must agree

I am not sure whether this error is due to the fact that Matlab does not know the proper size of alpher or if I am just plain wrong in my approach. The error occurs in the second last line according to matlab.

What is the best way to solve this in MATLAB?

1
as in this case M = N based on M being symmetric and thus ones(length(M),length(M)) having the same dimensions as M itself. Additionally the error occurs in the second last line so any presumed error in a previous line is rather unlikely. (this was a response to a now deleted comment)Sim

1 Answers

2
votes

If you want to solve for an array of symbolic variables, you actually have to create an array of symbolic variables. Currently MATLAB sees alpher as a scalar and this obviously has dimension issues when performing matrix multiplication since an N x N matrix times a scalar is not equal to an N x 1 array.

Rather than using the symbolic toolbox at all, I would recommend simply using MATLAB's built-in ability to solve a linear system of equations using the \ operator (mldivide)

alpha = Mprep \ V;

As a side note, please refrain from using length as the result is ambiguous as it simply returns the first non-singleton dimension. If you want the size of the ones to be equal to M just use ones(size(M)), alternately if you specifically want the number of rows or columns in M use the second input to size to specify the dimension that you'd like to query: nRows = size(M, 1) or nCols = size(M, 2)