Em đang làm bài tập phương pháp tính tìm nghiệm gần đúng của phương trình bậc 3 dùng phương pháp dây cung,chia đôi và newton,không hỉu sao chạy chương trình con riêng biet dể test thì đúng mà ráp vào lại sai???????
Phương pháp chia đôi thì đúng còn 2 phương pháp còn lại ekho6nf bit sai chỗ nào nữa,các pác xem dùm em
#include "stdio.h"
#include "conio.h"
#include "math.h"
#include "stdlib.h"
#include "dos.h"
int a,b,c,d;int l[3];int dem,i;
float E;
void tieude()
{
clrscr();
printf("\t/* ============== BAI TAP PHUONG PHAP TINH ============= */\n");
printf("\t/* CHUONG TRINH TIM NGHIEM GAN DUNG CUA */\n");
printf("\t/* DA THUC BAC BA */\n");
printf("\t/* (lam tron 4 chu so va epsilon=0.001) */\n");
printf("\t/* ================================================== ===== */\n");
}
void nhap()
{
p: printf("\n\tMoi nhap cac he so chuong trinh bac 3");
printf("\n\t\tA=");scanf("%d",&a);
if (a==0)
{
printf("\n\tPhuong trinh bac 3 he so A phai khac 0.Vui long nhap lai A");
goto p;
}
printf("\n\t\tB=");scanf("%d",&b);
printf("\n\t\tC=");scanf("%d",&c);
printf("\n\t\tD=");scanf("%d",&d);
printf("\n\tPhuong trinh bac 3 ban vua nhap la:");
printf("%dx^3",a);
if (b<0)
printf(" -%dx^2",abs(b));
else if (b>0)
printf(" +%dx^2",b);
if (c<0)
printf(" -%dx",abs(c));
else if (c>0)
printf(" +%dx",c);
if (d<0)
printf(" -%d",abs(d));
else if (d>0)
printf("+%d\n",d);
}
int giaodien()
{
printf("\n\tMoi chon phuong phap tim ngiem:\n");
printf("\t1.Phuong phap chia doi\n");
printf("\t2.Phuong phap tiep tuyen\n");
printf("\t3.Phuong phap day cung\n");
printf("\t4.Thoat");
b: printf("\nChon phuong phap thu : ");scanf("%d",&a);
if ((a==1)||(a==2)||(a==3)||(a==4))
{
return(a);
}
else
{
printf("Ban phai chon tu 1 den 4.Vui long chon lai\n");
goto b;
}
}
double f(double x)
{
return (a*pow(x,3)+b*pow(x,2)+c*x+d);
}
double ff(double x)
{
return 3*a*pow(x,2)+2*b*x+c;
}
double fff(double x)
{
return 6*a*x;
}
int kpl()
{
int i;
dem=0; //dem xem co bao nhieu khoang phan li
for(i=-100;i<=100;i++)
if(f(i)*f(i+1)<0)
{
l[dem]=i;
dem++;
if (dem==3) return dem;
}
return dem;
}
double lamtron(double x)
{
return floor((x*10000)+0.5)/10000;
}
void chiadoi(int x,int y)
{

double An,Bn,X;
An=x,Bn=y;
int i;
printf("\t________________________________________ ________\n");
printf("\t| An | Bn | X | E |\n");
printf("\t________________________________________ ________\n");
do
{
X=lamtron((An+Bn)/2);

printf("\t| %7.4f | %7.4f | %7.4f | %7.4f |\n",An,Bn,X,fabs(lamtron(An-Bn)));
printf("\t________________________________________ ________\n");
if (f(An)*f(X)<0)
Bn=X;
else An=X;
E=(lamtron(fabs(An-Bn)));
}
while (E>0.001);

printf("\t| %7.4f | %7.4f | %7.4f | %7.4f |\n",An,Bn,lamtron((An+Bn)/2),fabs(lamtron(An-Bn)));
printf("\t________________________________________ ________\n");
printf("\nVay nghiem gan dung la : %.4f",lamtron((An+Bn)/2));
}

void tieptuyen(int x,int y)
{
double Xn,Xm,A,B;
A=x;B=y;
if (f(A)*fff(A) >= 0)
Xn=A;
else
Xn=B;
printf("\t________________________________________ \n");
printf("\t| Xn | Xm | |Xn-Xm| |\n");
printf("\t________________________________________ \n");
do
{
Xm=lamtron(Xn-(f(Xn)/ff(Xn)));
E=fabs(Xm-Xn);
printf("\t| %7.4f | %7.4f | %7.4f |\n",Xn,Xm,E);
printf("\t________________________________________ \n");
Xn=Xm;
}
while (E>0.001);
printf("Vay nghiem gan dung la : %.4f",Xm);
}
void daycung(int x,int y)
{
double A,B,Xn,Xm;int d;
A=x;
B=y;
if ((f(A)*fff(A))>0)
{
Xn=B;d=A;
}
else
{
Xn=A;d=B;
}
printf("\t________________________________________ \n");
printf("\t| Xn | Xm | |Xn-Xm| |\n");
printf("\t________________________________________ \n");
do
{
Xm=lamtron(Xn-((f(Xn)*(Xn-d))/(f(Xn)-f(d))));
E=fabs(Xm-Xn);
printf("\t| %7.4f | %7.4f | %7.4f |\n",Xn,Xm,E);
printf("\t__________________________________\n");
Xn=Xm;
}
while (E>0.001);
printf("Vay nghiem gan dung la : %7.4f",Xm);
}
void tinh()
{
clrscr();tieude();
int phuongphap=giaodien();
a: switch (phuongphap)
{
case 1:
{

for(i=0;i<dem;i++)
{
tieude();
printf("\n /* ====================== PHUONG PHAP CHIA DOI ==================== */\n");
printf("\n\tVoi khoang phan li (%d,%d) ta co bang :\n",l[i],l[i]+1);
chiadoi(l[i],(l[i]+1));
printf("\n\t\t\tPress any key to continue");
getch();
}
};break;
case 2:for(i=0;i<dem;i++)
{
tieude();
printf("\n /* ====================== PHUONG PHAP TIEP TUYEN ==================== */\n");
printf("\n\tVoi khoang phan li (%d,%d) ta co bang :\n",l[i],l[i]+1);
tieptuyen(l[i],l[i]+1);
printf("\n\t\t\tPress any key to continue");
getch();
};break;
case 3: for(i=0;i<dem;i++)
{
tieude();
printf("\n /* ====================== PHUONG PHAP DAY CUNG ==================== */\n");
printf("\\tnVoi khoang phan li (%d,%d) ta co bang :\n",l[i],l[i]+1);
daycung(l[i],(l[i]+1));getch();
};break;
case 4:
return;
}
printf("\nBan co muon chon lai phuong phap khac khong?(Co:y,Khong:any key):");
if (getche()=='y')
tinh();
else
return;
}
void main()
{
int i,n;
a: tieude();
nhap(); //Nhap cac he so cua phuong trinh bac 3
n=kpl();
if (n!=0) //neu co khoang phan li
{
printf("\n\tPhuong trinh co khoang phan li la ");
for(i=0;i<n;i++)
{
printf("(%d,%d)\t",l[i],l[i]+1);
}
printf("\n\tPress any key to continue");
getch();
tinh();
}
else
{
printf("\nPhuong trinh khong co khoang phan li");
printf("\nBan co muon nhap phuong trinh khac khong(Co:y)? ");
if (getche()=='y')
goto a;
}
printf("\nBan co muon nhap tiep phuong trinh khac khong?(Co:y,Khong:any key) ");
if(getche()=='y')
goto a;
else
{
printf("\nChuong trinh se thoat trong 2 giay nua");
delay(2000);
exit(0);
}

}