Write a function called triangle_wave

8 ビュー (過去 30 日間)
Okongwu Chukwuebuka
Okongwu Chukwuebuka 2017 年 4 月 5 日
回答済み: Theocharis Maitis 2018 年 11 月 30 日
The function computes the sum Σ((−1^k)*sin((2k+1)(t))/((2k+1)^2)) 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”.
For this question, I have written a function to solve it but my code is always rejected by the grader.
This is the plot I get whenever I plot my function;
My attempt made use of a nested for loop as shown:
Please any clue to get my code right will be highly appreciated. Thanks
  2 件のコメント
Jan
Jan 2017 年 4 月 5 日
編集済み: Stephen23 2017 年 4 月 5 日
Please post your code as text using the "{} Code" format button. Using a screenshot is less convenient, when parts of the code could be used by copy&paste. Now posting an answer requires to retype your code, which is very tedious, because TMW decided to insert a bunch of GUI elements between the question and the answer, such that scrolling is obligatory, if you do not have a 56'' monitor with Giga pixels.
Stephen23
Stephen23 2017 年 4 月 5 日
@Okongwu Chukwuebuka: please edit your question and delete the useless screenshot and replace it with actual text that we can run. Make sure to format your code by selecting it and then clicking the {} Code button.

サインインしてコメントする。

採用された回答

Jan
Jan 2017 年 4 月 5 日
編集済み: Jan 2017 年 4 月 5 日
Do you have any useful details about the "rejection of the grader"? Is this a Cody Coursework? I start to detest this, because many students struggle with the automatic rejection without useful information. I do not believe in automatic teaching.
Perhaps it matters than 4*pi is not 12.5664. Try:
m = linspace(0, 4*pi, 1001)
You have to reset sump to 0 for each time point. Move the sump = 0 inside the for t loop.
By the way: The iterative growing of arrays is a bad programming practice. Care for a proper pre-allocation is real programs:
a = zeros(1, length(m));
...
for t = 1:length(m)
...
sump = 0;
for k = 0:n
...
end
a(t) = sump;
end
  2 件のコメント
Okongwu Chukwuebuka
Okongwu Chukwuebuka 2017 年 4 月 6 日
Thanks a million! You have helped to reignite my zeal for coding once more.
Okongwu Chukwuebuka
Okongwu Chukwuebuka 2017 年 4 月 6 日

サインインしてコメントする。

その他の回答 (3 件)

Torsten
Torsten 2017 年 4 月 5 日
Use (-1)^k instead of (-1^k).
Best wishes
Torsten.
  2 件のコメント
Jan
Jan 2017 年 4 月 5 日
+1, Most likely. The text of the question contains (-1^k) already, which is equivocal.
Okongwu Chukwuebuka
Okongwu Chukwuebuka 2017 年 4 月 6 日
Thank you too, Your contribution was helpful.

サインインしてコメントする。


Konstantinos Pantazis
Konstantinos Pantazis 2018 年 7 月 6 日
I am new to Matlab but it seems that how I proceed below gives the correct answer.
function v=triangle_wave(n)
t= *linspace*(0,4*pi,1001);
k=0;
l=0;
for k=0:n
l=l+((-1)^k)*sin((2*k+1).*t)./(2*k+1)^2;
end
v=l;

Theocharis Maitis
Theocharis Maitis 2018 年 11 月 30 日
function [v]=triangle_wave(n)
t=linspace(0,4*pi,1001);
v=0;
for k=0:n
v=v+((-1)^k)*sin((2*k+1).*t)./(2*k+1)^2;
end
plot(v)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by