How can I define an array of symbolic functions?
Show older comments
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
More Answers (1)
Victor
on 24 Apr 2020
1 vote
I think you can do it with for loop end "execute" command and cell array
3 Comments
Victor
on 24 Apr 2020
the easiest way:
syms f f1(p) f2(p) f3(p)
f=[f1(p) f2(p) f3(p)]
Walter Roberson
on 24 Apr 2020
Edited: Walter Roberson
on 25 Apr 2020
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.
Sara Linares
on 25 Apr 2020
Categories
Find more on Programming in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!