Na Project
Na Project
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<stdlib.h>
void main()
{
float x[20],y[20],f,s,h,d,p;
int j,i,n;
printf("enter the value of n :");
scanf("%d",&n);
printf("enter the elements of x:\n");
for(i=1;i<=n;i++)
{
scanf("%f",&x[i]);
}
printf("enter the elements of y:\n");
for(i=1;i<=n;i++)
{
scanf("%f",&y[i]);
}
h=x[2]-x[1];
printf("Enter the value of f:");
scanf("%f",&f);
s=(f-x[1])/h;
p=1;
d=y[1];
for(i=1;i<=(n-1);i++)
{
for(j=1;j<=(n-i);j++)
{
y[j]=y[j+1]-y[j];
}
p=p*(s-i+1)/i;
d=d+p*y[1];
}
printf("For the value of x=%f THe value is %f",f,d);
getch();
}
OUTPUT-
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main (){
float x[10], y[10], temp = 1, f[10], sum, p;
int i, n, j, k = 0, c;
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]);
}
for (i = 0; i < n; i++)
{
printf ("\n\nenter the value of f(x%d): ", i);
scanf ("%f", &y[i]);
}
printf ("\n\nEnter X for finding f(x): ");
scanf ("%f", &p);
for (i = 0; i < n; i++)
{
temp = 1;
k = i;
for (j = 0; j < n; j++)
{
if (k == j) {
continue;
}
else {
temp = temp * ((p - x[j]) / (x[k] - x[j]));
}}
f[i] = y[i] * temp;
}
for (i = 0; i < n; i++)
{
sum = sum + f[i];
}
printf ("\n\n f(%.1f) = %f ", p, sum);
getch ();
}
OUTPUT-
#include<stdio.h>
#include<math.h>
double f(double x)
{
return 1/(1+x);
}
void main()
{
int n,i;
double a,b,h,x,sum=0,integral;
printf("\nEnter the no. of intervals: ");
scanf("%d",&n);
printf("\nEnter the lower limit: ");
scanf("%lf",&a);
printf("\nEnter the upper limit: ");
scanf("%lf",&b);
h=fabs(b-a)/n;
for(i=1;i<n;i++){
x=a+i*h;
sum=sum+f(x);
}
integral=(h/2)*(f(a)+f(b)+2*sum);
OUTPUT-
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main(){
int n,i;
double a,b,h,x,sum=0,integral;
printf("\nEnter the lower limit: ");
scanf("%lf",&a);
printf("\nEnter the upper limit: ");
scanf("%lf",&b);
printf("Enter the number of intervals = ");
scanf("%d",&n);
h=fabs(b-a)/n;
for(i=1;i<n;i++){
x=a+i*h;
if(i%3==0){
sum=sum+2*f(x);
}
else{
sum=sum+3*f(x);
}
}
integral=(3*h/8)*(f(a)+f(b)+sum);
printf("\nThe integral is: %lf\n",integral);
getch();
}
OUTPUT-
#include<stdio.h>
#include<math.h>
float f(float x)
{
return cos(x) - x*exp(x);
}
void regula (float *x, float x0, float x1, float fx0, float fx1, int *itr)
{
*x = x0 - ((x1 - x0) / (fx1 - fx0))*fx0;
++(*itr);
printf("Iteration no. %3d X = %7.5f \n", *itr, *x);
}
void main ()
{
int itr = 0, maxmitr;
float x0,x1,x2,x3,allerr;
printf("\nEnter the values of x0, x1, allowed error and maximum iterations:\n");
scanf("%f %f %f %d", &x0, &x1, &allerr, &maxmitr);
regula (&x2, x0, x1, f(x0), f(x1), &itr);
do
{
if (f(x0)*f(x2) < 0)
x1=x2;
else
x0=x2;
regula (&x3, x0, x1, f(x0), f(x1), &itr);
if (fabs(x3-x2) < allerr)
{
printf("After %d iterations, root = %6.4f\n", itr, x3);
return 0;
}
x2=x3;
}
while (itr<maxmitr);
printf("Solution does not converge or iterations not sufficient:\n");
return 1;
}
OUTPUT-
Enter the values of x0, x1, allowed error and maximum iterations:
0
1
0.0005
20
Iteration no. 1 X = 0.31467
Iteration no. 2 X = 0.44673
Iteration no. 3 X = 0.49402
Iteration no. 4 X = 0.50995
Iteration no. 5 X = 0.51520
Iteration no. 6 X = 0.51692
Iteration no. 7 X = 0.51748
Iteration no. 8 X = 0.51767
After 8 iterations, root = 0.5177
#include<stdio.h>
#include<conio.h>
#include<math.h>
/* Defining equation to be solved.
Change this equation to solve another problem. */
#define f(x) x*log10(x) - 1.2
int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1;
clrscr();
/* Inputs */
up:
printf("\nEnter two initial guesses:\n");
scanf("%f%f", &x0, &x1);
printf("Enter tolerable error:\n");
scanf("%f", &e);
/* Calculating Functional Values */
f0 = f(x0);
f1 = f(x1);
/* Checking whether given guesses brackets the root or not. */
if( f0*f1 > 0.0)
{
printf("Incorrect Initial Guesses.\n");
goto up;
}
/* Implementing Regula Falsi or False Position Method */
printf("\nStep\t\tx0\t\tx1\t\tx2\t\tf(x2)\n");
do
{
x2 = x0 - (x0-x1) * f0/(f0-f1);
f2 = f(x2);
printf("%d\t\t%f\t%f\t%f\t%f\n",step, x0, x1, x2, f2);
if(f0*f2 < 0)
{
x1 = x2;
f1 = f2;
}
else
{
x0 = x2;
f0 = f2;
}
step = step + 1;
}
while(fabs(f2)>e);
printf("\nRoot is: %f", x2);
getch();
return 0;
}
Step x0 x1 x2 f(x2)
1 2.000000 3.000000 2.721014 -0.017091
2 2.721014 3.000000 2.740206 -0.000384
3 2.740206 3.000000 2.740636 -0.000009
4 2.740636 3.000000 2.740646 -0.000000
#include<stdio.h>
#include<math.h>
int main()
{
int x,i;
int fact = 1,n;
float sum=0;
for(i=1;i<n;i++)
{
fact = fact*i;
sum = sum + (pow(x,i)/fact) ;
}
sum= sum +1; //Since series starts with 1
return 0;
}
OUTPUT-
#include<stdio.h>
#include<math.h>
/*Define the RHS of the first order differential equation here(Ex: dy/dx=f(x,y)) */
double f(double x, double y){
return -2*x-y;
}
void main(){
int i;
double y,xi,yi,xf,h;
printf("Enter the initial condition for y: ");
scanf("%lf",&yi);
printf("Enter the initial condition for x: ");
scanf("%lf",&xi);
printf("Enter the value of x for which y is required: ");
scanf("%lf",&xf);
printf("Enter the step-width h: ");
scanf("%lf",&h);
//Begin Euler Routine
while(xi<xf){
y=yi+h*f(xi,yi);
yi=y;
xi=xi+h;
}
printf("The value of y is %lf\n\n",y);
getch();
OUTPUT-
#include<stdio.h>
#include<math.h>
float f(float x,float y);
int main()
{
float x0,y0,k1,k2,k3,k4,k,y,x,h,n;
printf("Enter the value of x0\n");
scanf("%f",&x0);
printf("Enter the value of y0\n");
scanf("%f",&y0);
printf("Enter the number of term\n");
scanf("%f",&n);
printf("Enter the value of h\n");
scanf("%f",&h);
x=x0;
y=y0;
printf("\n\nX\t\tY\n");
while(x<n)
{
k1=f(x0,y0);
k2=f((x0+h/2.0),(y0+k1*h/2.0));
k3=f((x0+h/2.0),(y0+k2*h/2.0));
k4=f((x0+h),(y0+k3*h));
k=((k1+2*k2+2*k3+k4)/6);
y=y+k*h;
x=x+h;
printf("%f\t%f\n",x,y);
}
}
float f(float x,float y)
{
float k;
k=x+y;
return k;
}
OUTPUT-
#include<stdio.h>
#include<conio.h>
void main()
{
float x[100],y[100],sumx=0,sumx2=0,sumy=0,sumyx=0,b,a;
int i,n;
printf("Enter number of term : ");
scanf("%d",&n: );
for(i=0;i<n;i++){
printf("Enter x[%d] and y[%d]: ",i,i);
scanf("%f%f",&x[i],&y[i]);
}
for(i=0;i<n;i++)
{
sumx=sumx+x[i];
sumx2=sumx2+x[i]*x[i];
sumy=sumy+y[i];
sumyx=sumyx+y[i]*x[i];
}
//for y=ax+b
b=(sumx2*sumy-sumyx*sumx)/(n*sumx2-sumx*sumx);
a=(n*sumyx-sumx*sumy)/(n*sumx2-sumx*sumx);
getch();
}
OUTPUT-
#include<math.h>
#include<stdio.h>
int main()
{
double start_value, end_value, allowed_error, temp;
double y1[30], y2[30], y3[30];
int count;
printf("\nStart Value:\t");
scanf("%lf", &start_value);
printf("\nEnd Value:\t");
scanf("%lf", &end_value);
printf("\nAllowed Error:\t");
scanf("%lf", &allowed_error);
for(temp = start_value, count = 0; temp <= end_value; temp = temp + allowed_error, count++)
{
y1[count] = Y1(temp);
y2[count] = Y2(temp);
y3[count] = Y3(temp);
}
printf("\nX\n");
for(temp = start_value; temp <= end_value; temp = temp + allowed_error)
{
printf("%.4lf ", temp);
}
printf("\n\nY(1)\n");
for(temp = start_value, count = 0; temp <= end_value; temp = temp + allowed_error, count++)
{
printf("%.4lf ", y1[count]);
}
printf("\n\nY(2)\n");
for(temp = start_value, count = 0; temp <= end_value; temp = temp + allowed_error, count++)
{
printf("%.4lf ", y2[count]);
}
printf("\n\nY(3)\n");
for(temp = start_value, count = 0; temp <= end_value; temp = temp + allowed_error, count++)
{
printf("%.4lf ", y3[count]);
}
return 0;
}
Start Value: 0
End Value: 3
Allowed Error: 0.4
X
0.0000 0.4000 0.8000 1.2000 1.6000 2.0000 2.4000 2.8000
Y(1)
1.0000 1.4800 2.1200 2.9200 3.8800 5.0000 6.2800 7.7200
Y(2)
1.0000 1.5045 2.3419 3.7552 6.0645 9.6667 15.0352 22.7205
Y(3)
1.0000 1.5053 2.3692 3.9833 7.1131 13.1333 24.3249 44.2335
#include<stdio.h>
#include<math.h>
#define F(x,y) x+y
void main()
{
int i,n;
float x[20],y[20],h,f,f1,f2,yp,yc,xn;
printf("\n enter the value xn");
scanf("%f",&xn);
printf("\n enter the value:x[i],y[i]:\n");
for(i=0;i<=3;i++)
scanf("%f%f",&x[i],&y[i]);
h=x[1]-x[0];
n=(xn-x[0]/h);
for(i=3;i<=n;i++)
{
x[i+1]=x[i]+h;
f=F(x[i],y[i]);
f1=F(x[i-1],y[i-1]);
f2=F(x[i-2],y[i-2]);
yp=y[i-3]+4.0*h/3.0*(2.0*f2-f1+2.0*f);
yc=y[i-1]+h/3.0*(f1+4.0*f+F(x[i+1],yp));
printf("\n\n predicted Y=%f corrected Y=%f",yp,yc);
if(fabs(yp-yc)<0.00005)
goto next;
yp-yc;
next;
y[i+1]=yc;
printf("\n\n x=%f Y=%f",x[i+1],y[i+1]);
}
return;
}
Output: