Convert sym to double

5 次查看(过去 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.

更多回答(0 个)

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by