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

1 vue (au cours des 30 derniers jours)
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?

Réponse acceptée

Michael Soskind
Michael Soskind le 18 Mar 2021
Sushanth,
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,
Michael

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by