-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
278 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#include <stdio.h> | ||
#include <limits.h> | ||
|
||
int main() { | ||
printf("The range of long int is from %ld to %ld\n", LONG_MIN, LONG_MAX); | ||
printf("The range of long int is from %d to %d\n", INT_MIN, INT_MAX); | ||
printf("The range of long int is from 0 to %u\n", UINT_MAX); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <math.h> | ||
|
||
int main(int argc, char **argv) { | ||
|
||
// read a long int | ||
long int numSteps = 100; | ||
if (argc == 2) { | ||
char *endPtr; | ||
numSteps = strtol(argv[1], &endPtr, 10); | ||
} | ||
|
||
// | ||
// perform calculation | ||
// | ||
|
||
double pi = 0; | ||
double dx = 1./numSteps; | ||
double x = dx; | ||
|
||
for (int i=0; i<numSteps; i++) { | ||
x = (i+.5)*dx; | ||
pi += 4./(1.+x*x); | ||
//x += dx; not as accurate | ||
} | ||
|
||
pi *= dx; | ||
|
||
printf("PI = %16.14f, diff(%16.14f) %ld\n",pi, fabs(pi-M_PI), numSteps); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <math.h> | ||
|
||
#include <stdio.h> | ||
#include <math.h> | ||
|
||
// | ||
// f(x) Function to integrate | ||
// | ||
|
||
double f(double x) { | ||
return 4.0 / (1.0 + x * x); | ||
} | ||
|
||
// | ||
// integration of f(x) from 0->1 by simpsons rule | ||
// | ||
|
||
double simpsons_rule(long int n) { | ||
if (n % 2 != 0) { | ||
n++; // Simpson's rule requires an even number of intervals | ||
} | ||
|
||
double sum = 0.0; | ||
double step = 1.0 / n; | ||
|
||
for (long int i = 0; i <= n; i++) { | ||
double x = i * step; | ||
if (i == 0 || i == n) { | ||
sum += f(x); | ||
} else if (i % 2 == 0) { | ||
sum += 2 * f(x); | ||
} else { | ||
sum += 4 * f(x); | ||
} | ||
} | ||
|
||
return sum * step / 3.0; | ||
} | ||
|
||
// | ||
// integration of f(x) from 0->1 by trapezoidal rule | ||
// | ||
|
||
double trapezoidal_rule(long int n) { | ||
double sum = 0.0; | ||
double step = 1.0 / n; | ||
|
||
for (long int i = 0; i <= n; i++) { | ||
double x = i * step; | ||
if (i == 0 || i == n) { | ||
sum += f(x) / 2.0; | ||
} else { | ||
sum += f(x); | ||
} | ||
} | ||
|
||
return sum * step; | ||
} | ||
|
||
// | ||
// | ||
// | ||
|
||
double basic(long int n) { | ||
double sum = 0.0; | ||
double step = 1.0 / n; | ||
|
||
for (long int i = 0; i <= n; i++) { | ||
double x = (i+.5)*step; | ||
sum += f(x); | ||
} | ||
|
||
return sum * step; | ||
} | ||
|
||
int main(int argc, char **argv) { | ||
|
||
// read args | ||
|
||
if (argc != 2) { | ||
printf("Usage: appName numSteps\n"); | ||
exit(-1); | ||
} | ||
char *endptr; | ||
long int numSteps = strtol(argv[1], &endptr,10); | ||
|
||
double piBasic = basic(numSteps); | ||
double piSimpson = simpsons_rule(numSteps); | ||
double piTrapezoidal = trapezoidal_rule(numSteps); | ||
|
||
printf("PI basic: %16.14f, diff(%16.14f), simpson %16.14f, (%16.14f), trap %16.14f, (%16.14f)\n", | ||
piBasic, fabs(piBasic-M_PI), | ||
piSimpson, fabs(piSimpson-M_PI), | ||
piTrapezoidal, fabs(piTrapezoidal-M_PI)); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
./pi 100 | ||
./pi 1000 | ||
./pi 10000 | ||
./pi 100000 | ||
./pi 1000000 | ||
./pi 10000000 | ||
./pi 100000000 | ||
./pi 1000000000 | ||
./pi 2000000000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// program to solve quadratic equation | ||
// ax^2 + bx + c = 0 | ||
// | ||
// soln: x = -b/2a +/- sqrt(b^2-4ac)/2a | ||
// | ||
// write a program to take 3 inputs and output the soln | ||
// deal with possible errors in input, i.e. b^2-4ac negative | ||
|
||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <math.h> | ||
|
||
int main(int argc, char **argv) { | ||
|
||
if (argc != 4) { | ||
printf("Usage: appName a b c\n"); | ||
exit(-1); | ||
} | ||
|
||
float a = atof(argv[1]); | ||
float b = atof(argv[2]); | ||
float c = atof(argv[3]); | ||
|
||
double b2minus4ac = b*b-4*a*c; | ||
|
||
if (b2minus4ac > 0.0) { | ||
|
||
double bDiv2a = -b/(2*a); | ||
double other = sqrt(b2minus4ac)/(2*a); | ||
double x1 = bDiv2a - other; | ||
double x2 = bDiv2a + other; | ||
printf("%.8f, %.8f\n", x1, x2); | ||
|
||
} else if (b2minus4ac == 0.0) { | ||
|
||
double x = -b/(2*a); | ||
printf("%.8f\n", x); | ||
|
||
} else { | ||
|
||
double bDiv2a = -b/(2*a); | ||
double other = sqrt(-b2minus4ac)/(2*a); | ||
double x1 = bDiv2a - other; | ||
double x2 = bDiv2a + other; | ||
|
||
printf("%.8f + %.8fi,%.8f - %.8f\n", bDiv2a, other, bDiv2a, other); | ||
|
||
} | ||
|
||
|
||
return 0; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// program to transform stress: | ||
// | ||
// sigmaX' = sigmaX * cos^2(theta) + sigmaY * sin^2(theta) + 2 * tauXY Sin(theta)Cos(theta) | ||
// sigmaY' = sigmaX * sin^2(theta) + sigmaY * cos^2(theta) - 2 * tauXY Sin(theta)Cos(theta) | ||
// tauXY' = (sigmaY-sigmaX) * sin(theta)cos(theta) + tauXY(cos^2(theta) - sin^2(theta)) | ||
// | ||
// write a program to take: | ||
// 1: 4 inputs: sigmaX, sigmaY, tauXY, theta and output transformed stresses: sigmaX', sigmaY', tauXY' | ||
// 2: 3 inputs: sigmaX, sigmaY, tauXY and output transformed stresses: sigmaX', sigmaY', tauXY' for every 10 degrees | ||
// | ||
// NOTE: perform the transformation inside a function that cannot be named main | ||
// | ||
// Extend to possibly include all outputs for a Mohr circle, were the theta now provided is a deltaTheta | ||
|
||
#include <stdlib.h> | ||
#include <stdio.h> | ||
#include <math.h> | ||
|
||
void transformStress(float *stressIN, float theta, float *stressTransformed); | ||
|
||
int main(int argc, char **argv) { | ||
|
||
// NOTE: if 5 args are provided, the program will output data for a Mohr Circle | ||
if (argc != 4 && argc != 5) { | ||
printf("Usage: appName sigX sigY tauXY theta <anything>\n"); | ||
exit(-1); | ||
} | ||
|
||
float vectorIN[3], vectorTransformed[3]; | ||
vectorIN[0] = atof(argv[1]); | ||
vectorIN[1] = atof(argv[2]); | ||
vectorIN[2] = atof(argv[3]); | ||
float theta = 0; | ||
if (argc == 5) | ||
theta = atof(argv[4]); | ||
|
||
if (argc == 5) { | ||
|
||
// 4 args: Compute single transformed stress for theta provided | ||
|
||
transformStress(vectorIN, theta, vectorTransformed); | ||
printf("%.4f, %.4f, %.4f\n", vectorTransformed[0], vectorTransformed[1], vectorTransformed[2]); | ||
|
||
} else { | ||
|
||
// 5 args provided: compute Mohr circle using deltaTheta | ||
|
||
// compute for all angles until we reach 360 (don't compute for 360) | ||
float currentTheta = theta; | ||
while (currentTheta < 360.) { | ||
transformStress(vectorIN, currentTheta, vectorTransformed); | ||
printf("%.4f, %.4f, %.4f, %.4f\n", currentTheta, vectorTransformed[0], | ||
vectorTransformed[1], vectorTransformed[2]); | ||
currentTheta += 10.; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
void transformStress(float *stressIN, float theta, float *stressTransformed) { | ||
|
||
float sigX = stressIN[0]; | ||
float sigY = stressIN[1]; | ||
float tauXY = stressIN[2]; | ||
|
||
// convert theta to radians & compute sin and cos of the angle | ||
|
||
float thetaRadians = theta*M_PI/180.; // M_PI is PI as a double from the math.h file | ||
float cosX = cos(thetaRadians); | ||
float sinX = sin(thetaRadians); | ||
|
||
stressTransformed[0] = sigX*cosX*cosX + sigY*sinX*sinX + 2*tauXY * sinX*cosX; | ||
stressTransformed[1] = sigX*sinX*sinX + sigY*cosX*cosX - 2*tauXY * sinX*cosX; | ||
stressTransformed[2] = (sigY-sigX)*sinX*cosX + tauXY * (cosX*cosX - sinX*sinX); | ||
} |