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

回答(2 个)

Image Analyst
Image Analyst 2016-9-15

0 个投票

Hint: look up linspace() and sum() in the help.
Walter Roberson
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!

Translated by