*** Write a function called triangle_wave that computes the sum (−1) sin(2 +1) (2 +1) 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 v
Show older comments

this is my code and I need your help I couldn't figure out where I'm making mistake.
function theSum = triangle_wave(n)
t = linspace(0, 4*pi, 1001);
for tIndex = 1 : length(t)
this_t = t(tIndex);
k = 1 : n;
numerator = ((-1)^k).*sin(this_t * (2*k+1));
denominator = (2 * k + 1)^2;
theSum(tIndex) = sum(numerator ./ denominator);
end
5 Comments
I've edited your code and inserted the required empty lines to let your image appear. It discourages me to post an answer, if the asking person does not care at all, if the question is readable.
You forgot to mention, why you assume that there is a mistake.
Wasi von Deutschland
on 21 May 2017
@Waseem Iqbal: actually Jan Simon edited your code so that it is readable, and so that the image is visible. You did not. When you show unreadable code, then it implies that you you do not want anyone to read it, which implies that you don't want help. The clearer your present your question, the more likely you are to get help.
Wasi von Deutschland
on 21 May 2017
Jan
on 21 May 2017
@Waseem Iqbal: It is okay. This happens frequently and we all remind newcomers to improve the format. Now let's come back to the question for clarification:
You forgot to mention, why you assume that there is a mistake. Please explain this.
Accepted Answer
More Answers (2)
Walter Roberson
on 21 May 2017
1 vote
(-1)^k needs to be (-1).^k
(2 * k + 1)^2 needs to be (2 * k + 1).^2
Vikrant dhall
on 29 Apr 2018
0 votes
function v = triangle_wave(n) t = linspace(0,4 * pi, 1001); v = zeros(size(t)); p = 1; for tt = 0:length(t) suma = 0; for k = 0:n suma = suma + (((-1 .^ k) .* sin((2.*k+1).*tt)) / ((2.* k+1).^2)); %suma end %suma v(1,p) = suma; p = p + 1; end end
What is the error in this can anyone please help
1 Comment
Vikrant dhall
on 29 Apr 2018
function v = triangle_wave(n)
if true
% code
end
t = linspace(0,4 * pi, 1001);
v = zeros(size(t));
p = 1;
for tt = 0:length(t)
suma = 0;
for k = 0:n
x = ((-1 .^ k) .* sin((2.*k+1).*tt)) / ((2.* k+1).^2);
suma = suma + x;
%suma
end
%suma
v(1,p) = suma;
p = p + 1;
end
end
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!