Problem with ploting involving function handle

Hi all, I encounter the following error when I want to plot a function involving function handle with symsum, I cannot figure out the error. My code is as follows. In my matlab 2023b, I receive the following error:
Warning: Error updating FunctionLine.
The following error was reported evaluating the function in FunctionLine update: Undefined function 'symsum' for input arguments of type 'double'.
N= 600
N = 600
sym ii
COMx_k = @(k) symsum(1.*exp(-2*pi*1i*(k/N)*ii), ii,1,N)
COMx_k = function_handle with value:
@(k)symsum(1.*exp(-2*pi*1i*(k/N)*ii),ii,1,N)
%k=0:0.1:300
fplot(COMx_k)
xlabel('k')
ylabel('COMx_k')
Warning: Error in state of SceneNode.
The following error was reported evaluating the function in FunctionLine update: Unrecognized function or variable 'ii'.

 Akzeptierte Antwort

Matt J
Matt J am 13 Okt. 2024
Bearbeitet: Matt J am 15 Okt. 2024
Perhaps this is what you meant?
N= 600;
COMx_k = @(k) real(sum(1.*exp(-2*pi*1i*(k/N).*(1:N)' ),1));
%k=0:0.1:300
fplot(COMx_k,[0,300])
xlabel('k')
ylabel('COMx_k')

6 Kommentare

Tsz Tsun
Tsz Tsun am 13 Okt. 2024
Verschoben: Matt J am 13 Okt. 2024
Thanks a lot, it works!!
Tsz Tsun
Tsz Tsun am 13 Okt. 2024
May I know what does ,1 mean at the end of the syntax? Thanks !
Stephen23
Stephen23 am 13 Okt. 2024
"May I know what does ,1 mean at the end of the syntax?"
The dimension to sum over:
I'm not sure how fplot works in this instance, and that warning sounds ominous. Maybe fplot detects the problem with array inputs and then does only scalar evaluations? The doc page states that:
"The function must accept a vector input argument and return a vector output argument of the same size."
Hmm, "a vector input" is requred in the doc, but the warning is about more general "array inputs".
Furthermore, it looks like COMx_k is supposed to be a DFT-like computation, but can't say for sure w/o more information from @Tsz Tsun.
I suspect that the function is supposed to be (using scalar expansion):
N = 600;
COMx_k = @(k) reshape(real(sum(1.*exp(-2*pi*1i*(reshape(k,1,[])/N) .* (1:N)'),1)),size(k));
Can k really take on non-integer values?
%k=0:0.1:300
%fplot(COMx_k,[0,300])
If this problem is really related to the DFT, then the function should be
COMx_k = @(k) reshape(real(sum(1.*exp(-2*pi*1i*(reshape(k,1,[])/N) .* (0:N-1)'),1)),size(k));
But this is all just speculation w/o more information from the OP as to the what COMx_k is supposed to represent mathematically.
Tsz Tsun
Tsz Tsun am 15 Okt. 2024
Yes, in fact I am trying to do a DFT. I just notice that there is a function package called fft(X) in matlab, but I am not sure if fft(X) is doing the job of DFT.
fft() does a two-sided numeric discrete fourier transform.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Walter Roberson
Walter Roberson am 13 Okt. 2024
sym ii
That is equivalent to
ans = sym('ii');
which creates the symbolic symbol ii but throws away the reference to the symbolic symbol.
What you probably wanted was
N= 600
syms ii
COMx_k = @(k) symsum(1.*exp(-2*pi*1i*(k/N)*ii), ii,1,N)
fplot(COMx_k)
xlabel('k')
ylabel('COMx_k')
However, this takes a fair amount of time to plot !!

1 Kommentar

@(k) symsum(1.*exp(-2*pi*1i*(k/N)*ii), ii,1,N)
That is complex-valued when k is not an integer, and is 0 when k is an integer.

Melden Sie sich an, um zu kommentieren.

Produkte

Version

R2023b

Gefragt:

am 13 Okt. 2024

Bearbeitet:

am 15 Okt. 2024

Community Treasure Hunt

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

Start Hunting!

Translated by