*** 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
Mostrar comentarios más antiguos

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 comentarios
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
el 21 de Mayo de 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
el 21 de Mayo de 2017
Jan
el 21 de Mayo de 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.
Respuesta aceptada
Más respuestas (2)
Walter Roberson
el 21 de Mayo de 2017
1 voto
(-1)^k needs to be (-1).^k
(2 * k + 1)^2 needs to be (2 * k + 1).^2
Vikrant dhall
el 29 de Abr. de 2018
0 votos
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 comentario
Vikrant dhall
el 29 de Abr. de 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
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
