how to find sum of square sine wave

4 次查看(过去 30 天)
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................
  3 个评论
Muhammad Usman Saleem
@thanks Image.. next time i shall take care of it.. Answer my question.. please
Salaheddin Hosseinzadeh
Hi Muhammad,
Give it a try it's fairly easy, its a good practice to learn.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2015-5-29
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
%
  3 个评论
Muhammad Usman Saleem
Thanks @image... Below is my understanding of my your code which is mark in comments..
function theSum = s_wave(n)
% set input argument n
t = linspace(0, 4*pi, 1001);
%using the linspace function generate a vector from 0 to 1001 with step interval of 4*pi.
for tIndex = 1 : length(t)
% loop over the length of vector t
this_t = t(tIndex);
% i do not get this statement clearly. To me i understand every time the loop run , we call that element of t
k = 1 : n;
numerator = sin(this_t * (2*k-1));
denominator = 2 * k - 1;
theSum(tIndex) = sum(numerator ./ denominator);
end
Image Analyst
Image Analyst 2015-5-30
Yes, every time you run the loop (do one iteration for one value of t), it will do the sum over k for all values of n. The sum over k=1:n is done "vectorized" rather than using a "for" loop, so each variable (numerator, denominator, theSum(t)) is a vector of n elements.

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2015-5-29
k = 1 : n;
numerator = sin(t * (2*k-1));
denominator = 2 * k - 1;
theSum = sum(numerator ./ denominator);
this is just very very basic MATLAB vectorization like you learn within the first hour of taking MATLAB training, so you need to look at this link
  3 个评论
Muhammad Usman Saleem
To me i understand the question in the following code..
function [sq] =square_wave(n)
k = 1 : n;
numerator = sin(t * (2*k-1));
denominator = 2 * k - 1;
theSum = sum(numerator ./ denominator);
when i check my code i got an error
Feedback: Your program made an error for argument(s) 1
@image guide me about corrections..thanks in advance
Joseph Cheng
Joseph Cheng 2015-5-29
Kindly put some effort into understanding what Image Analysis has done which gets you 80% of the way there.
Another method is to go
t = 0:.1:4*pi;
k = 1:10;
[T K]= meshgrid(t,k);
Calc= sin((2*K-1).*T)./(2*K-1);

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by