====================================
1
====================================
#include <stdio.h>
int main()
{
double l,ff ,xb;
int i,k,n ,n1 ,j ;
n=3;
double x[4]={ 5, 6, 9, 11 };
double f[4]={ 12, 13, 14, 16 };
xb=7;
//printf("The value of p(%.4lf)\n",xb);
ff=0.0;
for(k=0;k<=n;k++)
{
l=1.0;
for(i=0;i<=n;i++)
{
if(i !=k)
{
l=l*(xb-x[i])/(x[k]-x[i]);
}
printf("k=%1d , i=%1d , l=%.2lf ff=%.4lf\n ",k,i,l ,ff);
}
ff=ff+l*f[k];
printf(" ff=%.4lf\n",ff);
}
printf("The value of p(%.4lf)=%.4lf\n",xb,ff);
return 0;
}
k=0 , i=0 , l=1.00 ff=0.0000
k=0 , i=1 , l=-1.00 ff=0.0000
k=0 , i=2 , l=-0.50 ff=0.0000
k=0 , i=3 , l=-0.33 ff=0.0000
ff=-4.0000
k=1 , i=0 , l=2.00 ff=-4.0000
k=1 , i=1 , l=2.00 ff=-4.0000
k=1 , i=2 , l=1.33 ff=-4.0000
k=1 , i=3 , l=1.07 ff=-4.0000
ff=9.8667
k=2 , i=0 , l=0.50 ff=9.8667
k=2 , i=1 , l=0.17 ff=9.8667
k=2 , i=2 , l=0.17 ff=9.8667
k=2 , i=3 , l=0.33 ff=9.8667
ff=14.5333
k=3 , i=0 , l=0.33 ff=14.5333
k=3 , i=1 , l=0.07 ff=14.5333
k=3 , i=2 , l=-0.07 ff=14.5333
k=3 , i=3 , l=-0.07 ff=14.5333
ff=13.4667
The value of p(7.0000)=13.4667
=========================================
2
=========================================
// x[] = { 5, 6, 9, 11 }; y[]={ 12, 13, 14, 16 }
#include <stdio.h>
void main()
{
int i,j,n;
double f[40][40],x[40];
scanf("n=%d",&n);
for(i=0;i<=n;i++)
{
scanf("%lf %lf ",&x[i],&f[i][0]);
}
printf("\n Divided Difference Table:\n");
printf(" =========================\n");
for(j=1;j<=n;j++)
{
for(i=0;i<=n-j;i++)
{
f[i][j]=(f[i+1][j-1]-f[i][j-1])/(x[i+j]-x[i]);
}
}
printf("i x(i) f(i) f(i,i+1) f(i,i+1.i+2), ......\n");
for(i=0;i<=n;i++)
{
printf("%d %8.1lf ",i,x[i]);
for(j=0;j<=n-i;j++)
{
printf("%8.1lf ",f[i][j]);
}
printf("\n");
}
return;
}
=========================
i x(i) f(i) f(i,i+1) f(i,i+1.i+2), ......
0 5.0 12.000 1.000 -0.167 0.050
1 6.0 13.000 0.333 0.133
2 9.0 14.000 1.000
3 11.0 16.000
Pn(x)=12 + 1 (x-5) + -0.167 (x-5)(x-6) + 0.05(x-5)(x-6)(x-9)
Pn(10)=
=========================================
3
=========================================
#include <stdio.h>
int main()
{
double l,ff ,xb;
int i,k,n ,n1 ,j ;
n=3;
double x[4]={0.0 ,1.0,2.0 ,3.0};
double f[4]={0.0 , -3.0 , 0.0 , -15.0 };
xb=1.5;
//printf("The value of p(%.4lf)\n",xb);
ff=0.0;
for(k=0;k<=n;k++)
{
l=1.0;
for(i=0;i<=n;i++)
{
if(i !=k)
{
l=l*(xb-x[i])/(x[k]-x[i]);
}
printf("k=%1d , i=%1d , l=%.2lf ",k,i,l);
}
ff=ff+l*f[k];
printf(" ff=%.4lf\n",ff);
}
printf("The value of p(%.4lf)=%.4lf\n",xb,ff);
return 0;
}
==========================================
k=0 , i=0 , l=1.00 f=0.0000
k=0 , i=1 , l=-0.50 ff=0.0000
k=0 , i=2 , l=-0.12 ff=0.0000
k=0 , i=3 , l=-0.06 ff=0.0000
k=1 , i=0 , l=1.50 ff=0.0000
k=1 , i=1 , l=1.50 ff=0.0000
k=1 , i=2 , l=0.75 ff=0.0000
k=1 , i=3 , l=0.56 ff=-1.6875
k=2 , i=0 , l=0.75 k=2 , i=1 , l=0.38 k=2 , i=2 , l=0.38 k=2 , i=3 , l=0.56 ff=-1.6875
k=3 , i=0 , l=0.50 k=3 , i=1 , l=0.12 k=3 , i=2 , l=-0.06 k=3 , i=3 , l=-0.06 ff=-0.7500
The value of p(1.5000)=-0.7500
int main()
{
double l,ff ,xb;
int i,k,n ,n1 ,j ;
n=3;
double x[4]={ 0, 1, 2, 3 };
double f[4]={ 0, -3, 0, 15 };
xb=1.5;
//printf("The value of p(%.4lf)\n",xb);
ff=0.0;
for(k=0;k<=n;k++)
{
l=1.0;
for(i=0;i<=n;i++)
{
if(i !=k)
{
l=l*(xb-x[i])/(x[k]-x[i]);
}
printf("k=%1d , i=%1d , l=%.2lf ff=%.4lf\n ",k,i,l ,ff);
}
ff=ff+l*f[k];
printf(" ff=%.4lf\n",ff);
}
printf("The value of p(%.4lf)=%.4lf\n",xb,ff);
return 0;
}
k=0 , i=0 , l=1.0000 ff=0.0000
k=0 , i=1 , l=-0.5000 ff=0.0000
k=0 , i=2 , l=-0.1250 ff=0.0000
k=0 , i=3 , l=-0.0625 ff=0.0000
ff=0.0000
k=1 , i=0 , l=1.5000 ff=0.0000
k=1 , i=1 , l=1.5000 ff=0.0000
k=1 , i=2 , l=0.7500 ff=0.0000
k=1 , i=3 , l=0.5625 ff=0.0000
ff=-1.6875
k=2 , i=0 , l=0.7500 ff=-1.6875
k=2 , i=1 , l=0.3750 ff=-1.6875
k=2 , i=2 , l=0.3750 ff=-1.6875
k=2 , i=3 , l=0.5625 ff=-1.6875
ff=-1.6875
k=3 , i=0 , l=0.5000 ff=-1.6875
k=3 , i=1 , l=0.1250 ff=-1.6875
k=3 , i=2 , l=-0.0625 ff=-1.6875
k=3 , i=3 , l=-0.0625 ff=-1.6875
ff=-2.6250
The value of p(1.5000)=-2.6250
===========================================
4
===========================================
#include <stdio.h>
#include <stdlib.h>
// x[]={ 0, 1, 2 }; f[]={ 0, -3, 0} ,一階導數 f’[]={ -4, -1, 8}
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int i,j,k,n;
double x[30],z[30],q[30][30],f[30],ff[30];
printf("input n=");
scanf("n=%d",&n);
printf("\n");
for(i=0;i<=n+1;i++)
{
printf("\ninput x=");
scanf("%lf",&x[i]);
printf("\ninput f=");
scanf("%lf",&f[i]);
printf("\ninput ff=");
scanf("%lf",&ff[i]);
}
printf("i z(i) f(i) f(i-1,i) f(i-2,i-1,i)...\n");
for(i=0;i<=n;i++)
{
z[2*i]=x[i];
z[2*i+1]=x[i];
q[2*i][0]=f[i];
q[2*i+1][0]=f[i];
q[2*i+1][1]=ff[i];
if(i !=0)
q[2*i][1]=(q[2*i][0]-q[2*i-1][0])/(z[2*i]-z[2*i-1]);
if(i==0)
printf("%d %3.1lf %7.4lf\n",i,z[i],q[i][0]);
else if(i==1)
printf("%d %3.1lf %7.4lf %7.4lf\n",i,z[i],q[i][0],q[i][1]);
}
for(i=2;i<=2*n+1;i++)
{
printf("%d %3.1lf %7.4lf %7.4lf",i,z[i],q[i][0],q[i][1]);
for(j=2;j<=i;j++)
{
q[i][j]=(q[i][j-1]-q[i-1][j-1])/(z[i]-z[i-j]);
printf(" %7.4lf",q[i][j]);
}
printf("\n");
}
return 0;
}
===========================================
input ff=i z(i) f(i) f(i-1,i) f(i-2,i-1,i)...
0 0.0 0.0000input ff=i z(i) f(i) f(i-1,i) f(i-2,i-1,i)...
1 0.0 0.0000 -4.0000
2 1.0 -3.0000 -3.0000 1.0000
3 1.0 -3.0000 -1.0000 2.0000 1.0000
4 2.0 0.0000 3.0000 4.0000 1.0000 0.0000
5 2.0 0.0000 8.0000 5.0000 1.0000 0.0000 0.0000
===========================================
5
===========================================
//function used is x^3-2x^2+3
double func(double x)
{
return x*x*x - 2*x*x + 3;
}
double e=0.1;
double c;
void bisection(double a,double b)
{
if(func(a) * func(b) >= 0)
{
printf("Incorrect a and b");
return;
}
c = a;
while ((b-a) >= e)
{
c = (a+b)/2;
if (func(c) == 0.0){
printf("Root = %lf\n",c);
break;
}
else if (func(c)*func(a) < 0){
printf("Root =%lf\n",c);
b = c;
}
else{
printf("Root =%lf\n",c);
a = c;
}
}
}
int main()
{
double a,b;
a=-4;
b=4;
printf("The function used is x^3-2x^2+3\n");
printf("a = %lf\n",a);
printf("b = %lf\n",b);
bisection(a,b);
printf("\n");
printf("Accurate Root calculated is = %lf\n",c);
return 0;
}
================================
The function used is x^3-2x^2+3a = -4.000000
b = 4.000000
Root =0.000000
Root =-2.000000
Root = -1.000000
Accurate Root calculated is = -1.000000
沒有留言:
張貼留言