Search This Blog

Showing posts with label nsm. Show all posts
Showing posts with label nsm. Show all posts

Sunday, 28 February 2016

Netwon_Raphson Method ( Non_Bracketing Method )

#include<stdio.h>
#include<math.h>
float fun(float x)
{
    float fun;
    fun=(x*x*x)-(5*x)+1;
    return fun;
}
float dfun(float x)
{
    float dfun;
    dfun=3*(x*x)-5;
    return dfun;
}
void main()
{
float x,xo,y,yd,d,xao=0,eps;
int i;
printf("Enter x= ");
scanf("%f",&x);
printf("Enter error limit=");
scanf("%f",&eps);
i=0;
xo=x;
yd=dfun(x);
if(fabs(yd)<=0)
{
    printf("x do not bracket any rule");
    return;
}
  do
    {
        i=i+1;
        x=xo;
        y=fun(x);
        yd=dfun(x);
        xo=x-(y/yd);
        d=fabs(xo-xao);
        printf("\ni=%d\nx=%f\ty=%f\tyd=%f\txa=%f\td=%f",i,x,y,yd,xo,d);
        xao=xo;
    }
    while(d>=eps);
    if(d<=eps)
    {
        printf("\nAns:The root of the equation is x = %f with error of %f",xo,d);
    }



}

False_Position Method ( Bracketing Method)

#include<stdio.h>
#include<math.h>
float fun(float x)
{
    float fun;
    fun=(2*sin(x))-x;
    return fun;
}
void main()
{
float xl,xu,fl,fu,eps,xr,fr,d,xro=0;
int i,n;
printf("Enter xl & xu = ");
scanf("%f %f",&xl,&xu);
printf("Enter error limit=");
scanf("%f",&eps);
fl=fun(xl);
fu=fun(xu);
printf("fl=%f ,fu=%f",fl,fu);

if(fl*fu>0)
{
    printf("xl & xu do not bracket any rule");
    return;
}
    i=1;
    xr=((xu*fl)-(xl*fu))/(fl-fu);
    d=fabs(xr-xro);
    printf("\nxl=%f,xu=%f,\nroot=%f , d=%f, i=%d",xl,xu,xr,d,i);
  do
    {
        i=i+1;
        fr=fun(xr);
        if(fl*fr>=0)
        {
           xl=xr;
           fl=fr;
        }
        else
        {
           xu=xr;
           fu=fr;
        }
        xr=((xu*fl)-(xl*fu))/(fl-fu);
        d=fabs(xr-xro);
        printf("\nxl=%f,xu=%f,\nroot=%f , d=%f, i=%d",xl,xu,xr,d,i);
        xro=xr;
    }
    while(d>=eps);
    if(d<=eps)
    {
        printf("\nAns:The root of the equation is x = %f with error of %f",xr,d);
    }
}