# How to make a symbolic matrix?

12 views (last 30 days)
Evgheny on 11 Apr 2011
I need to make a symbolic matrix:
1 t1 t1^2 sin(t1) cos(t1)
1 t2 t2^2 sin(t2) cos(t2)
....
1 tm tm^2 sin(tm) cos(tm)
I could make an array of array but not a whole matrix:
# phi.m file
function[result] = phi(t)
% declaring omega
omega = 4;
result = [1 t t*t sin(omega*t) cos(omega*t)];
# main.m file
n=8;
m=5;
t = sym('t',[n,1]);
F = diag(sym('t',[1 m]));
for i=1:n
F(i,:) = phi(t(i));
end
F
and this returns:
[ 1, t1, t1^2, sin(4*t1), cos(4*t1)]
[ 1, t2, t2^2, sin(4*t2), cos(4*t2)]
[ 1, t3, t3^2, sin(4*t3), cos(4*t3)]
[ 1, t4, t4^2, sin(4*t4), cos(4*t4)]
[ 1, t5, t5^2, sin(4*t5), cos(4*t5)]
[ 1, t6, t6^2, sin(4*t6), cos(4*t6)]
[ 1, t7, t7^2, sin(4*t7), cos(4*t7)]
[ 1, t8, t8^2, sin(4*t8), cos(4*t8)]
But it is array of arrays (but I need matrix). How to do this?

Walter Roberson on 11 Apr 2011
What data class does it think that F is?
cell2mat might work.

Andrei Bobrov on 11 Apr 2011
variant:
>>n=8;t=[];for j=1:n, t = [t;sym(['t' num2str(j)])]; end
phi=@(k,omega)[ones(length(k(:)),1) k k.^2 sin(omega*k) cos(omega*k)];
phi(t,4)
ans*ones(size(ans,2),1)
ans =
[ 1, t1, t1^2, sin(4*t1), cos(4*t1)]
[ 1, t2, t2^2, sin(4*t2), cos(4*t2)]
[ 1, t3, t3^2, sin(4*t3), cos(4*t3)]
[ 1, t4, t4^2, sin(4*t4), cos(4*t4)]
[ 1, t5, t5^2, sin(4*t5), cos(4*t5)]
[ 1, t6, t6^2, sin(4*t6), cos(4*t6)]
[ 1, t7, t7^2, sin(4*t7), cos(4*t7)]
[ 1, t8, t8^2, sin(4*t8), cos(4*t8)]
ans =
t1 + cos(4*t1) + sin(4*t1) + t1^2 + 1
t2 + cos(4*t2) + sin(4*t2) + t2^2 + 1
t3 + cos(4*t3) + sin(4*t3) + t3^2 + 1
t4 + cos(4*t4) + sin(4*t4) + t4^2 + 1
t5 + cos(4*t5) + sin(4*t5) + t5^2 + 1
t6 + cos(4*t6) + sin(4*t6) + t6^2 + 1
t7 + cos(4*t7) + sin(4*t7) + t7^2 + 1
t8 + cos(4*t8) + sin(4*t8) + t8^2 + 1

Evgheny on 11 Apr 2011
I'm sorry but in such a way - it calculates too (like a matrix).
Walter Roberson on 11 Apr 2011
The interface between MATLAB and MuPad often converts matrix operations in to element-wise operations. To have a MATLAB-level matrix treated as an algebraic matrix in MuPad, you need to be more careful about how you code the problem.
Unfortunately I do not have the symbolic toolbox myself, so I cannot experiment with how you can best do this coding.
What *might* work for matrix multiplication, A * B, with A and B already defined as matrices, is to code
subs('A * B')
instead of using A * B . But I'm not at all certain of this: without the toolbox to play with, it is more difficult to tell which parts of the documentation apply to which circumstances.