Writing summation inside function handle

Hey,
I have this term
u = @(x) c(1)*f(3*(x-t(1)))+c(2)*f(3*(x-t(2)))+c(3)*f(3*(x-t(3)))+c(4)*f(3*(x-t(4)))+...
c(5)*f(3*(x-t(5)))+c(6)*f(3*(x-t(6)));
inside a function I have written. What I want is to write these six terms here using summation. So how should I go about it?

댓글 수: 3

John D'Errico
John D'Errico 2021년 11월 10일
It depends. Is the function f vectorized? If it is, then the answer is simple, even trivial. But if you cannot pass a vector of elements to the function f and have f operate on each element independently, then the solution will be more complicated, and very different.
So everything depends on the function f, and since we are not told what f is or how it is written, then no definitive answer is possible.
Here f is also a function handle, a function of x (which I have defined as a linearly spaced vector) which returns a scalar value.
And yes I can't pass a vector of elements to f. It takes only one element at a time.

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

답변 (1개)

Aneela
Aneela 2024년 2월 19일

0 개 추천

Hi Saurabh Madankar,
You can make use of the “sum” and “arrayfun” MATLAB functions to write these 6 terms using summation.
u = @(x) sum(arrayfun(@(k) c(k) * f (3 * (x - t(k))), 1:6));
Refer to the following links for further details on “sum” and “arrayfun“ functions in MATLAB.

카테고리

도움말 센터File Exchange에서 Performance and Memory에 대해 자세히 알아보기

질문:

2021년 11월 10일

답변:

2024년 2월 19일

Community Treasure Hunt

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

Start Hunting!

Translated by