How to code this?
이전 댓글 표시

I want to code and graph(xlabel:n, ylaberl:epsilon).
I study matlab now first time.
I search everything but I can't solve it.
I think below code helps me, but I don't know how to use it.
time=linspace(-pi,pi,1000);
partial_sum=0;
%Complex Function represented in terms of time and amplitude value
t=[-pi,-pi,0,0,pi,pi];
value=[0,-1,-1,1,1,-1];
handle1=line(t,value,'color','r','linewidth',2);
grid onhold on
axis([-pi pi -1.5 1.5])
%Since the given complex function exhibits odd periodic extension
%only Bn term is valid with n=1,3,5,...
for n=1:2:200 %Odd terms to consider for partial sums
%Plot 1 period of the given function
partial_sum=partial_sum+(4/(n*pi))*sin(n*time); %Fourier Series Expansion using Sine terms
error=mean((abs(partial_sum)-1).^2); %Error Criteria
handle2=plot(time,partial_sum,'k','linewidth',2);
title(['Square Wave Partial Sum: n = ',num2str(n),' Error = ',num2str(error)])
pause
set(handle2,'Visible','off');
if error<0.01
break
end
end
댓글 수: 4
Walter Roberson
2016년 9월 15일
You should really avoid naming a variable error because error is a very important MATLAB routine. At the very least you are going to confuse people reading the code.
Walter Roberson
2016년 9월 15일
The line
grid onhold on
needs to be
grid on
hold on
답변 (2개)
Image Analyst
2016년 9월 15일
0 개 추천
Hint: look up linspace() and sum() in the help.
Walter Roberson
2016년 9월 15일
0 개 추천
Tell MATLAB you want to create a new script. Paste the above code into it, and fix the problems I noted in the comments above. Then in the editor, click on the Run button. A graph will be produced, and the bottom of the command area will tell you to press any key to continue. Press return and the graph will be updated. Press return again and the graph will be updated again. Keep pressing return until the bottom of the command area no longer tells you to press any key to continue.
That is how you use that code.
카테고리
도움말 센터 및 File Exchange에서 Mathematics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!