How do I plot a summation equation?
5 次查看(过去 30 天)
显示 更早的评论
Hi,
I'm trying to plot a summation equation from -50 to 50:
The range for the plot is k = +- 3 / delta x.
delta x isn't defined in the question but I chose 0.5.
I tried this but the graph doesn't seem to look right.
What is wrong and how do I fix this?
% define constant values below
xc = 0.5 ;
% Make m a vector
m = -50:50;
% Given function
h = @(k) xc.*exp(-i.*2.*pi.*k.*m.*xc) ;
k = -3. /xc : 1. /xc : 3. /xc ; % possible k values
p = zeros(size(k)) ; % save the sum for eac function
for i = 1:length(k)
v = h(k(i)) ;
p(i) = sum(v) ;
end
plot(k,p)
0 个评论
回答(1 个)
Walter Roberson
2022-9-28
编辑:Walter Roberson
2022-9-28
You are summing exp() of values that are purely imaginary and are mostly not integer multiples of π. The individual entries are going to be mostly complex values (except at the occasional π multiple.)
Your exp() values include negative and positive coefficients. Is it possible that the imaginary parts cancel?
If each exp(1i*K) term is exactly balanced by a corresponding -K and there is no round off error then the imaginary components would cancel over the sum.
k = -3. /xc : 1. /xc : 3. /xc ; % possible k values
Will that definitely exactly balance positive and negative? No, in the general case you need to expect round-off error as 1/xc accumulates. And you should not assume that sin(x) will exactly balance sin(-x), especially as you are not pairing them up. 0.1+0.2-0.3 is not exactly 0 in binary floating point.
So the sum will typically have a small imaginary component.
2 个评论
Walter Roberson
2022-9-28
Though I just noticed you sum over m -50 to +50. Same argument, you are going to have small imbalances.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!