Summation of a Series Question
1 回表示 (過去 30 日間)
古いコメントを表示
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
0 件のコメント
回答 (1 件)
Jan
2013 年 9 月 24 日
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.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!