Why did this error message appear?

192 views (last 30 days)
Jason Chen
Jason Chen on 14 Nov 2021
Commented: Jason Chen on 15 Nov 2021
Unable to perform assignment because value of type 'sym' is not convertible to 'double'.
line10.Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to
substitute values for variables.
  1. m=600:1200;
  2. r=ones(20,601);t=ones(20,601);
  3. n0=1;
  4. n1=1.75;d1=125;
  5. n2=2.5;d2=87.5;
  6. n3=1.45;
  7. k=2*pi./m;
  8. e1=n1*d1*k;e2=n2*d2*k;
  9. x1= sym('x1',[1,601]);
  10. r(1,:)=(n0+n1)/(2*n1)+(n1-n0)/(2*n1)*x1;
  11. t(1,:)=(n1-n0)/(2*n1)+(n1+n0)/(2*n1)*x1;
  12. for i=2:20
  13. if rem(i,2)==0
  14. t(i,:)=(n1+n2)/(2*n2)*t(i-1)*exp(2*e1*1i)+(n1-n2)/(2*n2)*r(i-1)*exp(-2*e1*1i);
  15. r(i,:)=(-n1+n2)/(2*n2)*t(i-1)*exp(2*e1*1i)+(n2+n1)/(2*n2)*r(i-1)*exp(-2*e1*1i);
  16. else
  17. t(i,:)=(n1+n2)/(2*n1)*t(i-1)*exp(2*e2*1i)+(n2-n1)/(2*n1)*r(i-1)*exp(-2*e2*1i);
  18. r(i,:)=(-n2+n1)/(2*n1)*t(i-1)*exp(2*e2*1i)+(n2+n1)/(2*n1)*r(i-1)*exp(-2*e2*1i);
  19. end
  20. end
  21. solve(r(20,:),x);
  22. T=t(20,:).*conj(t(20,:))*n3;
  23. plot(m,T);

Answers (1)

Steven Lord
Steven Lord on 14 Nov 2021
Since r is a double array, anything that you want to assign into it must be a double array or convertible into a double array.
In this example:
r = 1:10
r = 1×10
1 2 3 4 5 6 7 8 9 10
syms x y
z = x.^2+y.^2
z = 
what is the double precision value of z? Without having values for x and y we cannot convert z into a double array, and so trying to store z inside r won't work.
r(1) = z
Unable to perform assignment because value of type 'sym' is not convertible to 'double'.

Caused by:
Error using symengine
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.
You could make r a symbolic array from the start, or you could substitute values for x and y into z (thus making z a plain number.)
rsym = sym(1:10)
  1 Comment
Jason Chen
Jason Chen on 15 Nov 2021
I try to make r a symbolic array from the start,but how can i make it a complex array at last and plot a image?
error symengine
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to
substitute values for variables.
sym/double (line 709 )
Xstr = mupadmex('symobj::double', S.s, 0);
(line 26 )
double(t);
  1. m=600:1200;
  2. r=sym('r',[20,601]);
  3. t=sym('t',[20,601]);
  4. n0=1;
  5. n1=1.75;d1=125;
  6. n2=2.5;d2=87.5;
  7. n3=1.45;
  8. k=2*pi./m;
  9. e1=n1*d1*k;e2=n2*d2*k;
  10. x1= sym('x1',[1,601]);
  11. r(1,:)=(n0+n1)/(2*n1)+(n1-n0)/(2*n1)*x1(1,:);
  12. t(1,:)=(n1-n0)/(2*n1)+(n1+n0)/(2*n1)*x1(1,:);
  13. for i=2:20
  14. if rem(i,2)==0
  15. t(i,:)=(n1+n2)/(2*n2)*t(i-1)*exp(2*e1*1i)+(n1-n2)/(2*n2)*r(i-1)*exp(-2*e1*1i);
  16. r(i,:)=(-n1+n2)/(2*n2)*t(i-1)*exp(2*e1*1i)+(n2+n1)/(2*n2)*r(i-1)*exp(-2*e1*1i);
  17. else
  18. t(i,:)=(n1+n2)/(2*n1)*t(i-1)*exp(2*e2*1i)+(n2-n1)/(2*n1)*r(i-1)*exp(-2*e2*1i);
  19. r(i,:)=(-n2+n1)/(2*n1)*t(i-1)*exp(2*e2*1i)+(n2+n1)/(2*n1)*r(i-1)*exp(-2*e2*1i);
  20. end
  21. end
  22. for i=1:601
  23. solve(r(20,i),x1(1,i));
  24. end
  25. double(t);
  26. T=t(20,:).*conj(t(20,:))*n3;
  27. plot(m,T);

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!

Translated by