summing to create a vector
Afficher commentaires plus anciens
I'm trying to write a function that calculates sum of ((-1)^k *sin((2k+1)t))/((2k+1)^2 ( for k=0 to n) t varies from 0 to 4*pi with 1001 values its supposed to return a vector with size n this is the code i wrote
function [summ ]= triangle_wave (n)
for k=1:n;
for t= linspace(0,4*pi,1001);
summ=sum((-1)^k*sin((2*k+1)*t))/((2*k+1)^2);
end
end
end
it keeps outputting the last calculate sum instead of adding each sum to the vector . what can i add to this code to achieve that ?
Réponse acceptée
Plus de réponses (1)
Azzi Abdelmalek
le 3 Juin 2016
n=20
t= linspace(0,4*pi,1001);
for k=1:n
s(k)=sum((-1)^k *sin((2*k+1)*t)/((2*k+1)^2)) ;
end
s
2 commentaires
OLUBUKOLA ogunsola
le 3 Juin 2016
OLUBUKOLA ogunsola
le 3 Juin 2016
Modifié(e) : Walter Roberson
le 3 Juin 2016
Catégories
En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!