Use index in anonymous function

3 views (last 30 days)
Hi guys
can i include index in anonymous function for example something like this where T is matrix element. And can i call it in for loop for example:
syms T n m
delta_x=(1/50);delta_t=0.001;
x=0:delta_x:1;
t=0:delta_t:0.25;
Lamda=((delta_t)/((delta_x)^2));
n=1:length(t);m=2:length(x);k=1;
Tem=@(T,n,m)((-Lamda*T(n+1,m-1))+(2*(1+Lamda)*T(n+1,m))-(Lamda*T(n+1,m+1)))....
-((Lamda*T(n,m-1)) +(2*(1-Lamda)*T(n,m)) +(Lamda*T(n,m+1)));
for n=1:length(t)-1
for m=2:length(x)
if n==1
T(n,m)=250;
elseif m==1 && n>=2
T(n,m)=350;
else
Temp{1}=Tem(T,n,m); % I want to generate several equations from thie line
end
end
end
Best Regards,

Accepted Answer

Walter Roberson
Walter Roberson on 3 Oct 2013
Yes, it is legal to index a symbol in an anonymous function. It is recommended, however, to initialize the symbol as a matrix.
e.g.,
A = sym('A',dim)
where
dim: Integer or vector of two integers specifying dimensions of A. For example, if dim is a vector [m n], then the syntax A = sym('A',[m n]) creates an m-by-n matrix of symbolic variables. If dim is an integer n, then the syntax A = sym('A',n) creates a square n-by-n matrix of symbolic variables.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!