How can I sum anonymous functions?

Dear all, I have a for-loop and every for-loop creates an instance of an anonymous function. In the end I would like to sum all these anonymous functions to one function, which I can then use to evaluate for different t. So far I have the following, which compiles, but gives me an error when trying Hn(0.5). Thanks for your help :)
A= rand(60,200);
X = randn(200,1);
eta = sqrt(0.03)*randn(60,1);
[u, s, v] = svd(A);
[m,~] = size(A);
xi = zeros(m,1);
nu = zeros(m,1);
alpha = zeros(m,1);
Hn = 0;
for i = 1:m
xi(i) = dot(X, v(:,i));
nu(i) = dot(eta, u(:,i));
alpha(i) = xi(i)*(s(i,i)*xi(i)+nu(i));
h = @(t) ((s(i,i)*nu(i)*inv(xi(i))+1)*t-1)/(1-(1-s(i,i)^2)*t)^3;
Hn = @(t) Hn(t)+s(i,i)*alpha(i)*h(t);
end

 Respuesta aceptada

Rik
Rik el 31 de Oct. de 2017
You need to initialize Hn as an anonymous function as well:
Hn = @(t) 0;

4 comentarios

Judith Wewerka
Judith Wewerka el 31 de Oct. de 2017
Thank you!! I just forgot about this...
Rik
Rik el 31 de Oct. de 2017
You're welcome. (If this solved your issue, could you mark this as accepted answer? It will give me reputation points and make it easier for others with similar questions to find this answer)
Eray Tunç YILMAZ
Eray Tunç YILMAZ el 23 de Mayo de 2019
Editada: Rik el 24 de Mayo de 2019
Hello,
I have a similar problem
f = @(x) 0;
for i=1:n
f =(@x) f(x) + c(i) * cos(i*x);
end
where is c is a vector.
It gives an error as: parse error at f: usage might be invalid MATLAB syntax.
What is wrong here?
Thank you in advance
Rik
Rik el 24 de Mayo de 2019
You should put this instead:
f = @(x) 0;
for i=1:n
f =@(x) f(x) + c(i) * cos(i*x);
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 31 de Oct. de 2017

Comentada:

Rik
el 24 de Mayo de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by