Error using imag: too many input arguments

Hi, I get the given error when trying to plot the contour of the given function
syms r x y k z
[ph,r] = meshgrid((0:5:360)*pi/180,0:.5:10);
[X,Y] = pol2cart(ph,r);
Z = X+i*Y;
J = besselj(k,l.*r);
J2 = besselj(k,m.*r);
Y = bessely(k,l.*r);
Y2 = bessely(k,m.*r);
H = besselh(k,r);
F1 = symsum((J).*exp(1i*k*ph),k,-5,5);
F2 = symsum((J2+Y2).*exp(1i.*k.*ph),k,-5,5);
F3 = symsum(H.*exp(1i.*k.*ph),k,-5,5);
pwu = nan(size(F1), 'like', F1);
mask = 0 <= r & r < 0.5;
pwu(mask) = F1(mask);
mask = 0.5 <= r & r < 1;
pwu(mask) = F2(mask);
mask = r >= 1;
pwu(mask) = F3(mask);
U = subs(pwu, {l, m}, {1.5, 3});hold on
contour(X,Y,imag(double(U),30))
axis equal
xlabel('r','FontSize',14);
ylabel('phi','FontSize',14);
The error lies in the 4th last line, however it appears to me as OK. What is wrong here?
thanks!

 採用された回答

Walter Roberson
Walter Roberson 2021 年 6 月 22 日
編集済み: Walter Roberson 2021 年 6 月 22 日

0 投票

Count the bracket level!
contour(X,Y,imag(double(U),30))
1 2 3 2 10
Notice that the ,30 is at nesting level 2 -- inside the imag() call.

7 件のコメント

Sergio Manzetti
Sergio Manzetti 2021 年 6 月 22 日
Ah got it!!
Sergio Manzetti
Sergio Manzetti 2021 年 6 月 22 日
Hi Walter, I did the correction, and got the new code:
syms r x y k z
[ph,r] = meshgrid((0:5:360)*pi/180,0:.5:10);
[X,Y] = pol2cart(ph,r);
Z = X+i*Y;
J = besselj(k,l.*r);
J2 = besselj(k,m.*r);
Y = bessely(k,l.*r);
Y2 = bessely(k,m.*r);
H = besselh(k,r);
F1 = symsum((J).*exp(1i*k*ph),k,-5,5);
F2 = symsum((J2+Y2).*exp(1i.*k.*ph),k,-5,5);
F3 = symsum(H.*exp(1i.*k.*ph),k,-5,5);
pwu = nan(size(F1), 'like', F1);
mask = 0 <= r & r < 0.5;
pwu(mask) = F1(mask);
mask = 0.5 <= r & r < 1;
pwu(mask) = F2(mask);
mask = r >= 1;
pwu(mask) = F3(mask);
U = subs(pwu, {l, m}, {1.5, 3});hold on
contour(X,Y,imag(double(U)),30)
axis equal
xlabel('r','FontSize',14);
ylabel('phi','FontSize',14);
but then I get a new error. "Input argumnets of contour must be numeric"
Walter Roberson
Walter Roberson 2021 年 6 月 22 日
[ph,r] = meshgrid((0:5:360)*pi/180,0:.5:10);
[X,Y] = pol2cart(ph,r);
X and Y are numeric.
Y = bessely(k,l.*r);
but now Y is overwritten with something symbolic.
You need to refer back to the code I posted for you within the last couple of days, as I was careful in the code to avoid this problem.
Sergio Manzetti
Sergio Manzetti 2021 年 6 月 22 日
編集済み: Sergio Manzetti 2021 年 6 月 22 日
Hi Walter, thanks. The code is precisely as you wrote it, but your code was for surface not for level curves. I just copied and pasted your code, and used it with the last three lines of contour instead.
This is your original surface code:
syms k l m
% l=zeta1, m=zeta2
[ph, r] = meshgrid((0:5:360)*pi/180,[0:.05:0.5, 1:.5:10]);
[x, y] = pol2cart(ph,r);
Z = x + 1i*y;
J = besselj(k,l.*r);
J2 = besselj(k,m.*r);
Y = bessely(k,l.*r);
Y2 = bessely(k,m.*r);
H = besselh(k,r);
F1 = symsum((J).*exp(1i*k*ph),k,-5,5);
F2 = symsum((J2+Y2).*exp(1i.*k.*ph),k,-5,5);
F3 = symsum(H.*exp(1i.*k.*ph),k,-5,5);
pwu = nan(size(F1), 'like', F1);
mask = 0 <= r & r < 0.5;
pwu(mask) = F1(mask);
mask = 0.5 <= r & r < 1;
pwu(mask) = F2(mask);
mask = r >= 1;
pwu(mask) = F3(mask);
U = subs(pwu, {l, m}, {1.5, 3});
surf(x, y, real(double(U)))
hold on
surf(x, y, zeros(size(x)))
hold off
Sergio Manzetti
Sergio Manzetti 2021 年 6 月 22 日
Here is your code:
syms k l m
% l=zeta1, m=zeta2
[ph, r] = meshgrid((0:5:360)*pi/180,[0:.05:0.5, 1:.5:10]);
[x, y] = pol2cart(ph,r);
Z = x + 1i*y;
J = besselj(k,l.*r);
J2 = besselj(k,m.*r);
Y = bessely(k,l.*r);
Y2 = bessely(k,m.*r);
H = besselh(k,r);
F1 = symsum((J).*exp(1i*k*ph),k,-5,5);
F2 = symsum((J2+Y2).*exp(1i.*k.*ph),k,-5,5);
F3 = symsum(H.*exp(1i.*k.*ph),k,-5,5);
pwu = nan(size(F1), 'like', F1);
mask = 0 <= r & r < 0.5;
pwu(mask) = F1(mask);
mask = 0.5 <= r & r < 1;
pwu(mask) = F2(mask);
mask = r >= 1;
pwu(mask) = F3(mask);
U = subs(pwu, {l, m}, {1.5, 3});
surf(x, y, real(double(U)))
hold on
surf(x, y, zeros(size(x)))
hold off
Walter Roberson
Walter Roberson 2021 年 6 月 22 日
My code had
[x, y] = pol2cart(ph,r);
Z = x + 1i*y;
Your code has
[X,Y] = pol2cart(ph,r);
Z = X+i*Y;
Notice y compared to Y . This is important because you have
Y = bessely(k,l.*r);
which assigns a symbolic result to Y, and then later when you want to
contour(X,Y,imag(double(U)),30)
then you are using the symbolic Y, not the numeric Y.
You should have just taken my code and changed
surf(x, y, real(double(U)))
hold on
surf(x, y, zeros(size(x)))
hold off
to
contour(x, y, imag(double(U)), 30);
Sergio Manzetti
Sergio Manzetti 2021 年 6 月 22 日
Thanks Water. I will do this immediately!

サインインしてコメントする。

その他の回答 (0 件)

製品

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by