In C Programming: NOT in JAVA OR C++ CODE the function Sec(x) using Newton Raphs
ID: 3822596 • Letter: I
Question
In C Programming: NOT in JAVA OR C++
CODE the function Sec(x) using Newton Raphson.
Search for, Select, and Define a numerical algorithm
Test the algorithm manually using a spreadsheet or calculator
Write a pseudocode version for the algorithm
Implement the pseudocode as a C function, and a main() function that calls it
Debug the code
Test the algorithm with a minimum of 50 test cases including extremes and invalid inputs.
Note: The algorithm must have the following characteristics:
* Use floating point variables (doubles are preferred over float variables)
* Use iteration to converge on a solution
* Multiple test cases must be used to demonstrate correct operation of the algorithm
Explanation / Answer
Answer ->
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
float function(float);
float differantaion(float);
int main()
{
int itr, mitr;
float h, x0, x1, allerr;
printf(" Enter x0, allowed error and maximum iterations ");
scanf("%f %f %d", &x0, &allerr, &mitr);
for (itr=1; itr<=mitr; itr++)
{
h=function(x0)/differantaion(x0);
x1=x0-h;
printf(" At Iteration no. %3d, x = %f ", itr, x1);
if (fabs(h) < allerr)
{
printf("After %3d iterations, root = %f ", itr, x1);
return 0;
}
x0=x1;
}
printf(" The required solution does not converge or iterations are insufficient ");
return 0;
}
float function(float x)
{
return (1/cos(x));
}
float differantaion(float x)
{
return ((sin(x))/(cos(x)*cos(x)));
}