I want to creat a function with multiple variables, something like:
f = @(x) = f1(x) + f2(x) + ... + fn(x)
where x is a n-dimensional vector which can be define by user. The number of functions f1,f2,..,fn can be define by user, too.
The question is can i use a for loop in function handle to define f according to the length of vector x?

답변 (2개)

Stephen23
Stephen23 2020년 6월 24일
편집: Stephen23 2020년 6월 24일

1 개 추천

You can simply store function handles in a cell array of any size and use cellfun. No loop required.
>> C = {@sin,@sqrt,@pow2};
>> F = @(x) sum(cellfun(@(f)f(x),C));
>> F(3)
ans = 9.8732
compare with:
>> sin(3) + sqrt(3) + pow2(3)
ans = 9.8732
If you really want to use a for loop then you would need to write the function in an Mfile, e.g.:
function out = myfun(x)
out = 0;
C = {@sin,@sqrt,@pow2};
for k = 1:numel(C)
out = out + C{k}(x);
end
end

댓글 수: 8

Andy Tran
Andy Tran 2020년 6월 24일
Thanks for replying me.
But my situation is a bit more complicated. The functions f1,f2,...,fn are not familier functions like sin,cos,pow2,... They are anonymous functions. For example f1 = @(x) = h1*x1+h2*x2+...+hn*xn, where h and n are n-dimentional vectors.
"But my situation is a bit more complicated. The functions f1,f2,...,fn are not familier functions like sin,cos,pow2,... They are anonymous functions"
That makes no difference, you can also use a cell array with anonymous functions:
C{1} = @(x)...;
C{2} = @(x)...;
... etc
and then use either the loop or cellfun (you might need to set its optional 'UniformOutput' to false),
That make sense. But what if:
C{1} = @(x) h1*x1 + h2*x2 +...+ hn*xn
where the number of variables "x" is an input parameter. I want to clair a function with unknown number of variables.
Andy Tran
Andy Tran 2020년 6월 27일
I think i got your point. But im not trying to create dynamical variable names. I want to create a function to calculate channel capacity acording to Shannon formular:
where p_n are variables. How can i define function C with unknown number of variables (i call it as N), others parameters are generate corresponding to N.
Walter Roberson
Walter Roberson 2020년 6월 27일
편집: Walter Roberson 2020년 6월 28일
You said it yourself, you have an unknown number of variables. The only way to address an indefinite number of variables is to dynamically create the names of the variables and access the contents of the variables through the names you created. Which we firmly advise you not to do.
where p_n are variables.
Don't do that. Instead make p and h into vectors. Then
dot(p, h) - p(n).*h(n)
provided that p(m) and h(m) are finite.
Stephen23
Stephen23 2020년 6월 28일
"How can i define function C with unknown number of variables (i call it as N), others parameters are generate corresponding to N."
Rather than creating lots of variables the simple and efficient MATLAB way is to use one vector/matrix/array.
Your approach is making this much more complex than it needs to be.
Andy Tran
Andy Tran 2020년 6월 28일
편집: Andy Tran 2020년 6월 28일
Yes i know it. But further i need to find the maximum of the function C minus D (which is a linear function acording to p_n) for each p_n. The way im trying to do is using anonymous function and so that i think i need to treat p_n as a variable. Do you have other suggestion?
Thanks for helping me out.

댓글을 달려면 로그인하십시오.

Walter Roberson
Walter Roberson 2020년 6월 26일

0 개 추천

The question is can i use a for loop in function handle to define f according to the length of vector x?
NO. Anonymous functions cannot use for .
Anonymous functions can use arrayfun() and cellfun() and can use function calls that process the resulting arrays.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2020년 6월 24일

편집:

2020년 6월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by