You are on page 1of 13

http://books.google.co.in/books?

id=f4f2wNPv5hYC&pg=PA60&lpg=PA60&dq=schur's+l
emma+theorem&source=bl&ots=2EuTRX6O5d&sig=sxKUXH18pZ0xoDpl4QFeG10bGRI&hl=en&s
a=X&ei=OJeMUZKELMrZrQf33IHAAw&ved=0CEcQ6AEwAw#v=onepage&q=schur's%20lemma%20t
heorem&f=false

/*
Program to Implement BISECTION METHOD.
EQN -> x*log10(x) - 1.2
*/

#include<stdio.h>
#include<conio.h>
#include<math.h>

#define F(x) (x)*log10(x)-1.2
int n;
float root=1;

void bisect();

void main()
{
clrscr();
printf("\n Solution by BISECTION METHOD ");
printf("\n Equation is -> x*log(x) - 1.2 = 0\n");
printf("\n Enter the no. of iterations ");
scanf("%d",&n);
bisect();
getch();
}

void bisect()
{
int count;
float x1,x2,x0;
float f1,f2,f0;
// Finding x2
for(x2=1; ;x2++)
{
f2=F(x2);
if(f2>0)
{
break;
}
}
printf("\n x2 = %d ",x2);
// Finding x1
for(x1=x2-1; ;x1--)
{
f1=F(x1);
if(f1<0)
{
break;
}
}
printf("\n x1 = %d ",x1);
printf("\n ----------------------------------------------------");
printf("\n \tIteration No. \t - ROOTS ");
printf("\n ----------------------------------------------------\n");



for(count=1;count<=n;count++)
{
x0=(x1+x2)/2;
f0=F(x0);
if(f0==0)
{
root=x0;
}
if(f0*f1<0)
{
x2=x0;
f2=f0;
}
else
{
x1=x0;
f1=f0;
}
printf("\n \t ITERATION %d \t : \t %f",count,x0);
if( fabs((x1-x2)/2) < 0.00000005)
{
printf("\n -----------------------------------------------");
printf("\n \t \t \tROOT = %f ",x0);
printf("\n \t \t \tITERATIONS = %d ",count);
printf("\n -----------------------------------------------");
exit(0);
}
}
printf("\n ----------------------------------------------------");
printf("\n \t \t ROOT = %7.4f ",x0);
printf("\n \t \t ITERATIONS = %d ",count-1);
printf("\n ----------------------------------------------------");
}













Eulers method ----------
#include<stdio.h>
#include <math.h>
#include<conio.h>
//dy/dx = xy
#define F(x,y) (x)*(y)
void main()
{
double y1,y2,x1,a,n,h;
int j;
clrscr();
printf("\nEnter the value of range: ");
scanf("%lf %lf",&a,&n);
printf("\nEnter the value of y1: ");
scanf("%lf",&y1);
printf("\n\nEnter the h: ");
scanf("%lf",&h);
printf("\n\n y1 = %.3lf ",y1);
for(x1=a,j=2; x1<=n+h; x1=x1+h,j++)
{
y2= y1 + h * F(x1,y1);
printf("\n\n x = %.3lf => y%d = %.3lf ",x1,j,y2);
y1=y2;
}
getch();
}














/*Gauss Elimination */
# include <stdio.h>
# include <conio.h>
# include <math.h>
# define MAX 10
void main()
{
int i,j,n,k;
float mat[MAX][MAX],x[MAX],temp,pivot,sum=0;
clrscr();
printf("\t\t\t GAUSS ELIMINITION METHOD\n");
printf("-------------------------------------------------------------------
\n");
printf("Enter No of Equtions : ");
scanf("%d",&n);
printf("Enter Coefficients of Eqution \n");
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
scanf("%f",&mat[i][j]);
printf("Enter Constant value\n");
for(i=1;i<=n;i++)
{
scanf("%f",&mat[i][n+1]);
x[i]=mat[i][n+1];
}
for(i=2;i<=n;i++)
{
for(j=i;j<=n;j++)
{
pivot=mat[j][i-1]/mat[i-1][i-1];
for(k=i-1;k<=n+1;k++)
mat[j][k]=mat[j][k]-pivot*mat[i-1][k];
}
}
printf("Eliminated matrix as :- \n");
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
printf("\t%.2f",mat[i][j]);
printf("\n");
}
for(i=1;i<=n;i++)
{
if(mat[i][i]==0)
{
printf("Since diagonal element become zero\n Hence solution is not
possible\n");
exit(1);
}
}
printf("Solution : \n");
for(i=0;i<n;i++)
{
sum=0;
for(j=n;j>n-i;j--)
sum=sum+mat[n-i][j];

x[n-i]=(mat[n-i][n+1]-sum*x[n])/mat[n-i][n-i];
printf("X%d = %4.2f\n",n-i,x[n-i]);
}

getch();
}























/*
Program to Implement NEWTON RAPHSON METHOD to solve eqn.
EQN -> 3*x - cos(x) - 1 = 0.
*/

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<process.h>

// Formulae Declaration
#define f(x) 3*x - cos(x) - 1
#define df(x) 3+sin(x)
int n;

void NEW_RAP();

void main()
{
clrscr();
printf("\n Solution by NEWTON RAPHSON METHOD ");
printf("\n\n Equation is -> 3*x - cos(x) - 1 = 0\n");
printf("\n Enter the no. of iterations ");
scanf("%d",&n);
NEW_RAP();
getch();
}

void NEW_RAP()
{
float x1,x0;
float f1,f0;
float df0;
int i=1,itr;
float EPS,error;

/* finding an approximate Root of a given equation, having +ve value*/
for(x1=0.0;;x1+=0.01)
{
f1=f(x1);
if(f1>0)
break;
}
itr=n;
x0=x1-0.01;
f0=f(x0);
printf("\n Enter the Maximum possible error: ");
scanf("%f",&EPS);
if(fabs(f0)>f1)
printf("\n\t\t The root is near to %.4f\n\n\n",x1);
if(f1>fabs(x0))
printf("\n\t\t The root is near to %.4f\n\n\n",x0);
x0=(x0+x1)/2;
for(;i<=itr;i++)
{
f0=f(x0);
df0=df(x0);
x1=x0-(f0/df0);
printf("\n\t\t The %d approximation to the root is : %f",i,x1);
error=fabs(x1-x0);
if(error<EPS)
break;
x0=x1;
}
if(error>EPS)
{
printf("\n\n\t NOTE :-");
printf(" The No. of Iterartions are not sufficient. ");
}
printf("\n\n\n\t\t----------------------------------------------------");
printf("\n\t\t ROOT = %.4f ",x1);
printf("\n\t\t----------------------------------------------------");
}




















Range kutta method
#include<stdio.h>
#include<conio.h>
#define MAX 20

float equation(float x,float y)
{
return(x*y);
}

void main()
{
FILE *fp;
int i=1,count=1;
float lower,upper,y1,h,xvalue[MAX],yvalue[MAX];
float s,s1,s2;
fp=fopen("rk_2.dat","w");
clrscr();
printf("\n\n");
fprintf(fp,"\n\n");
printf("Runge-Kutta Second Order ");
fprintf(fp,"Runge-Kutta Second Order ");
printf("\n\n");
fprintf(fp,"\n\n");
printf("\nEnter the lower bound of x : ");
fprintf(fp,"\nEnter the lower bound of x : ");
scanf("%f",&lower);
fprintf(fp,"%f",lower);
printf("\nEnter the upper bound of x : ");
fprintf(fp,"\nEnter the upper bound of x : ");
scanf("%f",&upper);
fprintf(fp,"%f",upper);
printf("\nEnter the value of y(lower): ");
fprintf(fp,"\nEnter the value of y(lower): ");
scanf("%f",&y1);
fprintf(fp,"%f",y1);
printf("\nEnter the value of h : ");
fprintf(fp,"\nEnter the value of h : ");
scanf("%f",&h);
fprintf(fp,"%f",h);
xvalue[i]=lower;
yvalue[i]=y1;
for(i=1;xvalue[i]<=upper;i++)
{
xvalue[i+1]=xvalue[i]+h;
count=count+1;
}
for(i=1;i<=count;i++)
{
s1=equation(xvalue[i],yvalue[i]);
s2=equation(xvalue[i]+h,yvalue[i]+(h*s1));
s=(s1+s2)/2;
yvalue[i+1]=yvalue[i]+(h*s);
}
printf("\n\n");
fprintf(fp,"\n\n");
printf("The complete solution of the differential equation is ");
fprintf(fp,"The complete solution of the differential equation is ");
printf("\n\n");
fprintf(fp,"\n\n");
for(i=1;i<=count;i++)
{
printf(" %d %.4f %.4f ",i,xvalue[i],yvalue[i]);
fprintf(fp," %d %.4f %.4f ",i,xvalue[i],yvalue[i]);
printf("\n");
fprintf(fp,"\n");
}
fclose(fp);
getch();
}




















Range kutta method 4
#include<stdio.h>
#include <math.h>
#include<conio.h>
//dy/dx = 1 + y^2
#define F(x,y) 1 + (y)*(y)
void main()
{
double y0,x0,y1,n,h,f,k1,k2,k3,k4;
clrscr();
printf("\nEnter the value of x0: ");
scanf("%lf",&x0);
printf("\nEnter the value of y0: ");
scanf("%lf",&y0);
printf("\nEnter the value of h: ");
scanf("%lf",&h);
printf("\nEnter the value of last point: ");
scanf("%lf",&n);
for(; x0<n; x0=x0+h)
{
f=F(x0,y0);
k1 = h * f;
f = F(x0+h/2,y0+k1/2);
k2 = h * f;
f = F(x0+h/2,y0+k2/2);
k3 = h * f;
f = F(x0+h/2,y0+k2/2);
k4 = h * f;
y1 = y0 + ( k1 + 2*k2 + 2*k3 + k4)/6;
printf("\n\n k1 = %.4lf ",k1);
printf("\n\n k2 = %.4lf ",k2);
printf("\n\n k3 = %.4lf ",k3);
printf("\n\n k4 = %.4lf ",k4);
printf("\n\n y(%.4lf) = %.3lf ",x0+h,y1);
y0=y1;
}
getch();
}








Simsons 1/3
rd
rule
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[10],y[10],sum=0,h,temp;
int i,n,j,k=0;
float fact(int);
clrscr();
printf("\nhow many record you will be enter: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\n\nenter the value of x%d: ",i);
scanf("%f",&x[i]);
printf("\n\nenter the value of f(x%d): ",i);
scanf("%f",&y[i]);
}
h=x[1]-x[0];
n=n-1;
sum = sum + y[0];
for(i=1;i<n;i++)
{
if(k==0)
{
sum = sum + 4 * y[i];
k=1;
}
else
{
sum = sum + 2 * y[i];
k=0;
}
}
sum = sum + y[i];
sum = sum * (h/3);
printf("\n\n I = %f ",sum);
getch();
}







Simsons 3/8
th
rule


#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[10],y[10],sum=0,h,temp;
int i,n,j,k=0,l=0;
float fact(int);
clrscr();
printf("\nhow many record you will be enter: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\n\nenter the value of x%d: ",i);
scanf("%f",&x[i]);
printf("\n\nenter the value of f(x%d): ",i);
scanf("%f",&y[i]);
}
h=x[1]-x[0];
n=n-1;
sum = sum + y[0];
for(i=1;i<n;i++)
{
if(k==0 || l==0)
{
sum = sum + 3 * y[i];
if(k==1)
{
l=1;
}
k=1;
}
else
{
sum = sum + 2 * y[i];
k=0;
l=0;
}
}
sum = sum + y[i];
sum = sum * (3*h/8);
printf("\n\n I = %f ",sum);
getch();
}






Trapizoidal rule
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[10],y[10],sum=0,h,temp;
int i,n,j,k=0;
float fact(int);
clrscr();
printf("\nhow many record you will be enter: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\n\nenter the value of x%d: ",i);
scanf("%f",&x[i]);
printf("\n\nenter the value of f(x%d): ",i);
scanf("%f",&y[i]);
}
h=x[1]-x[0];
n=n-1;
for(i=0;i<n;i++)
{
if(k==0)
{
sum = sum + y[i];
k=1;
}
else
sum = sum + 2 * y[i];
}
sum = sum + y[i];
sum = sum * (h/2);
printf("\n\n I = %f ",sum);
getch();
}

You might also like