MATLAB: How to find sum of square sine wave

MATLABseries sumsquare wave

Hi every one; I want to solve that question.Any assistance will be highly appreciable… function called s_wave that computes the sum for each of 1001 values of t uniformly spaced from 0 to 4π inclusive. The input argument is a positive scalar 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 == 200 or greater and plotting the result, and you will see why the function is called “s_wave”.
Again any assistance will be highly appreciable…………….

Best Answer

Alright, it looks like you need a lot more help than the hints I gave in my other answer. Here is one way to do it (Put all code in both functions into a single "test.m" and run it):
function test
fontSize = 24;
n = 200;
theSum = s_wave(n);
plot(theSum, 'b-', 'LineWidth', 2);
% Fancy up the plot.
grid on;
caption = sprintf('s_wave : Sum of %d terms', n);
title(caption, 'FontSize', fontSize, 'Interpreter', 'none');
xlabel('t', 'FontSize', fontSize);
ylabel('theSum', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')
%============================================================
function theSum = s_wave(n)
t = linspace(0, 4*pi, 1001);
for tIndex = 1 : length(t)
this_t = t(tIndex);
k = 1 : n;
numerator = sin(this_t * (2*k-1));
denominator = 2 * k - 1;
theSum(tIndex) = sum(numerator ./ denominator);
end
%