Summation of a Series Question

1 visualización (últimos 30 días)
Big Meech
Big Meech el 24 de Sept. de 2013
Hello,
I first have my square wave that is fine. What I'm having trouble with is graphing my sine wave function. The function is suppose to be equal to: f=0.5+(2/pi)*summation of (1/n)*sin(2+pi*n*t)) from 1 to infinity with n=1,3,5,7....
I have it written I just not sure how I can incorporate the summation. I tried using symsum function but I couldn't get it to work. Also note I already have n set up in my code.
Any advice or help on how I ca do this?
Code
clc;clear all;clf;
x=linspace(0,6,1000);
f=0.5;
for n=1:2:10
% First Plot Square Wave Function
X=[0,0,0.5,0.5,1.0,1.0,1.5,1.5,2.0,2.0,2.5,2.5,3.0,3.0,3.5,3.5,4.0,4.0,4.5,4.5,5.0,5.0,5.5,5.5,6,6];
Y=[0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1,1,0,0,1];
line(X,Y,'color','r','linewidth',2)
grid on;hold on;
f=f+(2/(pi))*(1/n)*sin(2*pi*n*x);
error=mean((abs(f)-1).^2);
plot(x,f,'k','linewidth',2)
title(['Square Wave FS Partial Sum: ',...
'n = ',num2str(n),' Error = ',num2str(error)])
pause
if error<0.01
break
end
clf
end

Respuestas (1)

Jan
Jan el 24 de Sept. de 2013
clear all is still a bad idea, because it deletes all break points, such that you cannot use the debugger anymore. But the debugger would be a really helpful tool to find bugs. To be exact, this is exactly its purpose.
Using "error" as name of a variable causes problems, because this is a very important Matlab function. Such shadowing leads to unexpected behavior frequently.
When you "cannot get symsum to work", post your code and the occurring problems, e.g. the error message of the difference between your expectations and the results. Otherwise we cannot help you to solve the problems.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by