Flexible symbolic function definition
조회 수: 8 (최근 30일)
이전 댓글 표시
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
Paul
2023년 1월 10일
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?
답변 (4개)
Paul
2023년 1월 10일
편집: Paul
2023년 1월 10일
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
Walter Roberson
2023년 1월 10일
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
2023년 1월 10일
댓글 수: 1
Paul
2023년 1월 10일
편집: Paul
2023년 1월 10일
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
2023년 1월 10일
댓글 수: 3
Paul
2023년 1월 10일
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Symbolic Variables, Expressions, Functions, and Settings에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!