Flexible symbolic function definition
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Paul
am 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?
Antworten (4)
Paul
am 10 Jan. 2023
Bearbeitet: Paul
am 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))
Create a symfun if desired:
f(symvar(w3)) = w3
1 Kommentar
Walter Roberson
am 10 Jan. 2023
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]
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))
f(allvars) = w3
This is a function that expects all of those positions, but only uses some of them.
Stefan Engstrom
am 10 Jan. 2023
1 Kommentar
Paul
am 10 Jan. 2023
Bearbeitet: Paul
am 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
formula returns the formula of f3
formula(f3)
So using what we have above
args = symvar(formula(f3));
f3(args) = formula(f3)
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.
Stefan Engstrom
am 10 Jan. 2023
3 Kommentare
Paul
am 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.
Walter Roberson
am 10 Jan. 2023
I suggest that you look at the 'vars' option of matlabFunction
0 Kommentare
Siehe auch
Kategorien
Mehr zu Conversion Between Symbolic and Numeric finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!