필터 지우기
필터 지우기

how to find sum of square sine wave

조회 수: 4 (최근 30일)
Muhammad Usman Saleem
Muhammad Usman Saleem 2015년 5월 29일
댓글: Image Analyst 2015년 5월 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
Muhammad Usman Saleem 2015년 5월 29일
@thanks Image.. next time i shall take care of it.. Answer my question.. please
Salaheddin Hosseinzadeh
Salaheddin Hosseinzadeh 2015년 5월 29일
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
Muhammad Usman Saleem 2015년 5월 30일
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
Muhammad Usman Saleem 2015년 5월 29일
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