how to find sum of square sine wave
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Muhammad Usman Saleem
le 29 Mai 2015
Commenté : Image Analyst
le 30 Mai 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 commentaires
Salaheddin Hosseinzadeh
le 29 Mai 2015
Hi Muhammad,
Give it a try it's fairly easy, its a good practice to learn.
Réponse acceptée
Image Analyst
le 29 Mai 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 commentaires
Image Analyst
le 30 Mai 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.
Plus de réponses (1)
Image Analyst
le 29 Mai 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 commentaires
Joseph Cheng
le 29 Mai 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);
Voir également
Catégories
En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!