How do I plot a summation equation?

조회 수: 4 (최근 30일)
Nathan
Nathan 2022년 9월 28일
댓글: Walter Roberson 2022년 9월 28일
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.

답변 (1개)

Walter Roberson
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
Nathan
Nathan 2022년 9월 28일
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 2022년 9월 28일
Though I just noticed you sum over m -50 to +50. Same argument, you are going to have small imbalances.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by