Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
help me plot this code -- urgency

Subject: help me plot this code -- urgency

From: maia

Date: 24 Jan, 2013 21:57:08

Message: 1 of 4

hi
i want plot p(K) in this program
when i write the code it give me error
i don't know why
 
and i don't know what i should do
 
i need it very soon in fact in 1 day
 
---------------------------------------------------------
thank you very much
and
sorry in advance for my poor English

-----------------------------------------------------------

my code
 -----------------------------------------------------------

close all
clear
clc
syms k r n teta z
n=0:1:5;
d=.8;
rs=1;
re=1.3-d;
sum=0;
for n=1:4
pin= besseli(n,k*r);
pin1=besselk(n,k*re);
pin2=((1-eps)*besseli(n,k*re).*besselk(n,k*rs).*diff(besselk(n,k*rs))).*(1./(eps*diff(besselk(n,k*rs)).*besseli(n,k*rs)-besselk(n,k*rs).*diff(besseli(n,k*rs))));
pin3=2*pin2.*pin1.*pin.*cos(n.*teta);
sum=sum+pin3;
end
teta=0:1:360;
k=0:.1:10;
r=0:(re/length(k)):re;
[X Y Z]=ndgrid(teta,r,k);
pk=cos(Z.*X).*sum;
plot(pk);

Subject: help me plot this code -- urgency

From: Roger Stafford

Date: 24 Jan, 2013 22:54:08

Message: 2 of 4

"maia " <antonietta.arista@yahoo.com> wrote in message <kdsank$br5$1@newscl01ah.mathworks.com>...
> syms k r n teta z
> n=0:1:5;
> ........
> pin2=((1-eps)*besseli(n,k*re).*besselk(n,k*rs).*diff(besselk(n,k*rs))).*(1./(eps*diff(besselk(n,k*rs)).*besseli(n,k*rs)-besselk(n,k*rs).*diff(besseli(n,k*rs))));
> pin3=2*pin2.*pin1.*pin.*cos(n.*teta);
\- - - - - - - - - - -
  In your expression for 'pin2' you have both besselk(n,k*rs) and diff(besselk(n,k*rs)), but these are of different sizes since 'diff' reduces the length by one and an error message would be the result. Also I doubt if 'besseli' and 'besselk' will accept symbolic variables such as k.

  One other point. It is inadvisable to use 'sum' as the name of a variable since that conflicts with the reserved name of matlab's 'sum' function.

Roger Stafford

Subject: help me plot this code -- urgency

From: maia

Date: 25 Jan, 2013 09:25:07

Message: 3 of 4

"Roger Stafford" wrote in message <kdse2g$nv3$1@newscl01ah.mathworks.com>...
> "maia " <antonietta.arista@yahoo.com> wrote in message <kdsank$br5$1@newscl01ah.mathworks.com>...
> > syms k r n teta z
> > n=0:1:5;
> > ........
> > pin2=((1-eps)*besseli(n,k*re).*besselk(n,k*rs).*diff(besselk(n,k*rs))).*(1./(eps*diff(besselk(n,k*rs)).*besseli(n,k*rs)-besselk(n,k*rs).*diff(besseli(n,k*rs))));
> > pin3=2*pin2.*pin1.*pin.*cos(n.*teta);
> \- - - - - - - - - - -
> In your expression for 'pin2' you have both besselk(n,k*rs) and diff(besselk(n,k*rs)), but these are of different sizes since 'diff' reduces the length by one and an error message would be the result. Also I doubt if 'besseli' and 'besselk' will accept symbolic variables such as k.
>
> One other point. It is inadvisable to use 'sum' as the name of a variable since that conflicts with the reserved name of matlab's 'sum' function.
>
> Roger Stafford
so i should use meshgrid?

Subject: help me plot this code -- urgency

From: Roger Stafford

Date: 25 Jan, 2013 22:13:07

Message: 4 of 4

"maia " <antonietta.arista@yahoo.com> wrote in message <kdsank$br5$1@newscl01ah.mathworks.com>...
> hi
> i want plot p(K) in this program
> when i write the code it give me error
> i don't know why
>
> and i don't know what i should do
>
> i need it very soon in fact in 1 day
>
> ---------------------------------------------------------
> thank you very much
> and
> sorry in advance for my poor English
>
> -----------------------------------------------------------
>
> my code
> -----------------------------------------------------------
>
> close all
> clear
> clc
> syms k r n teta z
> n=0:1:5;
> d=.8;
> rs=1;
> re=1.3-d;
> sum=0;
> for n=1:4
> pin= besseli(n,k*r);
> pin1=besselk(n,k*re);
> pin2=((1-eps)*besseli(n,k*re).*besselk(n,k*rs).*diff(besselk(n,k*rs))).*(1./(eps*diff(besselk(n,k*rs)).*besseli(n,k*rs)-besselk(n,k*rs).*diff(besseli(n,k*rs))));
> pin3=2*pin2.*pin1.*pin.*cos(n.*teta);
> sum=sum+pin3;
> end
> teta=0:1:360;
> k=0:.1:10;
> r=0:(re/length(k)):re;
> [X Y Z]=ndgrid(teta,r,k);
> pk=cos(Z.*X).*sum;
> plot(pk);
- - - - - - - - - - -
  I will list various things that worry me about your code:

1) You have listed k, r, n, and teta as symbolic, and you seem to expect that pin, pin1, pin2, pin3, and sum will all be able to operate in symbolic form. I am not sure that will work, especially since you have also used the same designations of these variables for numerical values.

4) In diff(besselk(n,k*rs)) and diff(besseli(n,k*rs)) you apparently expect the symbolic toolbox to know the derivatives of the two modified Bessel functions, but are you sure it does? If it doesn't, you need the formula for them which you can find in Wikipedia at

 http://en.wikipedia.org/wiki/Bessel_function

(Yesterday I thought the diff was meant to be numerical differencing based on the line "n=0:1:5;" which accounts for my remark about size discrepancy then.)

3) You have supposedly created a symbolic 'sum' which bears no relation to a 3D array and yet you seem to expect the line "pk=cos(Z.*X).*sum;" to treat it as such with the n, k, and teta in accordance with the ndgrid which you have formed. That cannot possibly work as it stands.

2) You have created three dimensional arrays X, Y, Z and from them pk which you placed in plot. I don't think plot knows how to deal with 3D arrays. Matlab has no good method for displaying a quantity as a function of three independent variables. It would need four dimensions to work with and our world is unfortunately only three-dimensional.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us