Borrar filtros
Borrar filtros

How to define a function with summation.

7 visualizaciones (últimos 30 días)
Mingxuan
Mingxuan el 22 de Oct. de 2022
Comentada: Walter Roberson el 22 de Oct. de 2022
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 .
I got started on the code but got stuck
function s = ccsum(f,k);
s = sum(f(1,k));
end

Respuesta aceptada

Walter Roberson
Walter Roberson el 22 de Oct. de 2022
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:
  • 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.
  1 comentario
Walter Roberson
Walter Roberson el 22 de Oct. de 2022
You should not assume that the function returns a scalar by the way.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by