Clear Filters
Clear Filters

Info

This question is closed. Reopen it to edit or answer.

how can i fix this error "Subscript indices must either be real positive integers or logicals".in the following program?

1 view (last 30 days)
e1=input('enter the value of ei1') e2=input('enter the value of ei2') w=1.5:0.01:1.6; k1=0.25; k2=0.25; a=0.0005; ne=3.19; r=1.5*10^-6; l=2*pi*r; kn=(2*pi*ne*l)./w; b=sqrt(1-k1); c=sqrt(1-k2); f=sqrt(k1-k2); g=sqrt(-1)*kn*l; ed=e2((c-b*exp(-a.*l-g))./(1-b*c*exp(-a.*l-g)))+e1((-f*exp(a*l/2-g/2))./(1-b*c*exp(-a.*l-g))); y=abs(ed); z=y^2; plot(w,z)
  1 Comment
Jan
Jan on 13 Sep 2013
If you format your code properly, the reading is much easier.
When you post the complete error message, we do not have to guess, where the problem occurs.

Answers (2)

Roger Stafford
Roger Stafford on 13 Sep 2013
Your problem presumably occurs at the line
ed=e2(...) + e1(...);
If you intended e2 and e1 to be vectors, which is how matlab is interpreting them, the quantities inside the parentheses need to be real, positive integers since they are interpreted as indices, whereas you have complex fractional values there.
If you intended them to be some kind of function, your method of using 'input' for that purpose didn't achieve that. What kind of entities do you actually have in mind for e2 and e1 as a user input?

Image Analyst
Image Analyst on 13 Sep 2013
Perhaps you meant:
ed=e2.*((c-b*exp(-a.*l-g))./(1-b*c*exp(-a.*l-g)))+e1.*((-f*exp(a*l/2-g/2))./(1-b*c*exp(-a.*l-g)));
Note, I don't know what values e2 took and what e1 took but for the values I plugged in, ed was complex and then it bombed at the z=y^2 line, so you might need to fix that too.
  2 Comments
ABHISHEK KUMAR
ABHISHEK KUMAR on 13 Sep 2013
sir i have corrected the above mentioned error,but i am getting a straight line in the plot, desired plot is not coming.it should be gaussian beam . either e1=1,e2=0 or e1=0,e2=1. help me to correct this program.
Image Analyst
Image Analyst on 13 Sep 2013
Do what people do in situations like this. Break your equation up into smaller terms and see what each one is like:
e1=0
e2=1
w=1.5:0.01:1.6;
k1=0.25;
k2=0.25;
a=0.0005;
ne=3.19;
r=1.5*10^-6;
l=2*pi*r;
kn=(2*pi*ne*l)./w;
b=sqrt(1-k1);
c=sqrt(1-k2);
f=sqrt(k1-k2);
g=sqrt(-1)*kn*l;
term1 = c-b*exp(-a.*l-g)
term2 = 1-b*c*exp(-a.*l-g)
ed=e2.*((c-b*exp(-a.*l-g))./(1-b*c*exp(-a.*l-g)))+e1.*((-f*exp(a*l/2-g/2))./(1-b*c*exp(-a.*l-g)));
y=abs(ed);
z=y.^2;
plot(w,z)
I don't think the line is straight. I think it's just that you are looking at such a small chunk of the curve that it looks straight over that really really small portion of it.

This question is closed.

Community Treasure Hunt

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

Start Hunting!