How can I generate a matrix of symbolic functions, whose size varies depending on the input values?

For example, I want to generate 4 symbolic functions which depend on the variable 't'. They should be something like th1(t), th2(t), th3(t), and th4(t). It's easy to define these variables when the number of variables is less and that number is constant. How can I automatically get a matrix of functions whose size may vary?

 Réponse acceptée

madhan ravi
madhan ravi le 3 Nov 2018
Modifié(e) : madhan ravi le 3 Nov 2018

13 commentaires

Do you also know any way to automatically store all the created symbolic functions into a matrix?
Sir, first I created symbolic functions using
if true
TH = sprintfc('th%d(t)', 1:n);
syms(TH{:})
end
I got the symbolic functions th1(t), th2(t) to thn(t). Now if I use
if true
TH = sym('th%d', [n 1])
end
It is storing new symbolic variables th1, th2,.... thn which are not functions of time. As I am trying to use these variables in a function code, I need a method to somehow call the symbolic function variable name. Can you help me with this?
Try:
TH = str2sym(sprintfc('th%d(t)', 1:5))
Sir, I am using MATLAB 2016a. str2sym is not working.
You should probably not be trying to call them by name for the reason noted in the link about eval.
If you want them put in a matrix then what kind of matrix? As I explained in the link above if you put them in a regular matrix you will not be able to index them.
So is there no way of calling the generated symbolic functions inside the code?
We did not say that it cannot be done. We said that it is seldom a good idea to do so. And we asked what kind of matrix you need, since there are limitations with symbolic functions, that a regular array of symbolic functions is treated the same way as a single symbolic function that produces an array of output. You should read that as meaning that you might need to use a cell array.
Typically it works out better to use symbolic expressions instead of symbolic functions, except some cases involving differential equations.
Ok. I will tell you what I am trying to code. I am trying to code an n-pendulum connected problem. The input will be the number of rods. As there is no standard equation for n links pendulum. I have to derive the equations inside of the code. So if I am able to access n symbolic functions, I will be able to derive the necessary equations and then will be able to solve them. This is the reason why I was asking about accessing the symbolic functions. There might be better ways to approach this problem, but this is what I have thought. Please let me know if I can proceed in this fashion or let me know if there is any other better way.
Use a cell array of symbolic functions.
TH = sprintfc('th%d(t)', 1:n);
funs = arrayfun( @(th) sym(th), TH, 'uniform', 0);
Now funs{1} through funs{n}

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by