# How do I plot a summation equation?

12 views (last 30 days)
Nathan on 28 Sep 2022
Commented: Walter Roberson on 28 Sep 2022
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)
Warning: Imaginary parts of complex X and/or Y arguments ignored.

Walter Roberson on 28 Sep 2022
Edited: Walter Roberson on 28 Sep 2022
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 CommentsShowHide 1 older comment
Walter Roberson on 28 Sep 2022
Though I just noticed you sum over m -50 to +50. Same argument, you are going to have small imbalances.

### Categories

Find more on Graphics Performance in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!