필터 지우기
필터 지우기

Convert sym to double

조회 수: 8 (최근 30일)
Mark
Mark 2022년 6월 16일
댓글: Mark 2022년 6월 16일
I try to find out the DW (The last line of code I showed below). I used sym x y at the beginning because I need to differentiate W with respect to second order x. After the differentiation, I substitute x and y with real value, X and Y. But when I tried to run the code, there is an error indicated "Unable to perform assignment because value of type 'sym' is not convertible to 'double'." Therefore, I am curious that if I can transfer sym to double or if there is alternative method to find out DW. Thank you very much!!
clc
clear
format long
v=0.3;
E=(209e+3)*10^6;
G=E/(2*(1+v));
q=-0.1*10^6;
h=15*10^(-3);
D=(E*h^3)/(12*(1-v^2));
I=(h^3)/12;
a=600*(10^-3);b=2400*(10^-3);
syms x y
c=13
for f = 1:c
k=[3:2:1+2*c];
mn{f}= 1:2:k(f)
end
for f=1:c
len=length(mn{f});
rfa_m=zeros(1,len);
for i=1:len
m=mn{f}(i)
rfa_m(i)= (m.*pi*b)./(2*a)
end
Am=zeros(1,len);
for i=1:len
m=mn{f}(i)
Am(i)=-2*(rfa_m(i)*tanh(rfa_m(i))+2)./((pi^5)*(m^5)*cosh(rfa_m(i)))
end
Bm=zeros(1,len);
for i=1:len
m=mn{f}(i)
Bm(i)=2./((pi^5)*(m^5)*cosh(rfa_m(i)))
end
W=zeros(1,len);
for i=1:len
m=mn{f}(i)
W(i)=(q/(24*D))*(x^4-2*a*x^3+(a^3)*x)+((q*a^4)/D).*sum((Am(i).*cosh(m(i).*pi*y./a)+Bm(i).*(m(i).*pi*y/a).*sinh(m(i).*pi*y./a)).*sin(m(i).*pi*x./a))
end
for i=1:len
m=mn{f}(i)
DW(i)=diff(W(i),x,2)
end
X=600*(10^-3);
Y=0;
DW=subs(DW,{x,y},{X,Y})
end
  댓글 수: 1
KSSV
KSSV 2022년 6월 16일
Your RHS is a syms class, you cannot convert it into double unless you substitute the values of x.

댓글을 달려면 로그인하십시오.

채택된 답변

Paul
Paul 2022년 6월 16일
Hi @Mark,
The first problem with the code is in this area
W=zeros(1,len);
for i=1:len
m=mn{f}(i)
W(i)=(q/(24*D))*(x^4-2*a*x^3+(a^3)*x)+((q*a^4)/D).*sum((Am(i).*cosh(m(i).*pi*y./a)+Bm(i).*(m(i).*pi*y/a).*sinh(m(i).*pi*y./a)).*sin(m(i).*pi*x./a))
end
W is a double array, but the rhs of the assignment to W(i) is a sym because of the use of x and y. Based on the rest fo the code, I suspect it really should be
W=sym(zeros(1,len)); % create a sym vector
for i=1:len
m=mn{f}(i)
W(i)=(q/(24*D))*(x^4-2*a*x^3+(a^3)*x)+((q*a^4)/D).*sum((Am(i).*cosh(m(i).*pi*y./a)+Bm(i).*(m(i).*pi*y/a).*sinh(m(i).*pi*y./a)).*sin(m(i).*pi*x./a))
end
and then W sill be a sym, which can then be diff()ed in the next for loop. But even after that change, another error message popped up, which you'll need to sort out.
  댓글 수: 1
Mark
Mark 2022년 6월 16일
Thank you very much! I have solved the problem

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Formula Manipulation and Simplification에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by