BÀI SỐ 1:
1.1 Viết chương trình tính giá trị đa thức p(x) bậc n tổng quát theo sơ đồ
Hoocner.
CHƯƠNG TRÌNH:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 20
float hoocner(float heso[],int n,float c)
{
int i;
float p;
p=heso[0];
for(i=1;i<=n;i++)
p=p*c+heso[i];
return p;
}
main()
{
int i,n;
float x;
float heso[max];
printf("nhap vao bac n:");
scanf("%d",&n);
printf("nhap vao he so da thuc: \n");
for(i=0;i<=n;i++)
{
printf("heso[%d] = ",i);
scanf("%f",& heso[i]);
}
printf("nhap vao gia tri c can tinh: ");
scanf("%f",&x);
printf("\nGia tri cua da thuc can tim la: %3f",hoocner(heso,n,x));
getch();
}
KẾT QUẢ:
1.2 Viết chương trình thực hiện:
Khai báo (định nghĩa) hàm tính giá trị đa thức p(x) bậc n tổng quát theo
sơ đồ Hoocner
Nhập vào đa thức p(x) bậc n và 2 giá trị thực y, z. Tính:
S1 = p(y) + p(z)
S2 = p(1) + p(2) + … + p(n)
Nhập vào 2 đa thức p
n
(x) bậc n, p
m
(y) bậc m và giá trị c. Tính p
n
(c) +
p
m
(c)
CHƯƠNG TRÌNH:
#include<stdio.h>
#include<conio.h>
double heso1[50], heso2[50], a, b,y,z;
int n, m,i;
double s1;
float s2;
double p(int n, double heso[], double x)
{
double k=heso[0];
int i;
for (i=1;i<=n;i++)
k=k*x+heso[i];
return k;
}
void nhap()
{
printf("Nhap bac da thuc pn: ");
scanf("%d", &n);
printf("\nNhap cac he so da thuc pn: \n");
for (i=0;i<=n;i++)
{
printf("heso1[%d]= ",i);
scanf("%lf",&heso1[i]);
}
printf("\nNhap bac da thuc pm: ");
scanf("%d", &m);
printf("\nNhap he so da thuc pm: \n");
for (int i=0;i<=m;i++)
{
printf("heso2[%d]= ",i);
scanf("%lf", &heso2[i]);
}
printf("tinh pn(a)+pm(b): \n");
printf("a= "); scanf("%lf", &a);
printf("b= "); scanf("%lf", &b);
}
int nhapdathuc()
{
printf("\nNhap bac da thuc p: ");
scanf("%d", &n);
printf("\nNhap cac he so da thuc p:\n");
for (i=0;i<=n;i++)
{
printf("heso1[%d]= ",i);
scanf("%lf",&heso1[i]);
}
printf("\ntinh p(y)+p(z): \n");
printf("y= "); scanf("%lf", &y);
printf("z= "); scanf("%lf", &z);
s1=p(n,heso1,y)+p(n,heso1,z);
s2=p(n,heso1,1);
for(i=2;i<=n;i++)
{
s2=s2+p(n,heso1,i);
}
}
main()
{
nhap();
printf("pn(%.3lf)+pm(%.3lf)= %.3lf", a, b, p(n, heso1, a)+p(m, heso2, b));
nhapdathuc();
printf("\nGia tri cua p(y)+p(z) la: ");
printf("s1=%.3lf",s1);
printf("\nGia tri cua p(1)+p(2)+ +p(n) la: ");
printf("s2=%f",s2);
getch();
}
KẾT QUẢ:
1.3Cho đa thức p(x) bậc n, viết chương trình xác định các hệ số của đa thức
p(y+c) theo sơ đồ Hoocner tổng quát.
CHƯƠNG TRÌNH:
#include<stdio.h>
#include<conio.h>
#define max 100
float A[max],c;
int n;
void nhaphamso()
{
int i;
printf("Nhap n: "); scanf("%d",&n);
for(i=0;i<=n;i++)
{
printf("Nhap A[%d]: ",i);
scanf("%f",&A[i]);
}
printf("Nhap c: "); scanf("%f",&c);
}
void Hoocnertongquat(float A[],float c,int n)
{
int i,k;
float B[100];
for(i=0;i<=n;i++)
B[i]=A[i]
for(k=n;k>=1;k )
for(i=1;i<=k;i++) B[i]=B[i-1]*c+B[i];
printf("He so p(y+%.2f) la: ",c);
for(i=0;i<=n;i++) printf("%.2f ",B[i]);
}
main()
{
nhaphamso();
Hoocnertongquat(A,c,n);
getch();
}
KẾT QUẢ:
1.4 Viết chương trình tìm nghiệm gần đúng cho phương trình có dạng tổng
quát:
f(x) = a
0
x
n
+ a
1
x
n-1
+ … + a
n-1
x + a
n
= 0 bằng phương pháp chia đôi
CHƯƠNG TRÌNH:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define MAX 20
#define eps 1e-3
/**************************************************/
float chiadoi(float A[],int n,float a,float b);
float f(float A[],int n,float x);
float f(float A[],int n,float x)
{
int i;
float p=A[0];
for(i=1;i<=n;i++)
p=p*x+A[i];
return p;
}
float chiadoi(float A[],int n,float a,float b)
{
float c;
do
{
c=(a+b)/2;
if(f(A,n,a)*f(A,n,b)<0)
b=c;
else a=c;
}
while(fabs(a-b)>eps);
return c;
}
/**************************************************/
main()
{
float a,b,A[MAX];
int i,n;
printf("Chuongtrinh tim nghiem cua pt a(0)*x^n+a(1)*x^(n-1)+ +a(n-
1)*x+a(n)=0\n");
printf("Nhap vao bac pt : ");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
printf("\nHeso A[%d]=",i);
scanf("%f",&A[i]);
}
do
{
printf("Nhap vao khoang nghiem [a,b] voi f(a)*f(b)<0: ");
scanf("%f%f",&a,&b);
}
while(f(A,n,a)*f(A,n,b)>0);
printf("\n Phuong phap chia doi : nghiem= %7.3f",chiadoi(A,n,a,b));
getch();
}
KẾT QUẢ:
BÀI SỐ 2:
2.1 Viết chương trình tìm nghiệm gần đúng cho phương trình có dạng tổng
quát:
f(x) = a
0
x
n
+ a
1
x
n-1
+ … + a
n-1
x + a
n
= 0 bằng phương pháp dây cung
CHƯƠNG TRÌNH:
#include<conio.h>
#include<math.h>
#include<stdio.h>
#define eps 1e-3
double f(float A[],int n,float x)
{
float p = A[0];
for(int i=1;i<=n;i++)
p=p*x+A[i];
return p;
}
float daycung(float A[],float a,float b,int n)
{
double x=a-(((b-a)*f(A,n,a))/(f(A,n,b)-f(A,n,a)));
if(f(A,n,x)*f(A,n,a)<0)
do{
b=x;
x=a-(((b-a)*f(A,n,a))/(f(A,n,b)-f(A,n,a)));
} while(fabs(x-b)> eps);
else
do{
a=x;
x=a-(((b-a)*f(A,n,a))/(f(A,n,b)-f(A,n,a)));
}while(fabs(x-a)>eps);
return x;
}
main()
{
float a,b;
int n;
float x;
float A[10];
int c;
printf("\nnhap vao bac n cua phuong trinh:");
scanf("%d",&n);
printf("\nnhap vao he so cua da thuc:\n");
for(int i=0;i<=n;i++)
{
printf("A[%d]= ",i);
scanf("%f",& A[i]);
}
do{
printf("nhap vao a ,b thoa man (f(a)*f(b)<0)\n");
scanf("%f%f",&a,&b);
}while(f(A,n,a)*f(A,n,b)>0);
printf("\nnghiem cua phuong trinh la:%3f",daycung(A,a,b,n));
getch();
}
KẾT QUẢ:
2.2 Viết chương trình tìm nghiệm cho phương trình e
x
- 10x + 7 = 0 (có thể
thay phương trình khác) bằng phương pháp tiếp tuyến.
CHƯƠNG TRÌNH:
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define esp 1e-3
float f(float x)
{
return(exp(x)-10*x+7);
}
float g(float x)
{
return(exp(x)-10);
}
main()
{
float x,y;
printf("nhap vao gia tri cua x: ");
scanf("%f",&x);
do{
y=x;
x=y-(f(y)/g(y));
}while(fabs(x-y)>esp);
printf("nghiem cua phuong trinh la:%3f",x);
getch();
}
KẾT QUẢ:
2.3 Viết chương trình giải hệ đại số tuyến tính bằng phương pháp Gauss
CHƯƠNG TRÌNH:
#include<stdio.h>
#include<conio.h>
float A[100][100],X[100];
int n;
void nhaptufile()
{
FILE *f;
int i,j;
f=fopen("Gauss.txt","r");
if(f==NULL)
printf("\nKhong the mo file!");
else
{
fscanf(f,"%d\n",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n+1;j++) fscanf(f,"%f",&A[i][j]);
}
fclose(f);
}
void xuatmatran()
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
printf("%5.2f ",A[i][j]);
printf("\n");
}
}
void swap(float &a,float &b)
{
float f;
f=a;
a=b;
b=f;
}
void doidong(int p,int q)
{
int k;
if(p<=n&&q<=n&&p!=q)
for (k=1;k<=n+1;k++)
swap(A[p][k],A[q][k]);
}
int Gauss(float A[100][100],int n)
{
int i,j,k;
float m,s;
for (i=1;i<=n;i++)
{
if (A[i][i]==0)
{
for (k=i+1;k<=n;k++)
if(A[k][i]!=0) break;
doidong(i,k);
if (k>n) return 1;
}
for (j=i+1;j<=n;j++)
{
m=-A[j][i]/A[i][i];
for(k=i;k<=n+1;k++) A[j][k]+=A[i][k]*m;
}
}
printf("\nHe phuong sau khi bien doi :\n");
xuatmatran();
// Tim nghien theo qua trinh nguoc
for (i=n;i>=1;i )
{
s=A[i][n+1];
for(k=i+1;k<=n;k++) s-=A[i][k]*X[k];
if(A[i][i]!=0) X[i]=s/A[i][i];
}
}
main()
{
nhaptufile();
xuatmatran();
if(Gauss(A,n)==1) printf("Du lieu khong hop le");
else
{
printf("\nNghiem he phuong trinh : ");
for(int i=1;i<=n;i++) printf("%5.2f ",X[i]);
}
getch();
}
FILE TEXT:
KẾT QUẢ:
2.3 Viết chương trình giải hệ đại số tuyến tính bằng phương pháp lặp Gauss
Siedel
CHƯƠNG TRÌNH:
#include<stdio.h>
#include<conio.h>
#include<math.h>
float A[100][100],X[100],Y[100];
int n;
void nhaptufile()
{
FILE *f;
int i,j;
f=fopen("Gauss_Siedel.txt","r");
if(f==NULL)
printf("Khong the mo file!");
else
{
fscanf(f,"%d\n",&n);
for(i=1;i<=n;i++)
for(j=1;j<=n+1;j++) fscanf(f,"%f",&A[i][j]);
}
fclose(f);
}
void xuatmatran()
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++) printf("%5.3f ",A[i][j]);
printf("\n");
}
}
void xuat()
{
for(int i=1;i<=n;i++)
printf ("%5.3f ",X[i]);
}
void Gauss_Siedel(float A[][100],int n)
{
int i,j,dem,lap;
printf("\nNhap nghiem xap xi ban dau : ");
for(int i=1;i<=n;i++) scanf("%f",&X[i]);
dem=0;
do
{
lap=0; dem++;
for(i=1;i<=n;i++)
{
float s=0;
for (j=1;j<=n;j++)
if(j!=i) s+=A[i][j]*X[j];
Y[i]=A[i][n+1]-s;
if(A[i][i]!=0) Y[i]=Y[i]/A[i][i];
//else return 1;
if(fabs(X[i]-Y[i])>10e-5&&dem<30) lap=1;
}
for(i=1;i<=n;i++) X[i]=Y[i];
}while(lap);
if(dem<30) {
printf("\nNghiem cua he phuong trinh : ");
xuat();
}
else printf("\nHe phuong trinh ko giai duoc bang phuong phap tren ");
}
main()
{
nhaptufile();
xuatmatran();
Gauss_Siedel(A,n);
getch();
}
FILE TEXT:
KẾT QUẢ:
Bài số 3.
• Cho hàm f(x) thoả mãn bảng giá trị:
x x
0
x
1
x
2
… …
x
n
f(x) y
0
y
1
y
2
… …
y
n
Viết chương trình tính giá trị hàm f(x) tại điểm x=t cho trước (t ϵ [x
0
, x
n
],
dùng bảng nội suy Ayken dạng 1)
CHƯƠNG TRÌNH:
#include <math.h>
#include <stdio.h>
#include <conio.h>
#define max 100
float x[max],y[max];
int n;
void nhaptufile()
{
FILE *f;
int i;
f=fopen("ayken.txt","r");
if(f==NULL)
printf("khong the mo file!");
else
fscanf(f,"%d",&n);
for(i=0;i<=n;i++) fscanf(f,"%f",&x[i]);
for(i=0;i<=n;i++) fscanf(f,"%f",&y[i]);
printf("Bang gia tri tuong ung\nX |");
for(i=0;i<=n;i++) printf("%5.3f ",x[i]);
printf("\nY |");
for(i=0;i<=n;i++) printf("%5.3f ",y[i]);
fclose(f);
}
void Ayken()
{
int i,j;
float w=1,s=0.0,c,d,temp;
printf("\nNhap gia tri can tinh : ");
scanf("%f",&c);
for(i=0;i<=n;i++)
{
w*=(c-x[i]);
d=c-x[i];
for (j=0;j<=n;j++)
if(j!=i) d*=x[i]-x[j];
s+=y[i]/d;
}
printf("\nGia tri ham f(%.3f) = %.3f ",c,w*s);
}
main()
{
nhaptufile();
Ayken();
getch();
}
FILE: ayken.txt
KẾT QUẢ:
• Viết chương trình tính gần đúng tích gần đúng phân xác định của hàm
f(x) trên đoạn [a, b] trong 3 trường hợp:
+ Công thức hình thang.
+ Công thức parabol.
+ Công thức Newton-cotet.
CHƯƠNG TRÌNH:
#include <math.h>
#include <stdio.h>
#include <conio.h>
#define PI 3.14159
float A[100],p[100];
int n,k;
void nhaptufile()
{
int i;
FILE *f;
f=fopen("input.txt","r");
fscanf(f,"%d",&n);
for(i=0;i<=n;i++) fscanf(f,"%f",&A[i]);
fclose(f);
}
float f(float x)
{
float p=A[0];
int i;
for(i=1;i<=n;i++) p=p*x+ A[i];
return p;
}
float hinhthang(float a,float b)
{
int n=100,i;
float s,h=(b-a)/n;
s=(f(a)+f(b))/2;
for(i=1;i<n;i++) s+=(f(a+i*h));
return s*h;
}
float Parabol(float a,float b)
{
int n=100,i;
float s,h=(b-a)/(2*n);
s=(f(a)+f(b));
for(i=1;i<2*n;i++)
if(i%2==0) s+=4*f(a+i*h);
else s+=2*f(a+i*h);
return s*h/3;
}
float Newton(float a,float b)
{
FILE *ft;
int i,j;
float temp,Y[100],h,J=0;
printf("\nChia thanh bao nhieu doan :");
scanf("%d",&k);
ft=fopen("heso.txt","r");
for(i=1;i<k;i++) for(j=0;j<=i;j++) fscanf(ft,"%f",&temp);//bo qua
nhung phan ko doc
for(i=0;i<=k;i++) fscanf(ft,"%f",&p[i]);
printf("He so Newton-Cotet:");
for(i=0;i<=k;i++) printf(" %0.3f",p[i]);
h=(b-a)/k;
for(i=0;i<=k;i++) Y[i]=f(a+i*h);
for(i=0;i<=k;i++) J+=Y[i]*p[i];
return (b-a)*J;
}
main()
{
float a,b;
//nhapdathuc();
nhaptufile();
printf("Nhap can de tinh tich phan ");
printf("\na= ");scanf("%f",&a);
printf("b= ");scanf("%f",&b);
printf("Ket qua tich phan theo phuong phap hinh thang = %.2f
",hinhthang(a,b));
printf("\nKet qua tich phan theo phuong phap Parabol = %.2f
",Parabol(a,b));
printf("\nKet qua tich phan theo phuong phap Newton = %.2f
",Newton(a,b));
getch();
}
FILE
INPUT.TXT:
HESO.TXT:
KẾT QUẢ:
Viết chương trình (có sử dụng hàm) tính gần đúng tích phân xác định trên
đoạn [a, b] của một số hàm cụ thể (trong đó có hàm đa thức bậc n) theo 3
cách (tương ứng với 3 công thức trên). So sánh kết quả, nhận xét.
CHƯƠNG TRÌNH:
#include <math.h>
#include <stdio.h>
#include <conio.h>
#define PI 3.14159
float A[100],p[100];
int n,k;
void nhapdathuc()
{
int i;
printf("Nhap bac da thuc n= ");
scanf("%d",&n);
for (i=0;i<=n;i++)
{
printf("A[%d] = ",i);
scanf("%f",&A[i]);
}
}
float f(float x)
{
float p=A[0];
int i;
for(i=1;i<=n;i++) p=p*x+ A[i];
return p;
}
float hinhthang(float a,float b)
{
int n=100,i;
float s,h=(b-a)/n;
s=(f(a)+f(b))/2;
for(i=1;i<n;i++) s+=(f(a+i*h));
return s*h;
}
float Parabol(float a,float b)
{
int n=100,i;
float s,h=(b-a)/(2*n);
s=(f(a)+f(b));
for(i=1;i<2*n;i++)
if(i%2==0) s+=4*f(a+i*h);
else s+=2*f(a+i*h);
return s*h/3;
}
float Newton(float a,float b)
{
FILE *ft;
int i,j;
float temp,Y[100],h,J=0;
printf("\nChia thanh bao nhieu doan :");
scanf("%d",&k);
ft=fopen("heso.txt","r");
for(i=1;i<k;i++) for(j=0;j<=i;j++) fscanf(ft,"%f",&temp);
for(i=0;i<=k;i++) fscanf(ft,"%f",&p[i]);
printf("He so Newton-Cotet:");
for(i=0;i<=k;i++) printf(" %0.3f",p[i]);
h=(b-a)/k;
for(i=0;i<=k;i++) Y[i]=f(a+i*h);
for(i=0;i<=k;i++) J+=Y[i]*p[i];
return (b-a)*J;
}
main()
{
float a,b;
nhapdathuc();
printf("Nhap can de tinh tich phan ");
printf("\na= ");scanf("%f",&a);