Flexible symbolic function definition

I am using symbolic functions but need it somehow to be flexible in terms of number of variables I use.
For 2 variable I get
syms a b
syms f1(a,b)
z = a + b
For 5 variables I get
syms a b c d e
syms f1(a,b,c,d,e)
z = a + b + c + d + e
I think for the first line of code I can use
syms a [1 N]
where N is number of variables. The results will be
a = (a1 a2)
But how do I create f1(a,b,c...) in a flexible way?

2 commentaires

Paul
Paul le 10 Jan 2023
HI Stefan,
Not really sure what you're trying do. How does z play into any of this? Can you give a little more detail on the workflow of how f1 is supposed to be formed, including its right-hand-side definiton?
Are you trying to start with symbolic expression, like z, and conver it into a symfun object with arguments being the variables in z?
Hi Paul,
Thanks for the response.
I want to create a third order polynomial expansion for z based on the symbols.
syms a [1 N_beams]
len_a = length(a);
z=0;
for q1 = 1:len_a
z = z + a(q1);
% f1(a,b,c...)
end
z^2*conj(z);
w3 = expand(z^2*conj(z))
The answer is
I would prefer to use a, b, c, etc instead of a with indices as it is easie to read.
f1 is used late in the code where I insert an array for each a,b,c, etc
IM3_exitations(k,:) = double(IM3(k).f3(A,B,C,D));

Connectez-vous pour commenter.

Réponses (4)

Paul
Paul le 10 Jan 2023
Modifié(e) : Paul le 10 Jan 2023
I'm still not sure what you want. Maybe this?
syms a b c
vars = [a b c];
len_vars = length(vars); % might want to use numel
z=0;
for q1 = 1:len_vars
z = z + vars(q1);
% f1(a,b,c...)
end
z^2*conj(z);
w3 = expand(z^2*conj(z))
w3 = 
Create a symfun if desired:
f(symvar(w3)) = w3
f(a, b, c) = 

1 commentaire

You can also do
syms a b c d e f g h i j k l m n o p q r s t u v w x
allvars = [a b c d e f g h i j k l m n o p q r s t u v w x]
allvars = 
vars = allvars(1:3);
len_vars = length(vars); % might want to use numel
z=0;
for q1 = 1:len_vars
z = z + vars(q1);
% f1(a,b,c...)
end
z^2*conj(z);
w3 = expand(z^2*conj(z))
w3 = 
f(allvars) = w3
f(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x) = 
This is a function that expects all of those positions, but only uses some of them.

Connectez-vous pour commenter.

Stefan Engstrom
Stefan Engstrom le 10 Jan 2023
Thanks Paul,
It works for the polynomial expansion. Thanks.
Now it is the f3 syms that I don't know what to do.
if I create
syms f3(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x)
Can I then remove, for instance d to x somehow if I only want a,b and c?

1 commentaire

Paul
Paul le 10 Jan 2023
Modifié(e) : Paul le 10 Jan 2023
I'm going to assume that you want only a, b, and c as arguments because those are the only variables in the function formula
syms f3(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x)
f3(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x) = a*b + b/c
f3(a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v, w, x) = 
formula returns the formula of f3
formula(f3)
ans = 
So using what we have above
args = symvar(formula(f3));
f3(args) = formula(f3)
f3(a, b, c) = 
If my assumption is incorrect, and you really have f3 as symfun without a formula and you just wnat to manipulate its arguments, you can use argnames to get the arguments to f3 and go from there.
Also, I edited my other answer to simplify it.

Connectez-vous pour commenter.

Stefan Engstrom
Stefan Engstrom le 10 Jan 2023

0 votes

Thanks again Paul,
How can I remove d and onwards as arguments in f3?

3 commentaires

Stefan,
It would be better to follow up with a Comment on the relevant Answer instead of adding a new Answer all the time. Anyway, I don't understand what you're asking here. Are you saying that you have f3 defined without a formula?
syms f3(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x)
Now, what do you want to produce from f3?
Stefan Engstrom
Stefan Engstrom le 10 Jan 2023
It is a long code and a lot of steps in between the "syms" and when I use it.
I started with only one or two arguments, a and b. But now I realize I need to use up to 24, a to x. So I don't want to do 24 "syms f(a.....)". Hence my question.
Paul
Paul le 10 Jan 2023
Still not clear. We don't need to see your full code, just a clear explanation of what you want your code to do. Are you trying to create a symbolic function in some code, but you don't know a priori how many arguments that function will take? Do you have a symbolic function already defined and you want to change the argument signature of that function? Something else? Ideally, you can provide a minimal working example in code that shows what you have working with explanation of how you'd like to make it work better, or a minimal working example that shows what code you have, where your stuck, and the desired end result. Either would help me (or anyone else) help you.

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 10 Jan 2023

0 votes

I suggest that you look at the 'vars' option of matlabFunction

Produits

Version

R2022a

Commenté :

le 10 Jan 2023

Community Treasure Hunt

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

Start Hunting!

Translated by