How do I plot a summation equation?

6 visualizaciones (últimos 30 días)
Nathan
Nathan el 28 de Sept. de 2022
Comentada: Walter Roberson el 28 de Sept. de 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.

Respuestas (1)

Walter Roberson
Walter Roberson el 28 de Sept. de 2022
Editada: Walter Roberson el 28 de Sept. de 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 comentarios
Nathan
Nathan el 28 de Sept. de 2022
Does my code look at least correct tho?
I tried with -10 to 10 for the sum, and it gave me something that I feel like is correct but aren't so sure:
Walter Roberson
Walter Roberson el 28 de Sept. de 2022
Though I just noticed you sum over m -50 to +50. Same argument, you are going to have small imbalances.

Iniciar sesión para comentar.

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by