Borrar filtros
Borrar filtros

how to find sum of square sine wave

4 visualizaciones (últimos 30 días)
Muhammad Usman Saleem
Muhammad Usman Saleem el 29 de Mayo de 2015
Comentada: Image Analyst el 30 de Mayo de 2015
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 comentarios
Muhammad Usman Saleem
Muhammad Usman Saleem el 29 de Mayo de 2015
@thanks Image.. next time i shall take care of it.. Answer my question.. please
Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh el 29 de Mayo de 2015
Hi Muhammad,
Give it a try it's fairly easy, its a good practice to learn.

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 29 de Mayo de 2015
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 comentarios
Muhammad Usman Saleem
Muhammad Usman Saleem el 30 de Mayo de 2015
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 el 30 de Mayo de 2015
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.

Iniciar sesión para comentar.

Más respuestas (1)

Image Analyst
Image Analyst el 29 de Mayo de 2015
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 comentarios
Muhammad Usman Saleem
Muhammad Usman Saleem el 29 de Mayo de 2015
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 el 29 de Mayo de 2015
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);

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by