1

Is there an easy way to check if a vector valued symbolic function is linear? If so, is there an easy way to represent this expression in the form A*x, where A is a symbolic matrix and x is the argument (i.e. is there a way to "extract" A given x and A*x)?

syms x1 x2 a b c;
fx1 = [a*(x1+x2); b*x1+c*x1];
fx2 = [a*x1/log(x2); x2^2];
A=checklinearity(fx1, [x1 x2]') % should return [a, a; b+c, 0]
A=checklinearity(fx2, [x1 x2]') % should return false

Answer: There is an easy way to solve the problem using a built-in MATLAB function equationsToMatrix.

  • Have yo tried to do the usual tests: `f(x+x) = f(x)+f(y)` and `f(ax)=a*f(x)`? – patrik Aug 26 '14 at 08:21
  • 2
    Well, assuming that symbolic toolbox is used it should not be a problem. Just do exactly the same way af if you have tested it analytically. I mean why is it not possible to do: `f(x) = x; q = f(ax)-a*f(x); if q~=0, fprintf('not equal'); else,fprintf('equal'); end`. Which is of course pseudo code. – patrik Aug 26 '14 at 12:29

2 Answers2

1

Your question is effectively asking if equations are polynomials that are linear in the variable in question. MuPAD has a whole suite of functions that you can use for polynomial algebra. For example, you might use the degree function. Here's an example using the map function to vectorize this over a list of functions:

function p = orderOfVars(f,x)
for i = numel(x):-1:1
    p(:,i) = evalin(symengine,['map(' char(f(:)) ',f->degree(f,' char(x(i)) '))']);
end

Then

syms x1 x2 a b c;
fx1 = [a*(x1+x2); b*x1+c*x1];
fx2 = [a*x1/log(x2); x2^2];

y1 = orderOfVars(fx1,[x1 x2])
y2 = orderOfVars(fx2,[x1 x2])

returns

y1 =

[ 1, 1]
[ 1, 0]


y2 =

[ 1, 0]
[ 0, 2]

From here it's simply a matter of testing the values:

all(y1(:)<=1)
all(y2(:)<=1)

which return true and false, respectively.

horchler
  • 18,384
  • 4
  • 37
  • 73
  • Yes, but there are easier ways - see comments and edits above. I feel like I should not have asked this question... –  Aug 26 '14 at 13:04
  • @user1391279: You're welcome to answer (and even accept) your own question. Test out this `equationsToMatrix` to see how well it works. Is it fast? What happens if you give it a nonlinear equation in one or more variables – can it tell? This function is also likely a new addition to Matlab, so users of older versions may not have access to it thus making your general question still valuable. – horchler Aug 26 '14 at 13:10
  • I have already tested the function to some degree - it works well in the aforementioned cases. If the equation is nonlinear it throws an exception with the identifier symbolic:sym:equationsToMatrix:NonlinearSystem. –  Aug 26 '14 at 13:24
0

I decided to post and accept my own answer.

Generally, there are several approaches that lead to the correct solution:

1). The easiest solution is to use the built-in MATLAB function equationsToMatrix(). The function tests for linearity and if the expression is nonlinear, then the function throws an exception with identifier symbolic:sym:equationsToMatrix:NonlinearSystem. If the system is linear, then the function returns the matrix of interest.

2). Solution proposed by patrik was to use the definition of the linearity: f(ax)-a*f(x). This only tests for linearity, but once it is confirmed that the system is linear it is not difficult to infer the matrix.

3). Solution proposed by horchler was to use polynomial algebra.