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

1 次查看(过去 30 天)
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?

采纳的回答

Michael Soskind
Michael Soskind 2021-3-18
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by