secant method

 avatar
user_7016102
plain_text
a month ago
643 B
2
Indexable
Never
//secant method  numerical method lab 2

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define F(x)(a3*x*x*x+a2*x*x+a1*x+a0)
int a3,a2,a1,a0;
float f(float x){
	return (a3*x*x*x+a2*x*x+a1*x+a0);
}
int main()
{
	float x0,x1,x2,fx0,fx1,E,Era;
	printf("Enter the coefficients a3,a2,a1 and a0 :\n");
	scanf("%d%d%d%d",&a3,&a2,&a1,&a0);
	printf("Enter two guesses and E : \n");
	scanf("%f%f%f",&x0,&x1,&E);

		while(1){
			fx0 = F(x0);
			fx1 = F(x1);
			x2 = x1-(fx1*(x1-x0))/(fx1-fx0);
			Era = (x2-x1)/x2;
			if(fabs(Era)<E){
				printf("Root = %f",x2);
				break;
			}
			x0=x1;
			x1=x2;
		}
		getch();
	}
Leave a Comment