function [qvect, qvelvect, qacelvect, qjvect, qjprimvect,t] = trapez4( qr1, qr2, qpr, qsec, qter, qcuar, tm,textra);

s=qr2-qr1;
signo = sign(s);

if abs(qsec) > qter*qter/qcuar
	t1=qter/qcuar;
	t2=abs(qsec)/qter;
	t3=t1+t2;
else
	t1 = sqrt(abs(qsec)/qcuar);
	t3 = 2*t1;
	t2=t1;
end;


t4=qpr/qsec;
t5=t4+t1;
t6=t5+t2-t1;
t7=t6+t1;
t8=(qr2-qr1)/qpr;
t9=t8+t1;
t10=t9+t2-t1;
t11=t10+t1;
t12=t11+t4-t3;
t13=t12+t1;
t14=t13+t2-t1;
t15=t14+t1;
tfin=t15;

q=qr1;
qprim=0;
jerk = 0;
acel = 0;
qvect=qr1;
qvelvect=0;
qacelvect=0;
qjvect=0;
qjprimvect=0;
t=0;

for tiempo=0:tm:tfin+textra,
	if (tiempo < t1) || ((tiempo >=t6)&&(tiempo <t7)) || ((tiempo >=t10)&&(tiempo <t11)) || ((tiempo >=t12)&&(tiempo <t13)),
        jerkprim = signo*qcuar;
        jerk = jerk + jerkprim*tm;
		acel = acel + jerk*tm +0.5*jerkprim*tm*tm;
		qprim= qprim + acel*tm + 0.5*jerk*tm*tm + (1/6)*jerkprim*tm*tm*tm;
		q= q + qprim*tm + 0.5*acel*tm*tm + (1/6)*jerk*tm*tm*tm + (1/24)*jerkprim*tm*tm*tm*tm;

	elseif ((tiempo >= t1)&&(tiempo < t2)) || ((tiempo >=t13)&&(tiempo <t14)),
        
        jerkprim = 0;
        jerk = signo*qter;
		acel = acel + jerk*tm;
		qprim =qprim + acel*tm + 0.5*jerk*tm*tm;
		q= q + qprim*tm + 0.5*acel*tm*tm + (1/6)*jerk*tm*tm*tm;
	
	elseif ((tiempo >=t2)&&(tiempo <t3)) || ((tiempo >=t4)&&(tiempo <t5)) || ((tiempo >=t8)&&(tiempo <t9)) || ((tiempo >=t14)&&(tiempo <t15)),
        
		jerkprim = -signo*qcuar;
        jerk = jerk + jerkprim*tm;
		acel = acel + jerk*tm +0.5*jerkprim*tm*tm;
		qprim= qprim + acel*tm + 0.5*jerk*tm*tm + (1/6)*jerkprim*tm*tm*tm;
		q= q + qprim*tm + 0.5*acel*tm*tm + (1/6)*jerk*tm*tm*tm + (1/24)*jerkprim*tm*tm*tm*tm;
        
    elseif (tiempo >=t3)&&(tiempo <t4),
        
        jerkprim = 0;
        jerk = 0;
		acel = signo*qsec;
		qprim = qprim + acel*tm;
		q = q + qprim*tm + 0.5*acel*tm*tm;     
        
    elseif ((tiempo >=t5)&&(tiempo <t6)) || ((tiempo >=t9)&&(tiempo <t10)),
        
        jerkprim = 0;
        jerk = -signo*qter;
		acel = acel + jerk*tm;
		qprim =qprim + acel*tm + 0.5*jerk*tm*tm;
		q= q + qprim*tm + 0.5*acel*tm*tm + (1/6)*jerk*tm*tm*tm;
        
    elseif (tiempo >=t7)&&(tiempo <t8),
        
        jerkprim = 0;
        jerk = 0;
		acel = 0;
		qprim = signo*qpr;
		q= q + qprim*tm;
                
    elseif (tiempo >=t11)&&(tiempo <t12)
        
        jerkprim = 0;
        jerk = 0;
		acel = -signo*qsec;
		qprim = qprim + acel*tm;
		q = q + qprim*tm + 0.5*acel*tm*tm; 
                     
	else
		jerkprim = 0;
        jerk = 0;
		acel = 0;
		qprim = 0;
		q = qr2;      
        
  	end

	qvect=[qvect;q];
	qvelvect=[qvelvect;qprim];
	qacelvect=[qacelvect;acel];
    qjvect=[qjvect;jerk];
    qjprimvect=[qjprimvect;jerkprim];
	t=[t;tiempo];
end






