Wrong answer to straightforward problem:
Afficher commentaires plus anciens
* Write a function called triangle_wave that computes the sum

for each of 1001 values of t uniformly spaced from 0 to 4π inclusive. The input argument is a scalar non-negative integer n, and the output argument is a row vector of 1001 such sums—one sum for each value of t. You can test your function by calling it with n == 20 or greater and plotting the result and you will see why the function is called “triangle_wave”.
My code is:
function [ out ] = triangle_wave( n )
%TRIANGLE_WAVE Summary of this function goes here
% Detailed explanation goes here
t = 4*pi/1000;
tvector = 0:t:4*pi;
length(tvector)
out = 0;
for count = 1:n+1
A= -1^(count-1);
B = sin((2*(count-1)+1)*tvector);
C = (2*(count-1)+1)^2;
triangle = A*B/C;
out = out + triangle;
end
end
This seems like it should be fairly straightforward.
Réponse acceptée
Plus de réponses (1)
DJ V
le 6 Déc 2016
0 votes
Catégories
En savoir plus sur Matrix Indexing 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!