Frage

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); 

NaNIndices = find(isnan(IntersectionPointsLayer(:,1))==1);
tCounter(NaNIndices) = NaN;
NotNaNIndices = find(isnan(IntersectionPointsLayer(:,1))==0);

for i = NotNaNIndices 
syms t
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;
end

Many thanks in advance

War es hilfreich?

Lösung

Symbolic math is slow, I would suggest using the roots function instead (you are converting the result to double precision anyway).

To use that you need to compute the coefficients for the polynomial with respect to variable t (which requires some rearrangements for your formula), but this will be significantly faster.

Andere Tipps

I found the answer. I had to solve the equation generally outside the loop and then use the loop to get for each instance. It is still slow but not like before

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top