- if you have already calculated all of the values and stored them in an array then use sum()
- if you have not already calculated the values, initialize the total to zero and then loop generating each of the terms in sequence, adding each value to the running total as you go.
How to define a function with summation.
7 次查看(过去 30 天)
显示 更早的评论
how would I define a function named ccsum , whose inputs are function handle f and the total number of terms for summation k. The output would be
.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1165183/image.png)
I got started on the code but got stuck
function s = ccsum(f,k);
s = sum(f(1,k));
end
0 个评论
采纳的回答
Walter Roberson
2022-10-22
you cannot assume that the function handle refers to vectorized code, so you need to somehow invoke f on each of the integers 1 to k, and create a total.
The two fundamental ways of totalling values are:
1 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!