Function handle of a sum

5 visualizzazioni (ultimi 30 giorni)
Bernoulli Lizard
Bernoulli Lizard il 15 Mar 2013
Modificato: Matt J il 24 Apr 2015
How can I define a function that involves a summation?
for example, a function f = @(x) e^(-n*x), where the function is a sum of terms over some range of n?
Thanks
  1 Commento
Bernoulli Lizard
Bernoulli Lizard il 15 Mar 2013
Actually it's slightly more complicated than that. I need the function to be of another function of n, so something of the form
f = @(x) exp( -g(n) * x ), summed over some range of n.

Accedi per commentare.

Risposta accettata

Matt J
Matt J il 15 Mar 2013
Modificato: Matt J il 15 Mar 2013
Assuming g(n) has vectored input/output, just do
f=@(x) sum( exp( -g(1:n) .* x )); %sum over 1...n
with obvious modifications for different ranges of n.
  2 Commenti
Bryan Engelhardt
Bryan Engelhardt il 24 Apr 2015
Modificato: Matt J il 24 Apr 2015
The function doesn't work for me when integrating. In other words,
g = [1,2]
f = @(x) sum( exp( -g(1:2) .* x));
integral(f,0,1);
gives me a matrix dimension error. How do I fix this?
Matt J
Matt J il 24 Apr 2015
Modificato: Matt J il 24 Apr 2015
g = [1,2]
f = @(x) sum( exp( -g(1:2) .* x));
F= @(X) arrayfun(f,X);
integral(F,0,1);
or
integral(f,0,1,'ArrayValued',1);

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by