0
votes
  1. How can I ask a user to input a function ( of say : t) and store it as a symbolic function?
  2. And after that I want to store all the symbolic functions in an array: Let's say my first function is : f1 ( and I have gotten information of f1 by user in step 1 as f1=t) second one is : f2 (and I have gotten information of f2 by user in step 1 as f2=t^2) Now I want to make an array A= [ f1, f2]

such that I when I call A(2) it gives me f2 which in turn gives for e.g. 4 when I write f2(2)

I already know how to get input as an inline function and by using this program (http://www.mathworks.com/matlabcentral/fileexchange/33025-convert-inline-to-symbolic/content/inline2sym.m) I am converting it to sym. But I want an easier way if it exists.

1
Don't use inline functions at all –they're inefficient and as the documentation states, deprecated. You don't need anything more than sym or symfun and basic Matlab arrays or cells to do what you describe. - horchler
An array of type symfun is not possible, you have to use a cell array. - Daniel
@Daniel: Yes. That is why I mentioned the use of cell arrays in my comment. symfun objects are like function handles. - horchler

1 Answers

0
votes

From your question, it seems to me that what you want/need is not a true symbolic function, but rather a function handle. If this is the case I'd go for something like the code below. A apologize for "ugly things" like while 1,... but it keeps the example short:

A = {};
while 1
    str = input('Enter a function of t (simply press return to exit):','s');
    if isempty(str), break; end
    A{end+1} = str2func(['@(t) ' str]);
end

When I run it i can input some functions of "t":

Enter a function of t (simply press return to exit):t
Enter a function of t (simply press return to exit):t.^2
Enter a function of t (simply press return to exit):sin(t)
Enter a function of t (simply press return to exit):

The cell array A contains:

A = 
    @(t)t    @(t)t.^2    @(t)sin(t)

And you can call functions in the following way:

>>A{3}(pi/2)

ans =

     1

I hope I understood your question correctly and that this helps.