In: Physics
Part I The input to the program will be a text file containing the information for a tolerance table. An example follows using the values from the first lecture on tolerance analysis. These values will be stored in a text file. The data is comma delimited, which means that each data field is separated by a comma. If the first word is ‘PART’ the following values are the nominal size, +/- impact, tolerance, and fixed/variable. If the first word is ‘GAP’ the following values are the minimum and maximum gap sizes. (Note: assume all units are inches.) PART,2.000,-1,0.050,V PART,0.975,-1,0.025,V PART,3.000,+1,0.010,F GAP,0.000,0.080 These input values will be processed using the methods taught in class. A sample output for the first stage is given. Actual Gap Mean: 0.025” Actual Gap Tolerance: 0.085” The Maximum Gap (0.110”) is (Greater) than specified (0.080”) The Minimum Gap (-0.060”) is (Less) than the specified (0.000”)
Part II The second stage extends the Stage 1 analysis. In this stage the program will suggest various combinations of part dimensions and tolerances to meet the gap specifications. The fixed dimension parts are not able to have their dimensions or tolerances adjusted. Take the required adjustment to all the variable parts and apply this adjustment to each part equally as a percentage of the total part dimension. Round dimensions to the nearest thousandth. Do the same application to adjust the gaps of all parts to have the gap fully use the available tolerance. Example Input: PART,2.000,-1,0.050,V PART,0.975,-1,0.025,V PART,3.000,+1,0.010,F GAP,0.000,0.080 Example Output: Recommended Adjustments to meeting GAP,0.000,0.80: PART,1.990,-1,0.020,V PART,0.970,-1,0.010,V PART,3.000,+1,0.010,F Math used to get to the result Variable parts should add to (3 – 0.04) = 2.96 Variable parts actually add to (2 + 0.975) = 2.975 All variable parts must be adjusted to 2.96/2.975 = 99.496% of the original value Variable tolerance should add to (0.04 – 0.01) = 0.03 Variable tolerance actually add to (0.05 + 0.025) = 0.075 All variable parts’ tolerance must be adjusted to 0.3/0.073 = 40% of the original value
Part III The third stage involves a statistical analysis called Monte Carlo simulation. Basically each of the dimensions is varied randomly and the gap is calculated. This random calculation is repeated hundreds or thousands of times. For each iteration, the individual gap value is calculated and stored in an array and in a file. The array of values will be used to compute the mean and standard deviation of the gap. The file will be opened using a spreadsheet program to graph a histogram, calculate an average, and calculate the standard deviation, which should match the result from your program. These values will then be used to estimate the number of rejected assemblies during production. It is reasonable to assume that the tolerance for a part is 3 standard deviations (99.73% of parts will fall within the tolerance). So for any part, we can generate random realistic values by taking the nominal value and adding/subtracting a random number, which represents variations due to the tolerance. To do this, we will need to generate a specific type of random numbers. The standard random number function in most programming language (rand() in C included) has a uniform distribution. This means that if we are finding random numbers from 0.0 to 1.0, the probability of getting 0.5 is the same as getting 0.1. This is not realistic for our application, since it will be more likely that we have our dimensions change by 0.1 than it is 0.5. For this reason, we need to modify the numbers from the rand() function so that they have a Gaussian (Normal) Distribution, which is sometimes referred to as the “bell curve”. This can be accomplished by using the Box-Muller Transformation. Although it sounds complicated, this transformation can be easily done and applied to generate a random dimension with the code given. Summary of Part 3: Apply the random_dimension() function below to get an output of a random_value of each part that will follow the explanation above. This will create a statistically likely value for each dimension given that you can then use to rerun the “actual mean gap” calculation from Part 1 1000 times. For each calculation, have the program store the value as a line in a text file (.csv extension.) You’ll be able to open this file up using Excel as a .csv file. In Excel, graph a histogram, calculate the average gap of all assemblies (of parts), and the standard deviation of the gap of all assemblies. void random_dimension(double nominal, double tolerance, double *random_value){ double r1, r2, r12; double sigma = tolerance / 3; do{ r1 = (double)( rand() % 10001 ) / 10000; }while(r1==0); r2 = (double)( rand() % 10001 ) / 10000; r12 = sqrt(-2*log(r1))*cos(2*M_PI*r2); *random_value = nominal + sigma * r12; }
/** C code for tolerance analysis **/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define BUFFER_SIZE 1024
#define ARRAYSIZE 100
// returns the number elements stored in arrays
int input(float nom[],float tollerance[],int SIGNS[],char
V_F[],float p_Spec_Minnimum,float p_Spec_Maximum)
{
int status = 0, i,c;
FILE *FTIN;
FTIN = fopen ("filename", "r");
if (FTIN == NULL){ //file empty/broken error
printf("ERROR\n");
return (-1);
}
/******/
else{
for (i=0; status != EOF && i < 100; ){ //reads until
EoF, even though some guy on stackoverflow taught me it's bad
status = fscanf(FTIN,"PART,%f,%d,%f,%c\n", &nom[i],
&SIGNS[i], &tollerance[i], &V_F[i]); //scans for a
part
if(status == 4)
{
i++;
}
status = fscanf(FTIN, "GAP,%f,%f\n", p_Spec_Minnimum,
p_Spec_Maximum); //scans for a gap
//printf("Reading Info Into Arrays\n");
}
fclose(FTIN);
return i;
}
}
// output arrays in special format
void output(int size, float nom[],float tollerance[],int
SIGNS[],char V_F[],float Spec_Minnimum,float Spec_Maximum)
{
int i;
for (i=0; i < size; i++)
{
printf("PART,%f,%d,%f,%c\n", nom[i], SIGNS[i], tollerance[i],
V_F[i]);
}
printf("GAP,%f,%f\n", Spec_Minnimum, Spec_Maximum);
}
float toleracningPt1(int size, float nom[],float tollerance[],int
SIGNS[],char V_F[],float Spec_Minnimum,float Spec_Maximum)
{
int x;
float Act_Gap, Act_Tollerance, Maximum_Gap = 0.0, Minnimum_Gap =
0.0;
for ( x=0, Act_Gap = 0; x<size; x++){ //does tolerance
math
Act_Gap = Act_Gap + (nom[x]*SIGNS[x]);
}
for ( x=0, Act_Tollerance = 0; x<size; x++){
Act_Tollerance = Act_Tollerance + (tollerance[x]);
}
for (x= 0, Maximum_Gap = 0; x<size; x++){
Maximum_Gap = (nom[x]*SIGNS[x]+tollerance[x])+Maximum_Gap;
Minnimum_Gap = (nom[x]*SIGNS[x]-tollerance[x])+Minnimum_Gap;
}
printf("Actual Gap Mean: %.3f\"\n", Act_Gap); //printing
printf("Actual Gap Tolerance: %.3f\"\n", Act_Tollerance);
if (Maximum_Gap > Spec_Maximum){
printf("The maximum gap (%.3f\") is (Greater) than specified
(%.3f\")\n", Maximum_Gap, Spec_Maximum);
}
if (Maximum_Gap < Spec_Maximum){
printf("The maximum gap (%.3f\") is (Less) than specified
(%.3f\")\n", Maximum_Gap, Spec_Maximum);
}
if (Minnimum_Gap > Spec_Minnimum){
printf("The minimum gap (%.3f\") is (Greater) than specified
(%.3f\")\n", Minnimum_Gap, Spec_Minnimum);
}
if (Minnimum_Gap < Spec_Minnimum){
printf("The minimum gap (%.3f\") is (Less) than specified
(%.3f\")\n", Minnimum_Gap, Spec_Minnimum);
}
}
int main(void){
/**Decs**/
float nom[100], tollerance[100];
int SIGNS[100];
char V_F[100];
//int status, i, x;
float Act_Gap, Act_Tollerance, Spec_Minnimum, Spec_Maximum;
/**custom functions**/
int size = 0;
size = input( nom, tollerance, SIGNS, V_F, &Spec_Minnimum,
&Spec_Maximum);
output( size, nom, tollerance, SIGNS, V_F, Spec_Minnimum,
Spec_Maximum);
toleracningPt1(size, nom, tollerance, SIGNS, V_F, Spec_Minnimum,
Spec_Maximum);
}