0% found this document useful (0 votes)
83 views15 pages

Chia Doi:: CODE Phuong Phap Tinh Ete - Dut

1) The document contains code written in C programming language for various numerical methods such as polynomial interpolation, root finding, matrix operations, and Lagrange interpolation. 2) Sections of code include functions for inputting arrays, calculating polynomials, finding roots of polynomials using bisection method, matrix multiplication, and calculating values using Lagrange interpolation. 3) Examples show how to input data, call functions to perform calculations, and output results. The code serves as examples for implementing numerical methods in C.

Uploaded by

Rua Tuệ Đoàn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
83 views15 pages

Chia Doi:: CODE Phuong Phap Tinh Ete - Dut

1) The document contains code written in C programming language for various numerical methods such as polynomial interpolation, root finding, matrix operations, and Lagrange interpolation. 2) Sections of code include functions for inputting arrays, calculating polynomials, finding roots of polynomials using bisection method, matrix multiplication, and calculating values using Lagrange interpolation. 3) Examples show how to input data, call functions to perform calculations, and output results. The code serves as examples for implementing numerical methods in C.

Uploaded by

Rua Tuệ Đoàn
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

CODE Phuong Phap Tinh ETE_DUT

Chia doi:
#include<conio.h>
#include<stdio.h>
#include<math.h>
void nhap( float d[20],int n){
int i;
for (i=0;i<=n;i++)
{printf("d[%d]=",i);scanf("%f",&d[i]);
}
}
float f(float d[],int n, float c ){
int i; float p=d[0];
for(i=1;i<=n;i++)
p=p*c+d[i];
return p;

}
main(){
float x,d[10],a,b,c,e; //char q;
int i,n;
printf("nhap bac da thuc n\n");scanf("%d",&n);
printf("nhap cac he so\n");
nhap(d,n);
while(1){
printf("\n nhap khoang nghiem a va b\n"); scanf("%f%f",&a,&b);
if (f(d,n,a)*f(d,n,b)>0) continue;

1
Le Huu Phu BR
CODE Phuong Phap Tinh ETE_DUT

do
{

c=(a+b)/2; e=f(d,n,c);
printf("\n%9.3f%9.3f%9.3f\n",a,b,e);
if(e*f(d,n,a)>0)
a=c;
else
b=c;
}
while(fabs(e)>=0.001);
printf("\n nghiem cua chuong trinh la:%.3f",c);
}
}
Day cung:
#include<conio.h>
#include<stdio.h>
#include<math.h>
void nhap( float d[],int n){
int i;
for (i=0;i<=n;i++)
{printf("d[%d]=",i);scanf("%f",&d[i]);
}
}
float f(float d[],int n, float c ){
int i; float p=d[0];
for(i=1;i<=n;i++)

2
Le Huu Phu BR
CODE Phuong Phap Tinh ETE_DUT

p=p*c+d[i];
return p;

}
main(){
float x,d[10],a,b,t1,t2; char q;
int i,n;
printf("nhap bac da thuc n\n");scanf("%d",&n);
printf("nhap cac he so\n");
nhap(d,n);
while(1){
printf("\n nhap khoang nghiem a va b\n"); scanf("%f%f",&a,&b);
if (f(d,n,a)*f(d,n,b)>0){
printf("\n a b khong phaikhoang ngiem\n");
continue;}
do{
printf("\n%9.3f%9.3f%9.3f%9.3f",a,b,x,f(d,n,x));
x=a-((b-a)*f(d,n,a))/(f(d,n,b)-f(d,n,a));
if(f(d,n,x)*f(d,n,a)>0) a=x;
else b=x;
}
while(fabs(f(d,n,x))>=0.001);
printf("\nngiem cua phuong trinh la %.3f",x);
printf("\ntiep tuc k?(c/k)");
q=getch();
if(q=='k') break;
}}

3
Le Huu Phu BR
CODE Phuong Phap Tinh ETE_DUT

Đổi BIẾn:
#include<conio.h>
#include<stdio.h>
#include<math.h>
void nhap( float d[20],int n)
{
int i;
for (i=0;i<=n;i++)
{
printf("d[%d]=",i);
scanf("%f",&d[i]);
}
}
main(){
float x,d[10],a,b,k,c,e; //char q;
int i,n;
printf("nhap bac da thuc n\n");scanf("%d",&n);
printf("nhap cac he so\n");
nhap(d,n);
printf("\nnhap c\n"); scanf("%f",&c);

for(k=n;k>0;k--)
for(i=0;i<=k;i++) d[i]=d[i-1]*c+d[i];
for(i=0;i<=n;i++)
printf("\n a[%d]=%f \n",i,d[i]); }
SinCos:
#include<stdio.h>

4
Le Huu Phu BR
CODE Phuong Phap Tinh ETE_DUT

#include<math.h>
#include<conio.h>
float cos(float x)
{
float q=0; float t=1; int k=0;
while(fabs(t)>0.000001)
{
q=q+t;
k=k+2;
t=-t*x*x/(k*(k-1));
}
return (q);
}
float exp(float x)
{float q=0,k=0,t=1;
while(fabs(t)>0.000001)
{q=q+t;
k++;
t=t*x/k;
}
return q;

}
float sin(float x)
{
float q=0; float t=x; int k=1;
while(fabs(t)>0.000001)

5
Le Huu Phu BR
CODE Phuong Phap Tinh ETE_DUT

{
q=q+t;
k=k+2;
t=-t*x*x/(k*(k-1));
}
return (q);
}
main ()

{ while (1)
{
float x; char c;
printf("nhap so do goc x= ");
scanf("%f",&x);
x=x*3.14/180;
printf("\ncos x = %f\n",cos(x));
printf("\nsin x = %f\n",sin(x));
printf("\n exp(x)=%f",exp(x));
c=getch(); if(c=='q') break;
}
}
TRỊ RIÊNG VECTO RIÊNG:
#include<stdio.h>
void nhap(float a[10][10],int n)
{ int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)

6
Le Huu Phu BR
CODE Phuong Phap Tinh ETE_DUT

{
printf("\na[%d][%d]=",i,j);
scanf("%f",&a[i][j]);
}
}
void xuat(float a[10][10],int n)
{ int i,j;
for(i=1;i<=n;i++)
{
printf("\n");
for(j=1;j<=n;j++)
printf("%9.3f",a[i][j]);
}
}
void tich(float a[10][10],float b[10][10],float c[10][10],int n)
{
int i,j,k;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
c[i][j]=0;
for(k=1;k<=n;k++)
c[i][j]+=a[i][k]*b[k][j];
}

}
main()

7
Le Huu Phu BR
CODE Phuong Phap Tinh ETE_DUT

{
int n,k,i,j;
float a[10][10], M[10][10], M1[10][10],b[10][10],c[10][10];
printf("\nnhap cap cua ma tran "); scanf("%d",&n);
printf("\nnhap ma tran a\n");
nhap(a,n);
for(i=1;i<=n;i++)//khoi tao c=E
for(j=1;j<=n;j++)
{
if(i==j) c[i][j]=1;
else c[i][j]=0;
}
for(k=n-1;k>=1;k--){
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(i!=k)
if(i==j)
{
M1[i][j]=1;
M[i][j]=1;
}
else
{
M1[i][j]=0;
M[i][j]=0;
}

8
Le Huu Phu BR
CODE Phuong Phap Tinh ETE_DUT

else
{
M1[i][j]=a[k+1][j];
if(j==k)
M[i][j]=1/a[k+1][k];
else
M[i][j]=-a[k+1][j]/a[k+1][k];
}
}
printf("\nlan chon thu %d",n-k);
printf("\nma tran M1= "); xuat(M1,n);
printf("\nma tran M= "); xuat(M,n);
tich(M1,a,b,n);
tich(b,M,a,n);
tich(c,M,b,n);
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
c[i][j]=b[i][j];
printf("\nma tran A(%d)=",n-k);
xuat(a,n);
printf("\nma tran B=\n");
xuat(b,n);
}
}
AYKEN 1:
#include<stdio.h>
void nhap(float x[10],float y[10],int n)

9
Le Huu Phu BR
CODE Phuong Phap Tinh ETE_DUT

{
int i;
for(i=0;i<n;i++)
{
printf("\n x[%d]=",i); scanf("%f",&x[i]);
printf("\n y[%d]=",i); scanf("%f",&y[i]);
}
}
float tinh(float x[10], float y[10], int n, float c)
{ int i,j;
float w=1,s=0,d;
for(i=0;i<n;i++)
{
w=w*(c-x[i]);
d=c-x[i];
for(j=0;j<n;j++)
if(j!=i)d=d*(x[i]-x[j]);
s+=y[i]/d;
}
return w*s;
}
main()
{
float x[10],y[10];
int n;
float c;
printf("nhap so moc noi suy n=\n");

10
Le Huu Phu BR
CODE Phuong Phap Tinh ETE_DUT

scanf("%d",&n);
nhap(x,y,n);
printf("\n nhap gia tri can tinh");
scanf("%f",&c);
printf("\n gia tri can tinh la: %f",tinh(x,y,n,c));
}
AYKEN 2(in bang)
#include<stdio.h>
#include<conio.h>
void nhap(float x[10],float y[10][10], int n)
{
int i;
for(i=0;i<n;i++)
{
printf("x[%d]=\n",i); scanf("%f",&x[i]);
printf("y[%d]=\n",i); scanf("%f",&y[i][0]);
}
}
void tinh(float x[10],float y[10][10], int n, float c)
{
int i,j;
for(i=0;i<n;i++)
y[i][n]=x[i]-c;
for(i=1;i<n;i++)
for(j=1;j<=i;j++)
y[i][j]=(y[j-1][j-1]*y[i][n]-y[i][j-1]*y[j-1][n])/(x[i]-x[j-1]);
}

11
Le Huu Phu BR
CODE Phuong Phap Tinh ETE_DUT

void xuat(float x[10], float y[10][10], int n)


{
int i,j;
printf("\nBANG NOI SUY AYKEN 2\n");
for(i=0;i<n;i++)
{
printf("\n %8.3f",x[i]);
for(j=0;j<=i;j++)
printf("%8.3f",y[i][j]);
}
}
main()
{
float c,x[10],y[10][10];
int n;
printf("\n nhap so moc noi suy n=");
scanf("%d",&n);
nhap(x,y,n);
printf("\nnhap gia tri can tinh c=");
scanf("%f",&c);
tinh(x,y,n,c);
xuat(x,y,n);
}
NỘI SUY NEWTON: in bang sai phân:
#include<stdio.h>
#include<conio.h>
void nhap(float x[10],float y[10][10], int n)

12
Le Huu Phu BR
CODE Phuong Phap Tinh ETE_DUT

{
int i;
for(i=0;i<n;i++)
{
printf("x[%d]=\n",i); scanf("%f",&x[i]);
printf("y[%d]=\n",i); scanf("%f",&y[i][0]);
}
}
void tinh(float y[10][10], int n)
{
int i,j;
for(i=1;i<n;i++)
for(j=1;j<=i;j++)
y[i][j]=y[i][j-1]-y[i-1][j-1];
}
void xuat(float x[10], float y[10][10], int n)
{
int i,j;
printf("\nBANG SAI PHAN\n");
for(i=0;i<n;i++)
{
printf("\n %8.3f",x[i]);
for(j=0;j<=i;j++)
printf("%8.3f",y[i][j]);
}
}
main()

13
Le Huu Phu BR
CODE Phuong Phap Tinh ETE_DUT

{
float x[10],y[10][10];
int n;
printf("nhap so moc noi suy n=\n");
scanf("%d",&n);
nhap(x,y,n);
tinh(y,n);
xuat(x,y,n);
}
CT HÌNH THANG
#include <stdio.h>
#include <math.h>
float f(float x)
{
return x/(1+x*x*x);
}
int main ()
{
int i,n;
float a,b,x,h,j=0;
printf("Nhap vao lan luot la a,b,n nhe : ");
scanf("%f%f%n",&a,&b,&n);
h = (b-a)/n;
j = (f(a) + f(b))/2;
for(i = 1; i<= n-1; i++)
j += f(a + i*h);
printf("\nGia tri tich phan la : %8.3f",h*j);
return(0);
}

14
Le Huu Phu BR
CODE Phuong Phap Tinh ETE_DUT

CT PARABOL
#include <stdio.h>
#include <math.h>
float f(float x)
{
return 1/(1+x*x);
}
int main ()
{
int i,n;
float a,b,x,h,j=0;
printf("Nhap vao lan luot la a,b,n nhe : ");
scanf("%f%f%n",&a,&b,&n);
h = (b-a)/(2*n);
j = f(a) + f(b);
for(i = 1; i<= 2*n-1; i++)
if(i%2)
j+=4*f(a+i*h);
else
j+=2*f(a+i*h);
printf("\nGia tri tich phan la : %8.3f",h*j/3);
return(0); }

15
Le Huu Phu BR

You might also like