Conversion to cell from double is not possible in for loop

2 views (last 30 days)
I am trying to write a code. Following is the part of code relative to query
Energy= En + (AV*q) ;
Widths = [ 10*AA, 10*AA, 10*AA ] ;
Entries= 3;
xmax=0;
for n=1: Entries
xmax = xmax + Widths ( n ) ;
end
Slope = AV*eV / xmax ;
Pots = [ 0.7*eV , 0*eV , 0.7*eV ] ;
Pot0Left = AV*q ;
Pot0Right= 0*eV ;
Precision = Prec ;
kr = sqrt(2*me*(Pots(1) + Pot0Left - Energy))/hbar ;
kl = sqrt (2*me*(Energy-Pot0Left))/hbar ;
SysMat(1,1)=1- 1i*kr/kl ; SysMat (1,2)=1+ 1i*kr/kl ;
SysMat(2,1)=1+ 1i*kr/kl ; SysMat (2,2)=1- 1i*kr/kl ;
SysMat=0.5*SysMat ;
for n=1: Entries
xposl = 0 ;
for j =1 :(n-1)
xposl = xposl + Widths (j) ;
end
DivW = Widths(n)/Precision ;
potl = Pots (n) + AV*q - xposl * Slope ;
for l=1:Precision
potr = potl - DivW * Slope ;
if (mod(n,2)==1)
a = sqrt(2*me*(potl - Energy))/hbar ;
M(1,1) = exp(-a*DivW);
M(1,2) = 0 ;
M(2,1) = 0 ;
M(2,2) = exp(a*DivW) ;
else
k = sqrt (2*me*(Energy - potl))/hbar ;
M(1,1) = exp( -1i*k*DivW ) ; M( 1 , 2 ) = 0 ;
M(2,1) = 0 ; M(2,2) = exp( 1i*k*DivW ) ;
end
SysMat = M*SysMat ;
if (l<Precision )
if (mod(n,2)==1)
k1= sqrt(2*me*(potr - Energy))/hbar ;
k2= sqrt(2*me*(potl - Energy))/hbar ;
else
k1= sqrt (2*me*(Energy - potr))/hbar ;
k2= sqrt (2*me*(Energy - potl))/hbar ;
end
M(1,1)=k2+k1 ; M(1,2) = k2-k1 ;
M(2,1)=k2-k1 ; M(2,2) = k2+k1 ;
SysMat = (0.5*M*SysMat)/k2 ;
potl = potr ;
end
end
But at the line
M(1,1) = exp(-a*DivW);
It shows the error
Conversion to cell from double is not possible.
Error in rtTransm (line 56)
M(1,1) = exp(-a*DivW);
In the workspace it shows that M is a 2X2 cell while I need it to be 2X2 double. While the other matrix SysMat is a 2X2 double.
I am varying the variable En from 0 to 10 in steps of 0.01 which in turn is varying the variable a which I think is causing the error as DivW is constant.
Any help in correcting the code is appreciated.
  5 Comments
AYUSH KUMAR
AYUSH KUMAR on 25 Mar 2019
Since I am varying En = 0:0.01:10;
and a depends on En it is an array 0f 1X1001 double
while DivW is a constant.
Walter Roberson
Walter Roberson on 25 Mar 2019
Perhaps you should be referring to a(n) instead of to the entire vector a ?

Sign in to comment.

Answers (0)

Categories

Find more on Data Type Conversion in Help Center and File Exchange

Products


Release

R2016a

Community Treasure Hunt

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

Start Hunting!