0

How to create symbolic function for matrices? For example, I want to have matrix multiplication

A = sym('A',[5,1])
B = sym('B',[1,5])
f = symfun(A * B,[A, B])

But I have an error because A and B have different dimensions, and I can't use square brackets.

0

1 Answer 1

2

symfun only supports scalar arguments, thus you can only define a symbolic function with 10 scalar arguments:

f = symfun(A * B,[A(:); B(:)])

I recommend to work with the symbolic expression instead and use subs to evaluate:

f=A*B

Further you can also use a function handle:

f=@(a,b)(a*b);
f(A,B)
2
  • 1
    A = sym('A',[5,1]) B = sym('B',[1,5]) f(a,b)=a*b; f(A,B) Error using symengine (line 58) Array sizes must match. Nov 26, 2015 at 16:12
  • Sorry, pasted the wrong code here. f(a,b)=a*b is just another way to define a symbolic function which does not succeed. Answer now contains the correct code.
    – Daniel
    Nov 26, 2015 at 16:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.