Applications
© The scientific sentence. 2010
|
Free fall C program
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double g = 9.81;
float y, to, E;
// First function ---------------
float f(float t)
{
return y - (g/2)*t*t;
}
float f_derivative(float t)
{
return - g*t;
}
typedef float (*PFUN) (float);
float newton (PFUN f, float to, float E){
float ss = 0;
float c[41];
c[0] = to ;
int i,n = 40;
printf("\nThe T values: \n");
for(i=1;i<=n;i++)
{
c[i] = c[i-1] - f(c[i-1])/f_derivative(c[i-1]);
printf("%f \n", c[i]);
if (abs((c[i] - c[i-1])/c[i-1]) <= E/100.0 )
{
printf("\nThe solution is t = %f degrees.\n", c[i]);
ss= c[i];
i = n+1;
}
}
return ss;
}
// Fourth function ---------------------the main -------------
int main()
{
printf("\n Input values for y : --> ");
scanf("%f", &y);
printf("\n Input values for t0 (degrees) and E (percentage): --> ");
scanf("%f%f", &to, &E);
newton(&f,to,E);
return 0;
}
|
|
|