C code, root finding algorithm - c

I'm using the bisection method to find the root of function in the domain from 70*10^9 to 250*10^9, but the output is always the upper bound, i.e 250*10^9. The function is a definite integral, I don't know where I did wrong. Thanks in advance.
#include <stdio.h>
#include <math.h>
double I = 0.0225, W = 50000, L = 25, Y = 12000;
double x, E;
typedef double(*DfD)(double);
double g(double), h(double);
double pow(double a, double b);
double g(double x){
return W*x*(x-L/2)-(Y*pow(x,3))/2;
}
double h(double x){
return Y*pow(x,3)/2;
}
double midpoint_int(DfD f, double x0, double x1, int n) {
int i;
double x, dx, sum = 0.0;
dx = (x1-x0)/n;
for (i = 0, x = x0 + dx/2; i < n; i ++, x +=dx)
sum += f(x);
return sum*dx;
}
double deflection (DfD f, double int1, double int2){
int1 = midpoint_int(g, L/2, L, 1000);
int2 = midpoint_int(h, 0, L/2, 1000);
return (int1 - int2)/(E*I)+0.5;
}
double bisection(DfD f, double a0, double a1, double tol ){
double middle;
for (;;){
middle = (a0 + a1)/2.0;
if (fabs(middle - a0) < tol)
return middle;
else if (f(middle) * f(a0) < 0.0)
a1 = middle;
else
a0 = middle;
}
}
int main (void){
double E;
E = bisection (deflection, 70*pow(10,9), 250*pow(10,9), 0.001);
printf("The optimal elastic modulus is %fPa.\n", E);
return;
}

Related

How do we import C code outputs to be used in matlab for visualisation

i have a C code that finds the solution of a function using the methode of bisection and Newton Raphson, and i want to compare the results using graphs ( i am asked to do so in matlab as it's a school project ), but i have no idea how.
here's my code :
`
#include <stdio.h>
#include <math.h>
#include<stdio.h>
#include<math.h>
float F0 (float V)
{
float J0 = 1e-15;
float n = 0.68;
float V0 = 0.025;
int E = 1;
int R = 100;
return (E-V-R*J0*(exp(n*V/V0)-1));
}
float F1 (float V)
{
float J0 = 1e-15;
float n = 0.68;
float V0 = 0.025;
int E = 1;
int R = 100;
return (-1-n*R*J0/V0*exp(n*V/V0));
}
void Dichotomie (float *V, float a, float b, int *itr)
{
*V=(a+b)/2;
++(*itr);
printf("Iteration no. %3d V = %7.5f\n", *itr, *V);
}
void Newton(int itr, int maxmitr, float h, float V0, float V1, float err)
{
for (itr=1; itr<=maxmitr; itr++)
{
h=F0(V0)/F1(V0);
V1=V0-h;
printf("Iteration no. %3d, V = %9.6f\n", itr, V1);
if (fabs(h) < 2*err)
{
printf("After %3d iterations, root = %8.6f\n", itr, V1);
return;
}
V0=V1;
}
printf(" The required solution does not converge or iterations are insufficient\n");
return;
}
int main ()
{
float J0 = 1e-15;
float n = 0.68;
float V0 = 0.025;
int E = 1;
int R = 100;
int itr = 0, maxmitr;
float V, a, b, err, V1;
float h;
a = 0;
b = 1;
err = 0.000001;
maxmitr = 100;
Newton(itr, maxmitr,h,V0,V1,err);
Dichotomie (&V, a, b, &itr);
do
{
if (F0(a)*F0(V) < 0)
b=V;
else
a=V;
Dichotomie (&V1, a, b, &itr);
if (fabs(V1-V) < 2*err)
{
printf("After %d iterations, root = %6.6f\n", itr, V1);
return 0;
}
V=V1;
}
while (itr < maxmitr);
printf("The solution does not converge or iterations are not sufficient");
return 1;
}
`
I read some documentations about this in the Matlab website, and i found that there is a function block in Simulink to be used, but i have no idea how Simulink works.

How to code the summation of a function in C?

EDIT: I've added the main, factorial, and trapGamma function to give the full picture but I am specifically talking about the for loop for iSum in the I function.
Basically I've run out of ideas and exhausted everywhere I know of to find an answer to this. I need to code a program that will compute a complex function which represents an M/M/1 queue.
The function includes sub functions such as calculating the integral of a gamma function and computing factorials. I've written all the code for the computations but my sum is giving me huge numbers when I would expect nothing higher than about .35
#include <math.h>
#include <stdio.h>
double I(int k, double t);
double trapGamma(double z);
unsigned long long int factorial(unsigned int n);
int main()
{
int k;
int i = 0;
double dt = 0.1;
printf("Ikx = [ \n");
for (t = 14.0 ; t <= 15.0; t += dt)
{
printf("%f " , t);
for (k = 1 ; k <= 10 ; k++)
{
I(k, t);
printf("%f " , I(k, t));
}
printf("\n");
}
printf(" ];\n");
return (0);
}
double I(int k, double t)
{
unsigned long long int x;
unsigned int n = 20;
double numerator, y, pow1, c;
double iSum;
double Ix;
int i = 0;
iSum = 0.0;
Ix = 0.0;
a = .25 * pow(t , 2);
b = pow(a, i);
x = factorial(n);
y = trapGamma(k + i + 1);
iSum = (b / (x * y));
//This is the sum loop that I'm having trouble with, I've broke the iSum equation down for my own readability while coding right above this comment
for (i = 0; i <= 100 ; i++)
{
iSum += i;
}
Ix = (pow((.5 * t), k) ) * iSum;
return Ix;
}
/*
I've checked both the factorial and trapGamma functions and they are giving me the expected results.
*/
unsigned long long int factorial(unsigned int n)
{
if(n <= 1)
return 1;
else
return (n * factorial(n - 1));
}
double trapGamma (double z)
{
int i , N = 100;
double gamma;
double a = 0.0;
double b = 15.0;
double x1, x2, y1, y2;
double areai;
double w = (b - a) / N;
gamma = 0.0;
for (i = 1; i < N; i++)
{
x1 = a + ((i - 1) * w); //the left bound point
x2 = a + (i*w); //the right bound point
y1 = pow(x1,z - 1)*exp(-x1); //the height of our left bound
y2 = pow(x2, z - 1)*exp(-x2); //the height of our right bound
areai = ((y1 + y2) / 2.0) * (x2 - x1);
gamma += areai;
}
return gamma;
}
This is building upon another project where I used a bessel function to create the M/M/1 queue over a 60 second span so I can see what this one is supposed to be. I've checked both my trapGamma and factorial functions results on there own and they are both working as expected.
How are summations supposed to be coded?
If the intent of the posted code is to calculate the modified Bessel function I, there are some pitfalls and useful semplifications to be aware of. Given
Trying to calculate the factorial, the value of the Gamma function, their product and the powers separately for each term of the sum leads to integer overflow sooner than later.
It's better to update the value of each addend of the sum instead.
Also, given that k is a whole, we have Γ(n) = (n - 1)!
The addends are increasingly smaller and, after some iterations, too small to be added to the sum, given the limited precision of type double.
// Evaluates x^k / k! trying not to overflow
double power_over_factorial(double x, int k)
{
double result = 1.0;
for ( int i = 1; i <= k; ++i )
{
result *= x / i;
}
return result;
}
#define MAX_ITERS 20
double modified_Bessel_I(int k, double x)
{
x /= 2;
const double xx = x * x;
double partial = power_over_factorial(x, k);
double old_sum, sum = partial;
int m = 1;
do
{
old_sum = sum;
partial *= xx / ((m + k) * m);
sum += partial;
}
while ( old_sum != sum && ++m < MAX_ITERS );
return sum;
}
Testable here.

Calculating cos(x) through Maclaurin series approximation using a factorial function and a cosine one

I have an assignment to code a program to calculate cos(x) through the Maclaurin approximation. However I must use a function for the cos(x) and another one to calculate the exponentials that go on the denominators inside the cos(x) function. I think most of this is right, but I'm probably missing on something and I can't figure out what.
#include<stdio.h>
#include <stdlib.h>
#include <math.h>
int fat(int);
float cosx(float);
int main()
{
float x1;
/* Original code: **x1 = x1 * 3.14159 / 180;** `transforms the value to radians` */
x1 = x1 * 3.14159 / 180; /* transforms the value to radians */
printf("Insert number:\n");
scanf("%f", &x1);
printf("Cosine of %f = %f", x1, cosx(x1));
return 0;
}
int fat(int y)
{
int n, fat = 1;
for(n = 1; n <= y; n++)
{
fat = fat * n;
}
return fat;
}
float cosx(float x)
{
int i=1, a = 2, b, c = 1, e;
float cos;
while(i < 20)
{
b = c * (pow(x,a)) / e;
cos = 1 - b;
a += 2;
e = fat(a);
c *= -1;
i++;
}
return cos;
}
If I input 0 it returns -2147483648.000000, which is clearly wrong.
First error is uninitialized variable x1, and right after that you have use:
int x1; // <<< uninitiated variable;
**x1 = x1 * 3.14159 / 180;** `transforms the value to radians
this will produce random value, you should put
int x = 0; // or some other value of your choice
In my opinion you should move x1 = x1 * 3.14159/100; after scanf("%d", x1).
Than again uninitiated value e before use.
int i=1, a = 2, b, c = 1, e;
...
b = c * (pow(x,a)) / e;
...
than you have in the line b = c * pow(x,a) where you go out of range of int variable potentially. If e = 1, x = 2 and a > 31 you are out of range for b. Another problem is pow(x,a) is rising much faster than `e. thus you get bigger and bigger values thus you are getting another overflow. And here is the code that works:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
long double fact(int);
long double cosx(double);
long double my_pow (double b, int e);
int main()
{
double x1 = 45.00;
printf("Insert number:\n");
scanf("%lf", &x1);
x1 = x1 * 3.14159 / 180; // ** `transforms the value to radians`
printf("Cosine of %f = %.10LF", x1, cosx(x1));
return 0;
}
long double fact(int y)
{
int n;
double fact = 1;
for(n = 1; n <= y; n++)
{
fact *= n;
}
return fact;
}
long double cosx(double x)
{
int a = 2, c = -1;
long i = 0, lim = 500;
long double cos = 1;
long double b = 0, e = 0;
while(i < lim) {
e = fact(a);
b = c * my_pow(x,a);
cos += b/e;
// printf ("%le %le %le\n", e, b, cos);
a += 2;
c *= -1;
i++;
}
return cos;
}
long double my_pow (double b, int e) {
long double pow = 1;
for (;e > 0; --e, pow *= b)
;
return pow;
}

segment fault on programming C

I am tyring to make velocity Verlet method, by using C language.
I thought I made it good. However, there pops up 'Segmentation fault(core dumped)' whenever, I increase the size of the vector or array, x and y.
For the size n equal and less than 1e3, it's fine, but at the point of n = 1e4, the program gets error.
Please anybody help me on this.
Thank you.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double verlet(double t, double x)
{
double E = 0.252;
double B = 0.052;
double a = M_PI/2;
return -sin(x) + E*cos(t) + B*cos(2*t+a);
}
double pverlet(double(*f)(double, double), double dt, double t, double x, double y)
{
return x + dt*( y + (dt/2)*f(t, x));
}
double vverlet(double(*g)(double, double), double dt, double t, double x, double y)
{
return y + (dt/2) * g(t, x);
}
int main(void)
{
int i;
double t;
int n = 1e4;
double ti = 0, tf = 1e5, dt = (tf-ti)/n;
double *x = (double *) malloc(sizeof(double)*n);
double *y = (double *) malloc(sizeof(double)*2*n);
if (x == NULL)
{
printf("error allocating memory!\n");
return 1;
}
if (y == NULL)
{
printf("error allocating memory!\n");
return 1;
}
for (y[0] = 0, i = 1; i <2*n; i++)
{
y[i] = vverlet(verlet, dt, ti + dt*(i-1), x[i-1], y[i-1]);
}
for (x[0] = 0, i = 1; i < n; i++)
{
x[i] = pverlet(verlet, dt, ti + dt*(i-1), x[i-1], y[2*(i-1)]);
}
for (i = 0; i < n; i++)
{
t = ti + dt * i;
printf("%e %e %e\n", t, x[i], y[2*i]);
}
return 0;
free(x);
free(y);
}
for (y[0] = 0, i = 1; i <2*n; i++)
{
y[i] = vverlet(verlet, dt, ti + dt*(i-1), x[i-1], y[i-1]);
}
x is defined from 0 to n-1.

Golden Section Routine Segmentation Fault

I'm trying to find minimum point of Gamma function by Golden Section method. But when I execute the program I get segmentation fault error. I think since I'm a newbie C user, the problem may be due to calling the function Min_Search_Golden_Section wrong. Here is my complete code. I can't find my mistake. Thanks in advance.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <float.h>
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define A 12
#define sqrt5 2.236067977499789696
static int Stopping_Rule(double x0, double x1, double tolerance);
double sp_gamma(double z)
{
const int a = A;
static double c_space[A];
static double *c = NULL;
int k;
double accm;
if ( c == NULL ) {
double k1_factrl = 1.0; /* (k - 1)!*(-1)^k with 0!==1*/
c = c_space;
c[0] = sqrt(2.0*M_PI);
for(k=1; k < a; k++) {
c[k] = exp(a-k) * pow(a-k, k-0.5) / k1_factrl;
k1_factrl *= -k;
}
}
accm = c[0];
for(k=1; k < a; k++) {
accm += c[k] / ( z + k );
}
accm *= exp(-(z+a)) * pow(z+a, z+0.5); /* Gamma(z+1) */
return accm/z;
}
void Min_Search_Golden_Section( double (*f)(double), double* a, double *fa,
double* b, double* fb, double tolerance)
{
static const double lambda = 0.5 * (sqrt5 - 1.0);
static const double mu = 0.5 * (3.0 - sqrt5); // = 1 - lambda
double x1;
double x2;
double fx1;
double fx2;
// Find first two internal points and evaluate
// the function at the two internal points.
x1 = *b - lambda * (*b - *a);
x2 = *a + lambda * (*b - *a);
fx1 = f(x1);
fx2 = f(x2);
// Verify that the tolerance is an acceptable number
if (tolerance <= 0.0) tolerance = sqrt(DBL_EPSILON) * (*b - *a);
// Loop by exluding segments from current endpoints a, b
// to current internal points x1, x2 and then calculating
// a new internal point until the length of the interval
// is less than or equal to the tolerance.
while ( ! Stopping_Rule( *a, *b, tolerance) ) {
if (fx1 > fx2) {
*a = x1;
*fa = fx1;
if ( Stopping_Rule( *a, *b, tolerance) ) break;
x1 = x2;
fx1 = fx2;
x2 = *b - mu * (*b - *a);
fx2 = f(x2);
} else {
*b = x2;
*fb = fx2;
if ( Stopping_Rule( *a, *b, tolerance) ) break;
x2 = x1;
fx2 = fx1;
x1 = *a + mu * (*b - *a);
fx1 = f(x1);
}
}
return;
}
int main()
{
double x;
double a = 0.0, b = 4.0, fa = 0.00001, fb = 6.0;
double fx = sp_gamma(x);
Min_Search_Golden_Section( &fx, &a, &fa, &b, &fb, 0.0000001);
return 0;
}
static int Stopping_Rule(double x0, double x1, double tolerance)
{
double xm = 0.5 * fabs( x1 + x0 );
if ( xm <= 1.0 ) return ( fabs( x1 - x0 ) < tolerance ) ? 1 : 0;
return ( fabs( x1 - x0 ) < tolerance * xm ) ? 1 : 0;
}
You should be getting a compiler error. The first argument to Min_Search_Golden_Section should be a function pointer, but you pass the address of a variable instead.
When you get compiler errors, fix them - don't run the program and hope. :)
I guess you just meant to write:
Min_Search_Golden_Section( &sp_gamma, &a, &fa, &b, &fb, 0.0000001);

Resources