Wrong answer to straightforward problem:
이전 댓글 표시
* 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.
채택된 답변
추가 답변 (1개)
카테고리
도움말 센터 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!