forked from skb1129/nmst-programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnewton-raphson.cpp
More file actions
40 lines (40 loc) · 799 Bytes
/
newton-raphson.cpp
File metadata and controls
40 lines (40 loc) · 799 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<stdio.h>
//NEWTON-RAPHSON METHOD
float a,b,c,d,a2,b2,c2;
float f(float x){
float res=a*x*x*x+b*x*x+c*x+d;
return res;
}
float fd(float x){
a2=3*a;b2=2*b;
float res=a2*x*x+b2*x+c;
return res;
}
int main(){
int n,i;
float x,y,k[100];
printf("Enter the constants of biquadratic equation :\n");
scanf("%f%f%f%f",&a,&b,&c,&d);
printf("1.Enter the limits\n2.Enter the root\n");
scanf("%d",&n);
if(n==1){
printf("Enter the limits :\n");
scanf("%f%f",&x,&y);
if(f(x)*f(y)>=0){
printf("Wrong Input. Restarting!");
main();
}
k[0]=(x+y)/2;
}
else if(n==2){
printf("Enter the root : ");
scanf("%f",&k[0]);
}
printf("Enter the number of iterations : ");
scanf("%d",&n);
for(i=0;i<n;i++){
k[i+1]=k[i]-f(k[i])/fd(k[i]);
}
printf("Root : %f",k[n-1]);
return 0;
}