How can I define an array of symbolic functions?

I want to make an array a1(t) a2(t)... an(t).
syms a(t)
creates a symbolic parameter 't' and 'a' which is an unknown function of 't'. I want an array of just this. How do I do that? I want to use this array to solve a system of ODEs using dsolve.

 Accepted Answer

In MATLAB it not possible to create an array of symbolic functions. If you have even one symbolic function then MATLAB will build a single function that returns an array.
I was looking at this the other day and noticed that diff(a1, x) would produce a function as output but that diff(a1(x), x) would produce an expression. I was wondering whether that made a difference for dsolve purposes and made a mental note to investigate but I did not get around to it yet.

5 Comments

I think it should be possible because here https://es.mathworks.com/help/symbolic/syms.html#mw_62a9a35d-f66d-4c40-82d5-76241b2922de I have read the following:
syms f(var1,...,varN) [n1 ... nM] creates an n1-by-...-by-nM symbolic array with automatically generated symbolic functions as its elements. This syntax also generates the symbolic variables var1,...,varN that represent the input arguments of f. For example, syms f(x) [1 2] creates the symbolic array f(x) = [f1(x) f2(x)], the symbolic functions f1(x) and f2(x), and the symbolic variable x in the MATLAB workspace. For multidimensional arrays, these elements have the prefix f followed by the element’s index using _ as a delimiter, such as f1_3_2.
and that's exactly what I want, but when I introduce the code, MATLABS answers:
Error using syms (line 228)
Invalid variable name.
>> syms f(x) [1 2]
>> f
f(x) =
[ f1(x), f2(x)]
>> f(1)
ans =
[ f1(1), f2(1)]
Therefore what it created was a single function that has an array result. f(1) is interpreted as applying argument 1 to each of the functions inside the array, instead of as indexing the first location in the array.
I just checked, and I find that dsolve() is happy to receive what is technically a single function that has an array of function expressions, as well as being happy to receive an array of expressions.
syms f1(x) f2(x)
df1 = diff(f1,x); df2 = diff(f2,x);
eqn1 = [df1 == 1 + f2, df2 == 1 + f1]; %this is a function that returns an array
sol1 = dsolve(eqn1)
sol1 = struct with fields:
f2: exp(x)*(C1 - exp(-x)) + C2*exp(-x) f1: exp(x)*(C1 - exp(-x)) - C2*exp(-x)
eqn2 = [df1(x) == 1 + f2(x), df2(x) == 1 + f1(x)]; %this is an array of expressions
sol2 = dsolve(eqn2)
sol2 = struct with fields:
f2: exp(x)*(C1 - exp(-x)) + C2*exp(-x) f1: exp(x)*(C1 - exp(-x)) - C2*exp(-x)
The solutions are the same, so dsolve() is happy with either version.
It does make a difference if you want to extract one component out of eqn1 or eqn2. To extract one component out of eqn2 you can just index. To extract one component out of eqn1 you have to do magic with feval(symengine)
i have a function of x
but i have 4 functions of x
and i want to store them like
a = f(x), b = f(x) , c = f(x) ,d =f(x)
these are the functions for 1 st 4 natural frequencies .
and i want to store and use it in for loop so , i ant to store like
delta = [a b c d ];
but i am not able to do that becuse arrays dont alow to store the expressions .
do you have any idea regarding this?
You cannot do that with [] for reasons I explained before. Use a cell array instead.

Sign in to comment.

More Answers (1)

I think you can do it with for loop end "execute" command and cell array

3 Comments

the easiest way:
syms f f1(p) f2(p) f3(p)
f=[f1(p) f2(p) f3(p)]
No, that makes f into a vector of expressions, but the user wanted an array of symbolic functions.
Suppose that you had
syms f1(p) f2(p)
f1(p) = sin(p);
f2(p) = cos(p);
f = [f1, f2];
Then if this was an array of functions, then f(1) would be the function f1, symfun(sin(p),p) . But it isn't:
>> f(1)
ans =
[ sin(1), cos(1)]
That did not index, that took the 1 as the input to the functions.
Your suggestion was
syms f1(p) f2(p)
f1(p) = sin(p);
f2(p) = cos(p);
f = [f1(p), f2(p)];
and then
>> f(1)
ans =
sin(p)
At first that looks good, but if you take a closer look, this is the expression sin(p), not a function. The function would output like
>> f1
f1(p) =
sin(p)
Notice that the function form includes the argument p in the = line, but when we used your suggestion it did not say ans(p) = because f(1) is an expression rather than a function.
Thank you very much to both of you! You were really helpful and I really appreciate your time!!

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products

Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!