(*******************************  caryophylloides masmenea  *)

Needs["Splines`"]

caryophylloides[1]={{0.893,40.1786},{4.464,32.1429},{8.036,27.6786},{11.607,33.0357},{15.179,42.8571},{18.750,44.6429},{22.321,48.2143},{25.893,50.0000},{29.464,50.8929},{33.036,52.6786},{36.607,54.4643},{40.179,54.4643},{43.750,54.4643},{47.321,53.5714},{50.893,52.6786},{54.464,50.8929},{58.036,50.0000},{61.607,48.2143},{65.179,46.4286},{68.750,42.8571},{72.321,38.3929},{75.893,32.1429},{79.464,33.0357},{83.036,36.6071},{86.607,38.3929},{90.179,40.1786},{93.750,40.1786},{97.321,40.1786},{100.893,41.9643},{104.464,41.9643},{108.036,41.0714},{111.607,40.1786},{115.179,38.3929},{118.750,36.6071},{122.321,33.9286},{125.893,30.3571},{129.464,25.0000},{133.036,19.6429},{136.607,11.6071},{140.179,12.5000}}
caryophylloides[2]={{0.893,34.8214},{4.464,32.1429},{8.036,29.4643},{11.607,27.6786},{15.179,28.5714},{18.750,33.0357},{22.321,37.5000},{25.893,41.9643},{29.464,44.6429},{33.036,45.5357},{36.607,47.3214},{40.179,47.3214},{43.750,49.1071},{47.321,49.1071},{50.893,49.1071},{54.464,49.1071},{58.036,48.2143},{61.607,45.5357},{65.179,43.7500},{68.750,41.9643},{72.321,40.1786},{75.893,35.7143},{79.464,32.1429},{83.036,28.5714},{86.607,30.3571},{90.179,33.0357},{93.750,37.5000},{97.321,41.0714},{100.893,42.8571},{104.464,45.5357},{108.036,46.4286},{111.607,47.3214},{115.179,49.1071},{118.750,49.1071},{122.321,49.1071},{125.893,49.1071},{129.464,49.1071},{133.036,48.2143},{136.607,47.3214},{140.179,46.4286},{143.750,44.6429},{147.321,43.7500},{150.893,41.0714},{154.464,38.3929},{158.036,35.7143},{161.607,32.1429},{165.179,27.6786},{168.750,24.1071},{172.321,25.8929},{175.893,28.5714},{179.464,32.1429},{183.036,33.9286}}
caryophylloides[3]={{0.893,19.6429},{4.464,15.1786},{8.036,10.7143},{11.607,11.6071},{15.179,17.8571},{18.750,24.1071},{22.321,28.5714},{25.893,31.2500},{29.464,33.0357},{33.036,33.9286},{36.607,35.7143},{40.179,37.5000},{43.750,38.3929},{47.321,39.2857},{50.893,40.1786},{54.464,38.3929},{58.036,38.3929},{61.607,38.3929},{65.179,36.6071},{68.750,34.8214},{72.321,33.0357},{75.893,30.3571},{79.464,27.6786},{83.036,25.0000},{86.607,21.4286},{90.179,25.0000},{93.750,28.5714},{97.321,30.3571},{100.893,33.9286},{104.464,35.7143},{108.036,37.5000},{111.607,39.2857},{115.179,40.1786},{118.750,40.1786},{122.321,40.1786},{125.893,40.1786},{129.464,40.1786},{133.036,38.3929},{136.607,37.5000},{140.179,35.7143},{143.750,33.0357},{147.321,31.2500},{150.893,28.5714},{154.464,24.1071},{158.036,23.2143},{161.607,28.5714},{165.179,32.1429},{168.750,35.7143}}
caryophylloides[4]={{0.893,23.2143},{4.464,21.4286},{8.036,17.8571},{11.607,17.8571},{15.179,20.5357},{18.750,23.2143},{22.321,26.7857},{25.893,28.5714},{29.464,31.2500},{33.036,32.1429},{36.607,33.9286},{40.179,34.8214},{43.750,34.8214},{47.321,34.8214},{50.893,34.8214},{54.464,33.9286},{58.036,33.0357},{61.607,31.2500},{65.179,28.5714},{68.750,26.7857},{72.321,24.1071},{75.893,19.6429},{79.464,16.9643},{83.036,18.7500},{86.607,25.0000},{90.179,27.6786},{93.750,30.3571},{97.321,32.1429},{100.893,34.8214},{104.464,35.7143},{108.036,37.5000},{111.607,38.3929},{115.179,38.3929},{118.750,38.3929},{122.321,38.3929},{125.893,38.3929},{129.464,37.5000},{133.036,35.7143},{136.607,34.8214},{140.179,32.1429},{143.750,30.3571},{147.321,26.7857},{150.893,24.1071},{154.464,19.6429},{158.036,16.9643},{161.607,16.9643},{165.179,16.9643}}

(*Poner aqui la familia*)
var=caryophylloides;
(* el número de curvas estudiadas/semillas de la familia*)
m=4;
For[j=1,j≤m,j++,
puntos=var[j];
(* pru es el spline cubico
d es el vector que contiene la información de pru 
n es el número de trozos del spline, es decir, el número de puntos menos 1
*)
pru=SplineFit[puntos,Cubic];
d=InputForm[pru];
n=IntegerPart[d[[1,2,2]]];
spli=ParametricPlot[pru[t],{t,0,n},PlotRange->All, DisplayFunction->Identity];
(* Con los coeficientes se calculan los polinomios cúbicos que dan el spline. Habrá n polinomios para la primera coordenada (X) y otros tantos para la segunda
*)
For[i=0,i<n,i++,
SX[i,t_]=d[[1,4,i+1,1,1]]+d[[1,4,i+1,1,2]]*(t-i)+d[[1,4,i+1,1,3]]*(t-i)^2+d[[1,4,i+1,1,4]]*(t-i)^3];
For[i=0,i<n,i++,SY[i,t_]=d[[1,4,i+1,2,1]]+d[[1,4,i+1,2,2]]*(t-i)+d[[1,4,i+1,2,3]]*(t-i)^2+d[[1,4,i+1,2,4]]*(t-i)^3];
(* Derivada primera y segunda de cada trozo del spline
*)
For[i=0,i<n,i++,DX[i,t_]=d[[1,4,i+1,1,2]]+2*d[[1,4,i+1,1,3]]*(t-i)+3*d[[1,4,i+1,1,4]]*(t-i)^2];
For[i=0,i<n,i++,DY[i,t_]=d[[1,4,i+1,2,2]]+2*d[[1,4,i+1,2,3]]*(t-i)+3*d[[1,4,i+1,2,4]]*(t-i)^2];
For[i=0,i<n,i++,DDX[i,t_]=2*d[[1,4,i+1,1,3]]+6*d[[1,4,i+1,1,4]]*(t-i)];
For[i=0,i<n,i++,DDY[i,t_]=2*d[[1,4,i+1,2,3]]+6*d[[1,4,i+1,2,4]]*(t-i)];
(*Cálculo de la curvatura a trozos
*)
For[i=0,i<n,i++,cur[i,t_]=(DX[i,t]DDY[i,t]-DDX[i,t]DY[i,t])/((DX[i,t])^2+(DY[i,t])^2)^(3/2)];
(* Dibujos de los trozos de curvatura 
*)
For[i=0,i<n,i++,dib[i]=Plot[cur[i,t],{t,i,i+1},DisplayFunction->Identity]];
pp=dib[0];
For[i=1,i<n,i++,pp=Show[pp,dib[i],PlotRange->All]];
(* bez es el spline de Bezier
b es el vector que contiene la información de bez
nb es el número de trozos del spline, es decir, el número de puntos menos 1 (Como nb=n, no se define nuevo)
*)
bez=SplineFit[puntos,Bezier];
b=InputForm[bez];
(*Calculo de las funciones de Bezier para la coordenada X y la Y; la curva es la imagen del intervalo[0,1]*)
For[i=1,i≤n+1,i++,BX[t_]=bez[[4,1,1]]/.#1->t];
For[i=1,i≤n+1,i++,BY[t_]=bez[[4,1,2]]/.#1->t];
dibez[j]=ParametricPlot[{BX[t],BY[t]},{t,0,1},PlotRange->All,DisplayFunction->Identity];
curbe[j,t_]=1000*(BX'[t]BY''[t]-BX''[t]BY'[t])/((BX'[t])^2+(BY'[t])^2)^(3/2);
aa=0.1;
bb=0.9;
cur[j]=Abs[NMinValue[{curbe[j,t],aa<t<bb},t]];
vmed[j]=Abs[NIntegrate[curbe[j,t],{t,aa,bb}]];
vals[j]=Reap[soln=y[x]/. First[NDSolve[{y'[x]==Evaluate[D[curbe[j,x],x]],y[0]==(curbe[j,x]/. x->0)},y[x],{x,aa,bb},Method->{"EventLocator","Event"->y'[x],"EventAction":>Sow[{x,y[x]}]}]]][[2,1]];
Print[ var,"-",j,":"];Print["Máximos y mínimos locales "];
Print[vals[j]];
z=vals[j];
max[j]=Max[Table[z[[k,2]],{k,1,Length[z]}]];
min[j]=Min[Table[z[[k,2]],{k,1,Length[z]}]];dibcurbe[j]=Plot[curbe[j,x],{x,0,1},Epilog->{PointSize[Medium],Red,Point[z]}];
Print[dibez[j]];
Print[dibcurbe[j]];
Print[Plot[{curbe[j,t],vmed[j]},{t,0,1},Axes->True,DisplayFunction->Identity,PlotStyle->{RGBColor[0,1,0],{Dotted,Dashed}}]];
Print[ "Máximo valor de la curvatura",max[j]];
Print[ "Mínimo valor de la curvatura",min[j]];Print[ "Máximo valor de la curvatura (en valor absoluto)con otro cálculo",cur[j]];
Print["______________________________________________________"];];
Print["Curvaturas de cada ", var ,":  ", Table[cur[r],{r,1,4}]];
Print["Valores medios de curvatura de cada ", var ," en [0,1]:  ", Table[vmed[j],{j,1,4}]];

Show[Table[dibcurbe[j],{j,1,6}]]

Table[NMinValue[{curbe[j,t],0.25<t<0.75},t],{j,1,6}]

dibcurbe[j],{j,1,6}

Table[dibcurbe[j],{j,1,6}]

curbe[j,t_]
dibcurbe[j]

Plot[curbe[1,x],{x,0,1},Epilog->{PointSize[Medium],Red,Point[vals[1]]}]

dibcurbe[j]=Plot[curbe[j,x],{x,0,1},Epilog->{PointSize[Medium],Red,Point[z]}]

dibcurbe2[1]

Print[dibez[1]];

f=vals[1]

Max[vals[1]]

Max[Table[f[[k,2]],{k,1,Length[f]}]]
Min[Table[f[[k,2]],{k,1,Length[f]}]]

List