How to code this?
Afficher commentaires plus anciens

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 commentaires
Walter Roberson
le 15 Sep 2016
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
le 15 Sep 2016
The line
grid onhold on
needs to be
grid on
hold on
Réponses (2)
Image Analyst
le 15 Sep 2016
0 votes
Hint: look up linspace() and sum() in the help.
Walter Roberson
le 15 Sep 2016
0 votes
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.
Catégories
En savoir plus sur Mathematics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!