# Defining sequence of functions in MATLAB

20 views (last 30 days)
Saurabh Madankar on 11 Feb 2022
Commented: Saurabh Madankar on 11 Feb 2022
Hello,
The Haar mother wavelet is defined as-
Now we can define a sequence of functions -
where
So for i=1, j=0 and k=0, i=2, j=1,k=0, i=3, j=1, k=1 and so on and we will define the functions accordingly. Now I could define these functions individually. But if I need to define till then how do I define them using for loop or any other way so I don't have to define each function individually?
Saurabh Madankar on 11 Feb 2022
Edited: Saurabh Madankar on 11 Feb 2022
Can we define a function inside for loop? So if we can define a function whose input is i and outputs are j and k, then for i=1, the outputs will be j=0, k=0, for i=2 the outputs will be j=1, k=0, for i=3, j=1,k=1, for i=4, j=2,k=0,...,for i=7, j=2,k=3 and so on. So in each loop we will get j and k for the corresponding i before defining the main sequence function.

Walter Roberson on 11 Feb 2022
Well, you can do
syms h(t) [1 5]
whos
Name Size Bytes Class Attributes h 1x1 8 symfun h1 1x1 8 symfun h2 1x1 8 symfun h3 1x1 8 symfun h4 1x1 8 symfun h5 1x1 8 symfun t 1x1 8 sym
but although that creates abstract functions with those names, it does not in itself give you a way to define the functions.
syms() does give a way to define symbols as existing without assigning to them, but when you use syms() that way, you cannot give them expressions. sym() (without an s) cannot be used to define variables without assignining to them, and symfun() cannot be used to define variables without assigning to them either. And the "assigning to them" is the problem, as the name to be assigned to is changing.
It isn't that it can't be done... but it is seldom a good idea to use the dirty parts of MATLAB that allow defining variables with constructed names.
I would recommend that you instead use cell arrays.
h{i}(t) = expression involving h{i-1}
Note that even if you did use one of the techniques for creating dynamic variable names, each of the functions is going to end up fully expanding all earlier functions, rather than just referring to the functions. For example,
f(t) = t + 1
f(t) =
g(t) = f^2 + 2
g(t) =
Notice that g(t) does not refer to f(t) -- it evaluated f(t) to get t+1, and squared that result. This is inherent in the way that Symbolic Toolbox works.
Saurabh Madankar on 11 Feb 2022
Thanks, I used cell arrays to define the sequence of functions and using function inside for loop like I said in the comment I was able to solve the whole problem. This was a huge help. Thanks for your time.