Function handle of a sum

조회 수: 3 (최근 30일)
Bernoulli Lizard
Bernoulli Lizard 2013년 3월 15일
편집: Matt J 2015년 4월 24일
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
Bernoulli Lizard
Bernoulli Lizard 2013년 3월 15일
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.

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

채택된 답변

Matt J
Matt J 2013년 3월 15일
편집: Matt J 2013년 3월 15일
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
Bryan Engelhardt
Bryan Engelhardt 2015년 4월 24일
편집: Matt J 2015년 4월 24일
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 2015년 4월 24일
편집: Matt J 2015년 4월 24일
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);

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Multidimensional Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by