// name: Devin Scott N23 CS407
// Assignment A02
#include <math.h>
#include <stdio.h>
int signof(double x)
{
if (x >= 0)
return 1;
else
return 0;
}
double compute_poly(double x)
{
double result=0;
//result=x;
printf("\n passed value of x was... %f \n", x);
result = result + (pow(x, 4));
printf("\n computing result... %f \n", result);
result = result + (4 * (pow(x,3)) );
printf("\n computing result... %f \n", result);
result = result - (4 * (pow(x,2)) );
printf("\n computing result... %f \n", result);
result = result - (16*x);
printf("\n computing result... %f \n", result);
result = result - 8;
printf("\n computing result... %f \n", result);
return result;
}
int main ()
{
int max_iterations=0;
int num_subintervals=0;
double relative_err=0;
double lower_limit=0;
double upper_limit=0;
double temp=0.0;
double tempx=0.0;
double low_interval = -1.0;
double high_interval = 0.0;
double trial_range=0.0;
double sub_interval=0.0;
double temp_interval=0.0;
double lower_value=0.0;
double upper_value=0.0;
int i=0;
printf ("\n Devin Scott NN: 23 Numerical Analysis\n"); //header
//collect input from user
//input is "lower limit","upper limit"
//"number of subintervals","relative error thresh",
//"maximum number of iterations"
printf ("\n please enter the lower limit \n");
scanf("%lf",&lower_limit);
printf ("\n please enter the upper limit \n");
scanf("%lf",&upper_limit);
printf ("\n please enter the number of sub-intervals\n");
scanf("%d",&num_subintervals);
printf ("\n please enter the relative error threshold \n");
scanf("%lf",&relative_err);
printf ("\n please enter the maximum number of iterations \n");
scanf("%d",&max_iterations);
printf("\n .. lower limit was %e \n",lower_limit);
printf("\n .. upper limit was %e \n",upper_limit);
printf("\n .. number of sub intervals was %d \n",num_subintervals);
printf("\n .. relative error threshold was %e \n",relative_err);
printf("\n .. max iterations was %d \n",max_iterations);
printf("\n .. range of trial interval is from %f to %f \n", lower_limit, upper_limit);
trial_range = abs(( (abs(lower_limit)) - (abs(upper_limit)) ));
printf("\n .. total absolute range of trial interval is %f \n", trial_range);
sub_interval=(trial_range/num_subintervals);
printf("\n .. range of each sub-interval is %f \n", sub_interval);
temp_interval=lower_limit;
for (i=0; i<num_subintervals; i++)
{
printf("\n ..testing sub-interval from %f \n", temp_interval);
lower_value = compute_poly(temp_interval);
temp_interval=temp_interval+sub_interval;
printf("\n ..to .. %f \n", temp_interval);
upper_value = compute_poly(temp_interval);
if ( signof(lower_value) == signof(upper_value) )
{ printf("\n the signs were the same. no root! \n");
}
else
printf("\n sign change detected in the sub-interval %f to %f ", (temp_interval-sub_interval), temp_interval );
}
printf("\n .. polynomial is x^4 + 4x^3 - 4x^2 - 16x - 8 \n please enter a value x to compute");
scanf("%lf",&tempx);
printf("\n the x was %f \n", tempx);
temp = compute_poly(tempx);
printf("\n the result was %f \n", temp);
return 0;
}