Solving equation with for loop is slow

2 views (last 30 days)
I have an equation (5th order Polynomial) and I have to solve it every time for different variables A,B and Coeff as written down. And the coefficient are quite alot (100000) I had no other way than using the for loop to do it but it is incredibely slow. Could anyone please give me a suggestion to make it faster or if there is another way to solve the equation faster.
This is my code:
tCounter = zeros(length(A),1);
for i = 1:length(A)
syms t
Check = (isnan(B(i,1))==1);
if Check ==1
tCounter(i) = NaN;
Equation = -(Coeff(21).*((B(i,2) + t*A(i,2)).^5) + (Coeff(20).*((B(i,2) + t*A(i,2)).^4)).*(B(i,1) + t*A(i,1)) + Coeff(19).*((B(i,2) + t*A(i,2)).^4) + (Coeff(18).*((B(i,2) + t*A(i,2)).^3)).*((B(i,1) + t*A(i,1)).^2) + (Coeff(17).*((B(i,2) + t*A(i,2)).^3)).*(B(i,1) + t*A(i,1)) + Coeff(16).*((B(i,2) + t*A(i,2)).^3) + (Coeff(15).*((B(i,2) + t*A(i,2)).^2)).*((B(i,1) + t*A(i,1)).^3) + (Coeff(14).*((B(i,2) + t*A(i,2)).^2)).*((B(i,1) + t*A(i,1)).^2) + (Coeff(13).*((B(i,2) + t*A(i,2)).^2)).*(B(i,1) + t*A(i,1)) + Coeff(12).*((B(i,2) + t*A(i,2)).^2) + (Coeff(11).*((B(i,2) + t*A(i,2)))).*((B(i,1) + t*A(i,1)).^4) + (Coeff(10).*(B(i,2) + t*A(i,2))).*((B(i,1) + t*A(i,1)).^3) + (Coeff(9).*(B(i,2) + t*A(i,2))).*((B(i,1) + t*A(i,1)).^2) + (Coeff(8).*(B(i,2) + t*A(i,2))).*((B(i,1) + t*A(i,1))) + (Coeff(7).*(B(i,2) + t*A(i,2))) + Coeff(6).*((B(i,1) + t*A(i,1)).^5) + Coeff(5).*((B(i,1) + t*A(i,1)).^4) + Coeff(4).*((B(i,1) + t*A(i,1)).^3) + Coeff(3).*((B(i,1) + t*A(i,1)).^2) + Coeff(2).*(B(i,1) + t*A(i,1)) + Coeff(1)) + Thickness - (B(i,3) + t*A(i,3));
t = solve(Equation,t);
t = double (t);
t(imag(t) ~= 0) = [];
t(t<0) = [];
t = min(t);
tCounter(i) = t;
Many thanks in advance

Accepted Answer

Walter Roberson
Walter Roberson on 14 Oct 2013
solve() generically outside of the loop and then subs() or matlabFunction() to get code executed for each loop instance.
t = roots( [Coeff(6)*A(i, 1)^5+Coeff(11)*A(i, 1)^4*A(i, 2)+Coeff(15)*A(i, 1)^3*A(i, 2)^2+Coeff(18)*A(i, 1)^2*A(i, 2)^3+Coeff(20)*A(i, 1)*A(i, 2)^4+Coeff(21)*A(i, 2)^5,
(5*Coeff(6)*B(i, 1)+Coeff(11)*B(i, 2)+Coeff(5))*A(i, 1)^4+(2*(2*Coeff(11)*B(i, 1)+Coeff(15)*B(i, 2)+(1/2)*Coeff(10)))*A(i, 1)^3*A(i, 2)+(3*Coeff(15)*B(i, 1)+3*Coeff(18)*B(i, 2)+Coeff(14))*A(i, 1)^2*A(i, 2)^2+(2*Coeff(18)*B(i, 1)+4*Coeff(20)*B(i, 2)+Coeff(17))*A(i, 1)*A(i, 2)^3+(Coeff(20)*B(i, 1)+5*Coeff(21)*B(i, 2)+Coeff(19))*A(i, 2)^4,
(10*Coeff(6)*B(i, 1)^2+Coeff(15)*B(i, 2)^2+(4*Coeff(11)*B(i, 2)+4*Coeff(5))*B(i, 1)+Coeff(10)*B(i, 2)+Coeff(4))*A(i, 1)^3+(3*(2*Coeff(11)*B(i, 1)^2+Coeff(18)*B(i, 2)^2+(2*Coeff(15)*B(i, 2)+Coeff(10))*B(i, 1)+(2/3)*Coeff(14)*B(i, 2)+(1/3)*Coeff(9)))*A(i, 1)^2*A(i, 2)+(3*Coeff(15)*B(i, 1)^2+6*Coeff(20)*B(i, 2)^2+(6*Coeff(18)*B(i, 2)+2*Coeff(14))*B(i, 1)+3*Coeff(17)*B(i, 2)+Coeff(13))*A(i, 1)*A(i, 2)^2+(Coeff(18)*B(i, 1)^2+10*Coeff(21)*B(i, 2)^2+(4*Coeff(20)*B(i, 2)+Coeff(17))*B(i, 1)+4*Coeff(19)*B(i, 2)+Coeff(16))*A(i, 2)^3,
(10*Coeff(6)*B(i, 1)^3+Coeff(18)*B(i, 2)^3+(6*Coeff(11)*B(i, 2)+6*Coeff(5))*B(i, 1)^2+Coeff(14)*B(i, 2)^2+(3*Coeff(15)*B(i, 2)^2+3*Coeff(10)*B(i, 2)+3*Coeff(4))*B(i, 1)+Coeff(9)*B(i, 2)+Coeff(3))*A(i, 1)^2+(4*Coeff(11)*B(i, 1)^3+4*Coeff(20)*B(i, 2)^3+(6*Coeff(15)*B(i, 2)+3*Coeff(10))*B(i, 1)^2+3*Coeff(17)*B(i, 2)^2+(6*Coeff(18)*B(i, 2)^2+4*Coeff(14)*B(i, 2)+2*Coeff(9))*B(i, 1)+2*Coeff(13)*B(i, 2)+Coeff(8))*A(i, 1)*A(i, 2)+(Coeff(15)*B(i, 1)^3+10*Coeff(21)*B(i, 2)^3+(3*Coeff(18)*B(i, 2)+Coeff(14))*B(i, 1)^2+6*Coeff(19)*B(i, 2)^2+(6*Coeff(20)*B(i, 2)^2+3*Coeff(17)*B(i, 2)+Coeff(13))*B(i, 1)+3*Coeff(16)*B(i, 2)+Coeff(12))*A(i, 2)^2,
(5*Coeff(6)*A(i, 1)+Coeff(11)*A(i, 2))*B(i, 1)^4+(Coeff(20)*A(i, 1)+5*Coeff(21)*A(i, 2))*B(i, 2)^4+(4*Coeff(5)*A(i, 1)+Coeff(10)*A(i, 2)+(4*Coeff(11)*A(i, 1)+2*Coeff(15)*A(i, 2))*B(i, 2))*B(i, 1)^3+(Coeff(17)*A(i, 1)+4*Coeff(19)*A(i, 2))*B(i, 2)^3+((3*Coeff(15)*A(i, 1)+3*Coeff(18)*A(i, 2))*B(i, 2)^2+3*Coeff(4)*A(i, 1)+Coeff(9)*A(i, 2)+(3*Coeff(10)*A(i, 1)+2*Coeff(14)*A(i, 2))*B(i, 2))*B(i, 1)^2+(Coeff(13)*A(i, 1)+3*Coeff(16)*A(i, 2))*B(i, 2)^2+Coeff(2)*A(i, 1)+Coeff(7)*A(i, 2)+A(i, 3)+((2*Coeff(18)*A(i, 1)+4*Coeff(20)*A(i, 2))*B(i, 2)^3+(2*Coeff(14)*A(i, 1)+3*Coeff(17)*A(i, 2))*B(i, 2)^2+2*Coeff(3)*A(i, 1)+Coeff(8)*A(i, 2)+(2*Coeff(9)*A(i, 1)+2*Coeff(13)*A(i, 2))*B(i, 2))*B(i, 1)+(Coeff(8)*A(i, 1)+2*Coeff(12)*A(i, 2))*B(i, 2),
Coeff(6)*B(i, 1)^5+Coeff(21)*B(i, 2)^5+(Coeff(11)*B(i, 2)+Coeff(5))*B(i, 1)^4+Coeff(19)*B(i, 2)^4+(Coeff(15)*B(i, 2)^2+Coeff(10)*B(i, 2)+Coeff(4))*B(i, 1)^3+Coeff(16)*B(i, 2)^3+(Coeff(18)*B(i, 2)^3+Coeff(14)*B(i, 2)^2+Coeff(9)*B(i, 2)+Coeff(3))*B(i, 1)^2+Coeff(12)*B(i, 2)^2-Thickness+(Coeff(20)*B(i, 2)^4+Coeff(17)*B(i, 2)^3+Coeff(13)*B(i, 2)^2+Coeff(8)*B(i, 2)+Coeff(2))*B(i, 1)+Coeff(7)*B(i, 2)+B(i, 3)+Coeff(1) ] );
and then do the filtering like you had before.
Jack_111 on 14 Oct 2013
It was more than perfect
Big thanks

Sign in to comment.

More Answers (0)


Find more on Dates and Time in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!