I find the solution !!! by calling it 1000 times in another code, total time decrease from 15s to 3s !!!
The problem was how i call for the function(variables) , this is how i was doing:
Pi1 = [x(1) L1(1)];
Pi2 = [x(end) L2(end)];
Pf1 = [x(1) L1(1)];
Pf2 = [x(end) L2(end)];
pX=pInter(Pi1,Pf1,Pi2,Pf2);
First solution:
Call
pX = pInterNew1(x,L1,L2);
Function
function [pX] = pInterNew1(x,L1,L2)
A = [x(1) L1(1)];
B = [x(end) L1(end)];
C = [x(1) L1(1)];
D = [x(end) L2(end)];
%====Same code of original function
end
Anoter solution:
Call
pX = pInterNew2(x(1),x(end),L1(1),L1(end),L2(1),L2(end));
function
function [pX] = pInterNew2(xin,xEnd,Pi1,Pf1,Pi2,Pf2)
a = Pf1 - Pi1;
b = xin - xEnd;
c = a*xin + b*Pi1;
a1 = Pf2 - Pi2;
b1 = xin - xEnd;
c1 = a1*xin+ b1*Pi2;
det = a*b1 - a1*b;
if det == 0
pX= NaN; %are parallel
else
X = (b1*c - b*c1)/det;
Y = (a*c1 - a1*c)/det;
pX=[X,Y];
end
end