problem in plot for two function

57 visualizaciones (últimos 30 días)
george veropoulos
george veropoulos el 8 de Nov. de 2024 a las 22:22
Comentada: Walter Roberson el 9 de Nov. de 2024 a las 17:34
Hi
i want to plot two function in the same axis
i rreceive to message Escattheory(rho,phi))
Error using plot
Data must be numeric, datetime, duration, categorical, or an array convertible
to double.
function y = Escattheory(rho,phi)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
syms k
global k0
global ra
Z1=k0.*rho
Z2=k0.*ra;
scale=1;
factor1=besselh(k,2,Z1).*besselj(k,Z2)./besselh(k,2,Z2);
factor2=(-1i).^k.*cos(k.*phi);
factor=factor1.*factor2.*en(k);
y=vpa(symsum(factor,k,0,inf),3)
end
and for the second i receive the message
Unable to perform assignment because the indices on the left side are not
compatible with the size of the right side.
Error in Escat (line 15)
RR(jj)=sqrt((x-xx(jj)).^2+(y-yy(jj)).^2);
Error in mas_closett_linsolve_2_2 (line 80)
plot(rho,abs(Escat(x,y)))
function y = Escat(x,y)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
global Is
global Nc
global k0
global Z0
global a
global Is
sum=0;
for jj=1:Nc
xx(jj)=a.*cos(2.*pi.*(jj-1)./Nc);
yy(jj)=a.*sin(2*pi.*(jj-1)./Nc);
RR(jj)=sqrt((x-xx(jj)).^2+(y-yy(jj)).^2);
sum=sum-Is(jj).*k0.*Z0.*besselh(0,2,k0.*RR(jj))./4;
end
y=sum;
end
what happen ??
thank you
George
the main code is
clear all
clc
global ra
global N
global a
global f
global Is
global N
global Nc
global k0
global Z0
global ra
global a
Z0=120.*pi;
c0=3e8;
f= input('Give the freuency = ');
%mumber of auxiliary source
N=input('Give the total number of the AS : ');
Nc=input('Give the the test point: ');
ra=input('Give the radius of cylinder : ');
a=input('Give the inside auxilary source radious : ');
lambda=c0./f;
x=zeros(1,N);
y=zeros(1,N);
xc=zeros(1,Nc);
yc=zeros(1,Nc);
k0=2*pi./lambda;
for j=1:N
for i=1:Nc
x(j)=a.*cos(2*pi.*(j-1)./N);
y(j)=a.*sin(2*pi.*(j-1)./N);
xc(i)=ra.*cos(2*pi.*(i-1)./Nc);
yc(i)=ra.*sin(2*pi.*(i-1)./Nc);
R(i,j)=sqrt((x(j)-xc(i)).^2+(y(j)-yc(i)).^2);
end
end
WI=zeros(N,N);
Source=zeros(1,N);
Is=zeros(1,N);
%N for the matching points
for j=1:N
% nomizo einai i sinartisi Electricfieldz ousiastika edo pairnei to kuma diegersis
for i=1:Nc
WI(i,j)=(1./4).*k0.*Z0.*besselh(0,2,k0.*R(i,j));
Source(i) =Efieldin(xc(i),yc(i));
end
W = linsolve(WI,Source');
end
for i=1:N
Is(i)=W(i);
end
for k=1:10.*N
xc(k)=ra.*cos(2*pi.*(k-1)./(10.*Nc));
yc(k)=ra.*sin(2.*pi.*(k-1)./(10.*Nc));
%z(k)=abs(Escat(xc(k),yc(k))+Efieldin(xc(k),yc(k)));
z(k)=abs(Escat(xc(k),yc(k)));
final=z';
end
phi=pi;
rho=0:0.1:ra
%plot(rho, Escattheory(rho,phi));
hold on
x=rho.*cos(phi);
y=rho.*sin(phi);
plot(rho,abs(Escat(x,y)))
xlabel('$\rho$','Interpreter','latex')
ylabel('$|E_{s}|$','Interpreter','latex' )
hold off

Respuesta aceptada

Walter Roberson
Walter Roberson el 8 de Nov. de 2024 a las 22:45
function y = Escattheory(rho,phi)
%...
y=vpa(symsum(factor,k,0,inf),3)
Regardless of anything else, because of the vpa(), Escattheory is going to be returning something symbolic.
%plot(rho, Escattheory(rho,phi));
plot() cannot plot anything symbolic. plot() is restricted to numeric, datetime, duration, categorical, or an array convertible to double. plot() does not automatically convert symbolic to double.
phi=pi;
rho=0:0.1:ra
rho will be a vector.
x=rho.*cos(phi);
y=rho.*sin(phi);
Because rho is a vector and you have rho.* in the code, x and y will end up as vectors.
plot(rho,abs(Escat(x,y)))
vector x and vector y will be passed into Escat
function y = Escat(x,y)
%...
RR(jj)=sqrt((x-xx(jj)).^2+(y-yy(jj)).^2);
x and y are vectors. vector x minus scalar xx(jj) value gives a vector result; likewise vector y minus scalar yy(jj) gives a vector result. As a result, the right hand side is calculating a vector. The result of the vector calculation is being assigned to the scalar location RR(jj) which is not going to fit.
sum=0;
We firmly recommend against naming a variable sum. It happens a lot that people use a variable named sum and then also want to use the function sum in the same section of code. Also, sum is such a common function, that people who glance at the code are likely to misread the code as if it is referring to function sum instead of variable sum . It is our job as programmers to write code that is not open to easy misinterpretation.
  2 comentarios
george veropoulos
george veropoulos el 9 de Nov. de 2024 a las 8:07
Movida: Voss el 9 de Nov. de 2024 a las 8:09
thank you
how can solve this problem ?
Walter Roberson
Walter Roberson el 9 de Nov. de 2024 a las 17:34
Well, there are two possibilities:
  1. The results of the symsum() are something that can be computed numerically (without convergence problems.) In this case, instead of y=vpa(symsum(factor,k,0,inf),3) you would use y=double(symsum(factor,k,0,inf),3)
  2. OR, the results of the symsum() are something that cannot be computed numerically without convergence problems. In this case, there is nothing you can do that will make plotting work.
As for the other problem:
If you examine your code
for jj=1:Nc
xx(jj)=a.*cos(2.*pi.*(jj-1)./Nc);
yy(jj)=a.*sin(2*pi.*(jj-1)./Nc);
RR(jj)=sqrt((x-xx(jj)).^2+(y-yy(jj)).^2);
sum=sum-Is(jj).*k0.*Z0.*besselh(0,2,k0.*RR(jj))./4;
end
you will note that you never use xx(jj) or yy(jj) or RR(jj) outside of the loop. There is no point in remembering their values. You might as well use
for jj=1:Nc
xx=a.*cos(2.*pi.*(jj-1)./Nc);
yy=a.*sin(2*pi.*(jj-1)./Nc);
RR=sqrt((x-xx).^2+(y-yy).^2);
sum=sum-Is(jj).*k0.*Z0.*besselh(0,2,k0.*RR)./4;
end
and this way you avoid all issues about assigning the wrong number of elements.

Iniciar sesión para comentar.

Más respuestas (0)

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by