Improving code to reduce run-time
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Hi
I need to improve following code for decreasing run-time.
The main deficiencies are
- The run-time, especially for Ln 137 [ i.e. bc(2) = solve(subs(fd1,[psi(1),X],[bc(1),1/2+sqrt(1/4-(z/h)^2)]),psi(N)) ] is too much.
- In the case of N>10, the
Please hint me
clc;
clear all
tic
N = 10;
L = 5;
h = 1;
n = 1;
md = 1;
P = 0;
rhom = 2702;
rhoc = 3960;
Em = 70e9;
Ec = 380e9;
nu = 0.3;
% X =(y/h+1/2);
sc = 1/h;
syms X z omega
om = sym('om',[1,N]);
om(1) = 0;
phi1 = sym('phi1',[1,N]);
x = zeros(1,N);
A = zeros(2,N,N);
l = sym('l',[1,N]);
df = sym('df',[1,N]);
df1 = sym('df1',[1,N]);
df1o = sym('df1o',[1,N]);
df2 = sym('df2',[1,N]);
psi = sym('psi',[1,N]);
dis = sym('dis',[1,N]);
M = zeros(N-2,N-2);
bc = sym('bc',[1,2]);
mm = sym('mm',[1,N-2]);
F = sym('F',[1,N]);
q = 1:N;
Vc =(X)^n;
Vm = 1-Vc;
rho = rhom*Vm+rhoc*Vc;
E = Em*Vm+Ec*Vc;
G = E/(2*(1+nu));
m0 = int(int(rho/sc,X,1/2-sqrt(1/4-(z/h)^2),1/2+sqrt(1/4-(z/h)^2)),z,-h/2,h/2);
for i=1:N
x(i) = 1/2*(1-cos((i-1)*vpa(pi)/(N-1)));
end
for m=1:N
phi1(m) = (X-x(m));
end
aphi = cumprod(phi1);
for r=1:2
for j=1:N
for m=1:N
if j==m
phi1(m) = 1;
else
phi1(m) = (x(j)-x(m));
end
end
dphi = cumprod(phi1);
l(j) = aphi(N)/((X-x(j))*dphi(N));
for i=1:N
A(r,i,j) = limit(diff(l(j),X,r),X,x(i));
end
end
end
for i=1:N
for j=1:N
df(j) = A(1,i,j)*psi(j);
end
df1o(i) = sum(df);
end
for i=1:N
for j=1:N
df(j) = A(2,i,j)*psi(j);
end
df2(i) = sum(df);
end
for i=1:N
df2(i) = df2(i)*subs(G,X,x(i));
end
for i=1:N
df1(i) = df1o(i)*sc*subs(diff(G,X),X,x(i));
end
for i=1:N
dis(i)= subs((rho*om(1)^2*L^2-md^2*vpa(pi^2)*E)*(L^2*psi(i)+md^2*vpa(pi^2)*(X-1/2)*h),X,x(i));
end
ode = L^4*sc^2*df2+L^4*sc*df1+dis;
for j=1:N-2
for i=1:N-2
M(i,j)= diff(ode(i+1),psi(j+1));
end
end
for j=1:N-2
mm(j) = ode(j+1);
for i=1:N-2
mm(j)= subs(mm(j),psi(i+1),0);
end
end
mat = -(M^(-1)*mm');
df1s = subs(df1o,psi(2:N-1),mat');
fd1 = polyfit(x,df1s,N-1)*(X.^(N-q))';
bc(1) = solve(subs(fd1,X,1/2-sqrt(1/4-(z/h)^2)),psi(1));
bc(2) = solve(subs(fd1,[psi(1),X],[bc(1),1/2+sqrt(1/4-(z/h)^2)]),psi(N));
bc(1) = subs(bc(1),psi(N),bc(2));
F(1) = bc(1);
F(N) = bc(2);
F = subs(F,F(2:N-1), subs(mat',[psi(1),psi(N)],[bc(1),bc(2)]));
f = polyfit(x,F,N-1)*(X.^(N-q))';
INT = int(G*diff(f,X),X,1/2-sqrt(1/4-(z/h)^2),1/2+sqrt(1/4-(z/h)^2))*L^2;
INT = matlabFunction(INT);
INT = integral(INT,-h/2,h/2,'ArrayValued',1);
solve(INT+m0*omega^2*L^2+md^2*vpa(pi^2)*P,omega);
om(2) = abs(ans(1));
lp=1;
while abs(om(lp+1)-om(lp))>1e-10
lp=lp+1;
for i=1:N
dis(i)= subs((rho*om(lp)^2*L^2-md^2*vpa(pi^2)*E)*(L^2*psi(i)+md^2*vpa(pi^2)*(X-1/2)*h),X,x(i));
end
ode = L^4*sc^2*df2+L^4*sc*df1+dis;
ode = subs(ode,[psi(1),psi(N)],[bc(1),bc(2)]);
for j=1:N-2
for i=1:N-2
M(i,j)= diff(ode(i+1),psi(j+1));
end
end
for j=1:N-2
mm(j) = ode(j+1);
for i=1:N-2
mm(j)= subs(mm(j),psi(i+1),0);
end
end
mat = -(M^(-1)*mm');
df1s = subs(df1o,psi(2:N-1),mat');
fd1 = polyfit(x,df1s,N-1)*(X.^(N-q))';
bc(1) = solve(subs(fd1,X,1/2-sqrt(1/4-(z/h)^2)),psi(1));
bc(2) = solve(subs(fd1,[psi(1),X],[bc(1),1/2+sqrt(1/4-(z/h)^2)]),psi(N));
bc(1) = subs(bc(1),psi(N),bc(2));
F(1) = bc(1);
F(N) = bc(2);
F = subs(F,F(2:N-1), mat');
f = polyfit(x,F,N-1)*(X.^(N-q))';
INT = int(G*diff(f,X),X,1/2-sqrt(1/4-(z/h)^2),1/2+sqrt(1/4-(z/h)^2))*L^2;
INT = matlabFunction(INT);
INT = integral(INT,-h/2,h/2,'ArrayValued',1);
solve(INT+m0*omega^2*L^2+md^2*vpa(pi^2)*P,omega);
om(1+lp) = abs(ans(1));
end
toc
1 comentario
Stephen23
el 17 de Abr. de 2019
"I need to improve following code..."
Start by using consistent indentation and code alignment.
Respuestas (0)
Ver también
Categorías
Más información sobre Function Creation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!