i want to plot a square wave
조회 수: 7 (최근 30일)
이전 댓글 표시
the equation is f(t)=(4/k*pi)sin(2*pi*k*t) and k is odd number
댓글 수: 1
Star Strider
2015년 10월 17일
Please do not post duplicate Questions: http://www.mathworks.com/matlabcentral/answers/249162-i-want-to-plot-a-square-wave
답변 (1개)
Geoff Hayes
2015년 10월 17일
Abduladeam - in order to get the square wave, you will have to sum over the odd k (which is missing from your above equation). Also, you need to divide the 4 by (k*pi). Try the following code
freq = 1;
func = @(k,t)(4/(k*pi))*sin(2*pi*k*t*freq);
t = linspace(0,1,500);
k = 1;
% create a vector for k==1 across all t
fkt = func(k,t);
% now sum over the odd numbers
upperBound = 100;
for k=3:2:upperBound
fkt = fkt + func(k,t);
end
plot(t,fkt);
Adjust the freq to increase the frequency of the wave, and increase the upperBound in order to improve upon the square wave's approximation.
댓글 수: 2
Geoff Hayes
2015년 10월 17일
I'm not sure I understand your question then. Can you not use the above to code for your work?
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!