How can I write the arguments of an anonymous function when programming?

조회 수: 2 (최근 30일)
Good afternoon, my question arises when trying to programme the following code:
s(i+1)=NRK(Dt*f(tv(i+1),x)+s(i)-x,s(i));
Where NRK=NRK(function , numeric scalar) This was the symbolic implementation, with f=symbolic function, and x a symbolic array of unknowns.
The thing is that working with symbolic expressions can solve the issue, but this goes inside a loop, and symbolic tools slow down suprisingly the performance in a ratio of 100 times! However, anonymous functions do a perfect job.
My try was the following:
h=@([arguments (i.e. a, b, c, ...])Dt*f(t(i+1),[arguments (i.e. a, b, c,...])+s(i)-[a b c ...];
s(i+1)=NRK(@h,s(i));
How can I write these arguments? Is it possible? Thank you very much!

채택된 답변

per isakson
per isakson 2014년 4월 17일
편집: per isakson 2014년 4월 18일
Doesn't the info in Anonymous Functions cover your case?
I'm guessing. Try something like to create the anonymous function
Dt = ???;
s = ???;
f = ???;
fh = @(a,b,c,ii) Dt*f( t(ii+1),a,b,c) + s(ii)-[a,b,c];
(What are Dt, s, f and t ?) And call it
fh(a,b,c,ii)
.
Added later: With varying number of input variables
Try
[fh1,fh2] = cssm( );
a = 'a1';
b = 'b2';
c = 'c3';
fh1( a )
fh1( a, b, c )
fh2( a )
fh2( a,b,c )
which returns
a1
a1, b2, c3
a1
a1, b2, c3
where
function [fh1,fh2] = cssm( )
fh1 = @(varargin) foo( varargin{:} );
fh2 = @(varargin) disp( strjoin( varargin, ', ' ) );
function foo( varargin )
disp( strjoin( varargin, ', ' ) )
end
end

추가 답변 (1개)

Piockñec
Piockñec 2014년 4월 18일
Thank you for your answer! Your solution is right, when writing an anonymous function for 3 variables (a,b,c). The problem is that the number of variables can vary (a,b,c,d,e,f...)... because x is a symbolic array of unkwnowns.
However, I have written the programme so that I do not need to solve my issue. Thank you again!!!
  댓글 수: 1
per isakson
per isakson 2014년 4월 18일
I added a very simple case with a varying number of variables to my answer.

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

카테고리

Help CenterFile Exchange에서 Function Creation에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by