Hi,
I am trying to write a program to handle some simple numerical integration schemes and I keep having certain errors. Whenever I try to convert the integer types from float to double the program stops working, I am getting accuracy errors using float so I would like to know what I am doing wrong. Also on the monte carlo routine I am trying to generate a random number between a and b and then convert it back to a floating point but if you run the code you can tell it doesnt work. Any general advice in addition would be helpful. I am trying to self teach myself c for grad school and have no formal background in any languages.
Please see code below:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
/* use of variable declaration outside of main causes them to be global*/
void simp(void);
void trap(void);
void monte(void);
float f1(float x);
float f2(float x);
main ()
{
int choice;
printf("\nHello there and welcome to Jared's integration routine\n"
while(choice != 4)
{
printf("\nWould you prefer to use 1-The Trapezoidal Method (x^3) 2-Simpson's Method (e^(-x)) 3-Monte Carlo 4-exit\n"
scanf("%d", &choice);
switch(choice)
{
case 1: /*Trapezoidal Method for x^3*/
trap();
break;
case 2: /*Simpson's Method*/
simp();
break;
case 3: /*Monte Carlo Method*/
monte();
break;
}
}
printf("\n Goodbye "
}
/*Trapezoidal method*/
void trap(void)
{
float x1, x2, sumt, a, b, step;
printf("\nWhat is your starting point?\n"
scanf("%f", &a);
printf("\nWhat is your end point?\n"
scanf("%f", &b);
printf("\nWhat is your step size?\n"
scanf("%f", &step);
sumt = 0.0;
x1 = a;
printf("%f %f %f", a, b, step);
for (x2 = (a + step) ; x2 <= b; x2 = (x2 + step))
{
sumt += ((step)*(f1(x1) + f1(x2)))/2.0;
x1 = x2;
}
printf("\n The answer is %f", sumt);
return;
}
/*Simpson's Rule*/
void simp(void)
{
float sums, x1, x2, a, b , step;
int i;
printf("\nWhat is your starting point?\n"
scanf("%f", &a);
printf("\nWhat is your end point?\n"
scanf("%f", &b);
printf("\nWhat is your step size?\n"
scanf("%f", &step);
sums = (f2(a) + f2(b));
x1 = a;
for (x2 = (x1 + step); x2 <= b; x2 = (x2 + step))
{
sums += (2.0*f2(x2));
}
sums = (sums * step)/2.0;
printf("\n The answer is %f", sums);
return;
}
/*Monte Carlo Method*/
void monte(void)
{
float summ, yr, f, c = 0.0, rx, rxn, ry, ryn;
int rand();
int i, n, a, b, rxi;
printf("\nWhat is your starting point?\n"
scanf("%f", &a);
printf("\nWhat is your end point?\n"
scanf("%f", &b);
printf("\nHow many iterations would you like?\n"
scanf("%d", &n);
for (i=1; i<=n; ++i)
{
rxi = rand()%(b-a+1)+a;
printf("\n%d", rxi);
rxn = rxi/1.0;
printf("\n%f", rxn);
yr = f1(rxn);
printf("\n%", yr);
ry = rand()/32768.0;
ryn = (yr * ry);
c += (ry <= yr) ? 1.0 :0.;
}
f = (float) c/i;
summ = f*f1(b - a);
printf("\n The answer is %f", summ);
return;
}
/* function to evalaute by trapezoidal method*/
float f1(float x)
{
return ( x * x * x );
}
/* function to evalaute by simpson's method*/
float f2(float x)
{
return (exp(-x));
}
I am trying to write a program to handle some simple numerical integration schemes and I keep having certain errors. Whenever I try to convert the integer types from float to double the program stops working, I am getting accuracy errors using float so I would like to know what I am doing wrong. Also on the monte carlo routine I am trying to generate a random number between a and b and then convert it back to a floating point but if you run the code you can tell it doesnt work. Any general advice in addition would be helpful. I am trying to self teach myself c for grad school and have no formal background in any languages.
Please see code below:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <ctype.h>
/* use of variable declaration outside of main causes them to be global*/
void simp(void);
void trap(void);
void monte(void);
float f1(float x);
float f2(float x);
main ()
{
int choice;
printf("\nHello there and welcome to Jared's integration routine\n"
while(choice != 4)
{
printf("\nWould you prefer to use 1-The Trapezoidal Method (x^3) 2-Simpson's Method (e^(-x)) 3-Monte Carlo 4-exit\n"
scanf("%d", &choice);
switch(choice)
{
case 1: /*Trapezoidal Method for x^3*/
trap();
break;
case 2: /*Simpson's Method*/
simp();
break;
case 3: /*Monte Carlo Method*/
monte();
break;
}
}
printf("\n Goodbye "
}
/*Trapezoidal method*/
void trap(void)
{
float x1, x2, sumt, a, b, step;
printf("\nWhat is your starting point?\n"
scanf("%f", &a);
printf("\nWhat is your end point?\n"
scanf("%f", &b);
printf("\nWhat is your step size?\n"
scanf("%f", &step);
sumt = 0.0;
x1 = a;
printf("%f %f %f", a, b, step);
for (x2 = (a + step) ; x2 <= b; x2 = (x2 + step))
{
sumt += ((step)*(f1(x1) + f1(x2)))/2.0;
x1 = x2;
}
printf("\n The answer is %f", sumt);
return;
}
/*Simpson's Rule*/
void simp(void)
{
float sums, x1, x2, a, b , step;
int i;
printf("\nWhat is your starting point?\n"
scanf("%f", &a);
printf("\nWhat is your end point?\n"
scanf("%f", &b);
printf("\nWhat is your step size?\n"
scanf("%f", &step);
sums = (f2(a) + f2(b));
x1 = a;
for (x2 = (x1 + step); x2 <= b; x2 = (x2 + step))
{
sums += (2.0*f2(x2));
}
sums = (sums * step)/2.0;
printf("\n The answer is %f", sums);
return;
}
/*Monte Carlo Method*/
void monte(void)
{
float summ, yr, f, c = 0.0, rx, rxn, ry, ryn;
int rand();
int i, n, a, b, rxi;
printf("\nWhat is your starting point?\n"
scanf("%f", &a);
printf("\nWhat is your end point?\n"
scanf("%f", &b);
printf("\nHow many iterations would you like?\n"
scanf("%d", &n);
for (i=1; i<=n; ++i)
{
rxi = rand()%(b-a+1)+a;
printf("\n%d", rxi);
rxn = rxi/1.0;
printf("\n%f", rxn);
yr = f1(rxn);
printf("\n%", yr);
ry = rand()/32768.0;
ryn = (yr * ry);
c += (ry <= yr) ? 1.0 :0.;
}
f = (float) c/i;
summ = f*f1(b - a);
printf("\n The answer is %f", summ);
return;
}
/* function to evalaute by trapezoidal method*/
float f1(float x)
{
return ( x * x * x );
}
/* function to evalaute by simpson's method*/
float f2(float x)
{
return (exp(-x));
}