Finding a summation of a two variable function keeping one parameter constant.

9 views (last 30 days)
I need to do a summation of a function f(x,n) such that x remains as a variable in the process and n varies from 1 to N ( like 1:100 or something).
So I need f(x,1) + f(x,2) + f(x,3) ........... + f(x,N) in a variable SUM, such that SUM could work like a function handle, with x alone as its argument.
f(x,n) is any arbitrary function.
How do I do this in MATLAB?

Accepted Answer

Michael Soskind
Michael Soskind on 18 Mar 2021
The method for doing this depends on your function f(x,N).
If this function works with vectors, then the easiest function for this would be something in the form:
N = 1:100;
sum_function = @(x) sum(f(x, N)); % Assuming f(x,N) can take a vector N and is already defined
Then, calling sum_function(10) would provide the summation with x = 10.
Alternatively, you could make a function that only accepts X, and a range to sum over, and go through a for loop summing the values over that range. However, that is up to you.
Hope that helps,

More Answers (0)

Community Treasure Hunt

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

Start Hunting!