Search code examples
matlabvectorsymbolic-math

How to symbolize a vector with its elements function of time in matlab


I am trying to solve some differential equations in this form

dU/dt=A*U, where U is a vector of N elements and N is an input from the user

So U=[u1(t);u2(t)...uN(t)], A is NxN matrix

I want to have a vector (U) its elements u1,u2..uN are function of time

I can define U as follows

U=sym('u',[N 1]);

This is the output:

u1
u2
.
.
uN

And if I write this

eqn= diff(U)==A*U;
U=dsolve(eqn);

This is the error I got

{Error using symengine (line 58)
Could not extract differential variables to solve for. Use 'solve' or
 'vpasolve' to compute the solutions of non-differential equations.

 Error in mupadengine/feval (line 155)
symengine('error',S(8:find(S=='[',1)-2));

 Error in dsolve>mupadDsolve (line 325)
 T = feval(symengine,'symobj::dsolve',sys,x,options);

 Error in dsolve (line 186)
 sol = mupadDsolve(args, options);
} 

He treats the elements of U vector as just a variable not function of time

What do I do?


Solution

  • You can use mat2str and regular expressions to create the string '[u1(t);u2(t); ... ]':

    str= mat2str((1:N).');     % create the string '[1;2;...;N]'
    % replace the digit sequences with 'u<digit sequence>(t)'
    str= regexprep(str,'(\d)+','u$&(t)');  
    u= sym(str);