Professional Documents
Culture Documents
NM Lab
NM Lab
#include<stdio.h>
double func(double x) {
return x*x*x-4*x-9;
c = (a+b)/2;
f_a = func(a);
f_c = func(c);
if (f_a*f_c<0){
b=c;
else{
a=c;
return c;
int main(){
double a, b, tol;
int max_iter;
scanf("%lf", &b);
scanf("%d", &max_iter);
return 0;
Output :
Lab:02
Newton raphson method to find the root of x3 -4x:
#include <stdio.h>
#include <math.h>
double func(double x) {
return x*x*x-4*x-9;
double derivative(double x) {
return 3*x*x-4;
double x = x0;
x_prev = x;
double f = func(x_prev);
if(f_prime== 0) {
return x;
x=x_prev - f/f_prime;
if(fabs(x-x_prev)<epsilon) {
printf("Convergence achived.\n");
return x;
}
}
return x;
int main(){
int max_iter;
scanf("%lf", &x0);
scanf("%lf",&epsilon);
scanf("%d", &max_iter);
return 0;
Output:
Lab:03
Regulafalsi Method to find the root of x3 -4x:
#include <stdio.h>
#include <math.h>
double func(double x) {
return x*x*x-4*x-9;
f_a = func(a);
f_c = func(c);
if(f_a*f_c>0) {
a=c;
}else{
b=c;
c=(a*func(b)-b*func(a))/(func(b)-func(a));
return c;
int main() {
double a,b;
int max_iter;
scanf("%lf", &a);
scanf("%d", &max_iter);
return 0;
Output: