## Skewed perpendicular function

Hello,
I am trying to fit a sine function to a general curve. Convolution isn't giving me what I want, which is for every point on a general curve, displace that point by another function. I have written some code in Octave, and it's close, but not quite it.
What I did was to take a general function, take it's derivative at each point, took the inverse reciprocal for the slope of the perpendicular line to that point, set the distance to the point I wanted, then put the perpendicular line in point slope for to solve for the points of interest. The plot come close, but at the derivative approaches zero, so do the perpendicular points.

My math:
f(theta)= 4*theta^3-36*theta+14*theta+4
f'(theta)= 12*theta^2-72*theta+14
(1) tan line: y-z=f'(theta)*(x-theta)
(2) perp line: y-z=(-1/f'(theta))*(x-theta)
(3) d=sqrt((x-theta)^2+(y-z)^2)

solve for y in (2) and sub into (3)
d^2=(x-theta)^2+((theta-x)/f'(theta)+z-z)^2
Simplify
d^2=(x-theta)^2*(1-1/f'(theta)^2)
Simplify
sqrt((d^2*f'(theta)^2)/(f'(theta)-1))=x-theta
Solve for x
d*f'(theta)/sqrt(f'(theta)^2-1)+theta=x
Then sub into perpendicular point slope form and solve for y.

Code:
theta=[-2*pi:2*pi/1000:2*pi];#domain
z=4*theta.^3-36*theta.^2+14*theta+4;#general function
dz=12*theta.^2-72*theta+14;#derivative
d=0.5*cos(36*theta);#distance
x=((d.*dz)./(sqrt(dz.^2-1)))+theta;
y=-((x-theta)./dz)+z;
figure
hold on
#plot(theta,z)#un-comment for general plot.
plot(x,y)
hold off
Thank you.