Now I'm not an engineer, and I don't know what the function is supposed to do, but I noticed what seemed to be some mistakes in the code, so I rewrote the function as follows (in my own style; sorry if it doesn't match yours):
[tt]
void DriveTorque
(
int p_initial,int gamma1,int gamma2,double out_radius,
double in_radius,int resolution,int p_limit,double h,
double r_dash,double capacitance,int range,
double*torque_output
)
{
gamma1 = gamma1/360.0*2*pi;
gamma2 = gamma2/360.0*2*pi;
p_initial = p_initial/360.0*2*pi;
p_limit = p_limit/360.0*2*pi;
double*n_vector=new double[p_initial-p_limit];
n_vector[1] = 1.0/resolution;
for(int index=2;index<p_initial-p_limit;index++)
n_vector[index]=(1.0/resolution)+n_vector[index-1];
for(int index=1;index<=range;index++)
{
double
upper_limit=gamma2-n_vector[index],
lower_limit=gamma1-n_vector[index];
*torque_output=
capacitance
*
(
(
r_dash
/pow(cos(upper_limit),2)
*log((r_dash/cos(upper_limit))-out_radius)
+(out_radius/cos(upper_limit))
)
-
(
r_dash
/pow(cos(upper_limit),2)
*log((r_dash/cos(upper_limit))-in_radius)
+(in_radius/cos(upper_limit))
)
-
(
r_dash
/pow(cos(lower_limit),2)
*log((r_dash/cos(lower_limit))-out_radius)
+(out_radius/cos(lower_limit))
)
+
(
r_dash
/pow(cos(lower_limit),2)
*log((r_dash/cos(lower_limit))-in_radius)
+(in_radius/cos(lower_limit))
)
);
torque_output++;
}
delete[]n_vector;
}
[/tt]
The main corrections were as follows:
- When you divide two integers, the result is truncated to an integer, so gamma1/360, gamma2/360, p_limit/360, p_initial/360, and 1/resolution would give wrong results. When 360.0 and 1.0 are used, floating-point division happens, and precision is retained.
- The first "for" loop was an endless loop, since you were assigning sizeof(n_vector) to "index" at each iteration. Also, sizeof(n_vector) always equals 4, the size of a pointer.
- The torque_output argument was declared as a reference but used like a pointer.
- The arrays n_vector[] and torque[] were not freed in the function, which would result in memory leakage.
- The array torque[] was created but never used.
I haven't tested this new code, so you would probably have more changes to make.