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?
댓글 수: 0
채택된 답변
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!