*** 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

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

Jan
Jan el 21 de Mayo de 2017
Editada: Jan el 21 de Mayo de 2017
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.
Hi Jan, I'm sorry but what do you mean by 'if the asking person does not care at all, if the question is readable.' you didn't understand the statement of question that question says? if that's your question then you may see the inserted image. Why do I assume that there is a mistake because my autograder says it's incorrect.
Stephen23
Stephen23 el 21 de Mayo de 2017
Editada: Stephen23 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.

@ stephen I'm really sorry. Next I will keep this in mind.

@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.

Iniciar sesión para comentar.

 Respuesta aceptada

Jan
Jan el 21 de Mayo de 2017
Editada: Jan el 21 de Mayo de 2017
denominator = (2 * k + 1) .^ 2;
Use the elemtnwise .^ instead of the matrix operation ^ . The same for "(-1)^k" .
The corresponding error message is clear:
Error using ^
Inputs must be a scalar and a square matrix.
To compute elementwise POWER, use POWER (.^) instead.
[EDITED] The formula contains: "Sum from k=0 to n", you code contains:
k = 1:n;
Replace this by k = 0:n. It will be more efficient to move the definition before the loop and pre-allocate the output:
function S = triangle_wave(n)
t = linspace(0, 4*pi, 1001);
S = zeros(size(t)); % Pre-allocation
k = 0:n; % Not 1:n
for tIndex = 1 : length(t)
numerator = ((-1) .^ k) .* sin(t(tIndex) .* (2 * k + 1));
denominator = (2 .* k + 1) .^ 2;
S(tIndex) = sum(numerator ./ denominator);
end

6 comentarios

Sorry but it didn't make any difference. My code is still incorrect.
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
Jan
Jan el 21 de Mayo de 2017
Editada: Jan el 21 de Mayo de 2017
This does not make a difference? Formerly your code failed with an error message. Does it now also?
Please explain, why you think, that the code is not correct: Do you get an error message or do the results differ from your expectations? It is more efficient not to let the readers guess this very important detail.
Read the formula and your code again. See [EDITED] in my answer above.
sir I do understand whole code except why did you use 0:n rather than 1:n as well as zeros(size(t))
@Waseem: Look at the formula: Look at the limits of the sum, it goes from k = 0 to n. Do you see it under and above the Sigma? With the zeros() command I've pre-allocated the output. Search in this forum for "pre-allocation".
can you please tell how is the value of k changing when it is placed outside the loop??

Iniciar sesión para comentar.

Más respuestas (2)

(-1)^k needs to be (-1).^k
(2 * k + 1)^2 needs to be (2 * k + 1).^2
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

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

Iniciar sesión para comentar.

Categorías

Preguntada:

el 20 de Mayo de 2017

Comentada:

el 29 de Abr. de 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by