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

4 次查看(过去 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!!!

类别

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