#include <stdio.h> #include <stdlib.h> #include <math.h> #define MAX 200 #define ED 0.5e-12 #define ENR 0.5e-16
//------------------------------------------------------------------------------------------- //------------------------------------------------------------------------------------------- float bisection(float (*f)(float),float *a,float *b); float stshmeiou(float (*f)(float),float (*g)(float),float *x); float newton(float (*f)(float),float (*parf)(float),float *x); float temnousas(float(*f)(float),float *x,float *y); int ebisection(float (*f)(float),float *a,float *b); int estshmeiou(float (*f)(float),float (*g)(float),float *x); int enewton(float (*f)(float),float (*parf)(float),float *x); int etemnousas(float(*f)(float),float *x,float *y); float nbisection(float (*f)(float),float *a,float *b); float nnewton(float (*f)(float),float (*parf)(float),float *x); float f1(float x); float f2(float x); float f3(float x); float g1(float x); float g2(float x); float g3(float x); float parf1(float x); float parf2(float x); float parf3(float x); //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- int main(void) { int epilogi; float a1,b1,a2,b2,a3,b3,q1,q2,q3,s1,s2,s3,x1,x2,x3,n1,n2,n3,t1,t2,t3,y1,y2,y3; float x01,x02,x03,x11,x12,x13,y01,y02,y03,y11,y12,y13,z01,z02,z03,z11,z12,z13,w1,w2,w3; char xar; xar='n'; while(xar=='n') { printf("-----------------------------------ASKHSH 1----------------------------------"); printf("-----------------------------------------------------------------------------------"); printf("\nPROSEGGISH TIMHS X[N] THS RIZAS J"); printf("\nPATHSTE:"); printf("\n1.GIA F1"); printf("\n2.GIA F2"); printf("\n3.GIA F3"); printf("\n4.GIA TAXYTHTA SYGKLISHS"); printf("\n5.EMFANISH APOTELESMATON"); printf("\n6.METHODOS DIXOTOMISIS-NR\n"); scanf("\n%d",&epilogi); switch(epilogi) { case 1: a1=-1; b1=2; x1=-1; y1=2; q1=bisection(f1,&a1,&b1); printf("H prosegisi tis rizas me thn methodo ths dixotomisis\ngia tin f1(x) einai q1=%lf\n",q1); s1=stshmeiou(f1,g1,&x1); printf("\nH prosegisi tis rizas me thn methodo tou statherou shmeiou\ngia tin f1(x) einai s1=%lf\n",s1); n1=newton(f1,parf1,&x1); printf("\nH prosegisi tis rizas me thn methodo NR\ngia tin f1(x) einai n1=%lf\n",n1); t1=temnousas(f1,&x1,&y1); printf("\nH prosegisi tis rizas me thn methodo ths temnousas\ngia tin f1(x) einai t1=%lf\n",t1); break; case 2: a2=0; b2=1; x2=0.5; y2=1; q2=bisection(f2,&a2,&b2); printf("H prosegisi tis rizas me thn methodo ths dixotomisis\ngia tin f2(x) einai q2=%lf\n",q2); s2=stshmeiou(f2,g2,&x2); printf("\nH prosegisi tis rizas me thn methodo tou statherou shmeiou\ngia tin f2(x) einai s2=%lf\n",s2); n2=newton(f2,parf2,&x2); printf("\nH prosegisi tis rizas me thn methodo NR\ngia tin f2(x) einai n2=%lf\n",n2); t2=temnousas(f2,&x2,&y2); printf("\nH prosegisi tis rizas me thn methodo ths temnousas\ngia tin f2(x) einai t2=%lf\n",t2); break; case 3: a3=4; b3=5; x3=4; y3=5; q3=bisection(f3,&a3,&b3); printf("\nH prosegisi tis rizas me thn methodo ths dixotomisis\ngia tin f3(x) einai q3=%lf\n",q3); s3=stshmeiou(f3,g3,&x3); printf("\nH prosegisi tis rizas me thn methodo tou statherou shmeiou\ngia tin f3(x) einai s3=%lf\n",s3); n3=newton(f3,parf3,&x3); printf("\nH prosegisi tis rizas me thn methodo NR\ngia tin f3(x) einai n3=%lf\n",n3); t3=temnousas(f3,&x3,&y3); printf("\nH prosegisi tis rizas me thn methodo ths temnousas\ngia tin f3(x) einai t3=%lf\n",t3); break; case 4: a1=-1; b1=2; x1=-1; y1=2; a2=0; b2=1; x2=0.5; y2=1; a3=4; b3=5; x3=4; y3=5; printf("\nO ARITHMOS EPANALIJEON TON METHODON EINAI:"); getchar(); printf("\nGIA F1:"); printf("\nDIXOTOMISI:%d",ebisection(f1,&a1,&b1)); printf("\nSTSHMEIOU:%d",estshmeiou(f1,g1,&x1)); printf("\nNR:%d",enewton(f1,parf1,&x1)); printf("\nTEMNOUSA:%d",etemnousas(f1,&x1,&y1)); getchar(); printf("\nGIA F2:"); printf("\nDIXOTOMISI:%d",ebisection(f2,&a2,&b2)); printf("\nSTSHMEIOU:%d",estshmeiou(f2,g2,&x2)); printf("\nNR:%d",enewton(f2,parf2,&x2)); printf("\nTEMNOUSA:%d",etemnousas(f2,&x2,&y2)); getchar(); printf("\nGIA F3:"); printf("\nDIXOTOMISI:%d",ebisection(f3,&a3,&b3)); printf("\nSTSHMEIOU:%d",estshmeiou(f3,g3,&x3)); printf("\nNR:%d",enewton(f3,parf3,&x3)); printf("\nTEMNOUSA:%d",etemnousas(f3,&x3,&y3)); break; case 5: a1=-1; b1=2; x01=-1; x02=1.5; x03=0.5; x11=0; x12=1; x13=2; a2=0; b2=1; y01=0.3; y02=0.5; y03=0.7; y11=0; y12=0.3; y13=1; a3=4; b3=5; z01=4; z02=4.3; z03=4.5; z11=4.3; z12=4.5; z13=4.8; printf("\n--------------------------------------------------------------------"); printf("\n--------------------------------------------------------------------"); printf("\n || PINAKAS APOTELESMATON ||"); printf("\n--------------------------------------------------------------------"); printf("\n || METHODOS ||"); printf("\n--------------------------------------------------------------------"); printf("\n || I || II || III || IV ||"); printf("\n--------------------------------------------------------------------"); printf("\n ||Xo|Xn|F(Xn)|n||Xo|Xn|F(Xn)|n||Xo|Xn|F(Xn)|n||Xo|X1|Xn|F(Xn)|n||"); printf("\n--------------------------------------------------------------------"); printf("\n ||-|%.2f|%2.f|%d||-1 |%.2f|%2.f|%d||-1 |%.2f|%2.f|%d||-1 |0|%.2f|%2.f|%d||",bisection(f1,&a1,&b1),f1(bisection(f1,&a1,&b1)),ebisection(f1,&a1,&b1),stshmeiou(f1,g1,&x01),f1(stshmeiou(f1,g1,&x01)),estshmeiou(f1,g1,&x01),newton(f1,parf1,&x01),f1(newton(f1,parf1,&x01)),enewton(f1,parf1,&x01),temnousas(f1,&x01,&x11),f1(temnousas(f1,&x01,&x11)),etemnousas(f1,&x01,&x11)); printf("\n F1||-|%.2f|%2.f|%d||1.5|%.2f|%2.f|%d||1.5|%.2f|%2.f|%d||1.5|1|%.2f|%2.f|%d||",bisection(f1,&a1,&b1),f1(bisection(f1,&a1,&b1)),ebisection(f1,&a1,&b1),stshmeiou(f1,g1,&x02),f1(stshmeiou(f1,g1,&x02)),estshmeiou(f1,g1,&x02),newton(f1,parf1,&x02),f1(newton(f1,parf1,&x02)),enewton(f1,parf1,&x02),temnousas(f1,&x02,&x12),f1(temnousas(f1,&x02,&x12)),etemnousas(f1,&x02,&x12)); printf("\n ||-|%.2f|%2.f|%d||0.5|%.2f|%2.f|%d||0.5|%.2f|%2.f|%d||0.5|2|%.2f|%2.f|%d||",bisection(f1,&a1,&b1),f1(bisection(f1,&a1,&b1)),ebisection(f1,&a1,&b1),stshmeiou(f1,g1,&x03),f1(stshmeiou(f1,g1,&x03)),estshmeiou(f1,g1,&x03),newton(f1,parf1,&x03),f1(newton(f1,parf1,&x03)),enewton(f1,parf1,&x03),temnousas(f1,&x03,&x13),f1(temnousas(f1,&x03,&x13)),etemnousas(f1,&x03,&x13)); printf("\n--------------------------------------------------------------------"); printf("\n ||-|%.2f|%2.f|%d||0.3|%.2f|%2.f|%d||0.3|%.2f|%2.f|%d||0.3|0|%.2f|%2.f|%d||",bisection(f2,&a2,&b2),f2(bisection(f2,&a2,&b2)),ebisection(f2,&a2,&b2),stshmeiou(f2,g2,&y01),f2(stshmeiou(f2,g2,&y01)),estshmeiou(f2,g2,&y01),newton(f2,parf2,&y01),f2(newton(f2,parf2,&y01)),enewton(f2,parf2,&y01),temnousas(f2,&y01,&y11),f2(temnousas(f2,&y01,&y11)),etemnousas(f2,&y01,&y11)); printf("\n F2||-|%.2f|%2.f|%d||0.5|%.2f|%2.f|%d||0.5|%.2f|%2.f|%d||0.5|0.3|%.2f|%2.f|%d||",bisection(f2,&a2,&b2),f2(bisection(f2,&a2,&b2)),ebisection(f2,&a2,&b2),stshmeiou(f2,g2,&y02),f2(stshmeiou(f2,g2,&y02)),estshmeiou(f2,g2,&y02),newton(f2,parf2,&y02),f2(newton(f2,parf2,&y02)),enewton(f2,parf2,&y02),temnousas(f2,&y02,&y12),f2(temnousas(f2,&y02,&y12)),etemnousas(f2,&y02,&y12)); printf("\n ||-|%.2f|%2.f|%d||0.7|%.2f|%2.f|%d||0.7|%.2f|%2.f|%d||0.7|1|%.2f|%2.f|%d||",bisection(f2,&a2,&b2),f2(bisection(f2,&a2,&b2)),ebisection(f2,&a2,&b2),stshmeiou(f2,g2,&y03),f2(stshmeiou(f2,g2,&y03)),estshmeiou(f2,g2,&y03),newton(f2,parf2,&y03),f2(newton(f2,parf2,&y03)),enewton(f2,parf2,&y03),temnousas(f2,&y03,&y13),f2(temnousas(f2,&y03,&y13)),etemnousas(f2,&y03,&y13)); printf("\n--------------------------------------------------------------------"); printf("\n ||-|%.2f|%2.f|%d||4 |%.2f|%2.f|%d||4 |%.2f|%2.f|%d||4|4.3|%.2f|%2.f|%d||",bisection(f3,&a3,&b3),f3(bisection(f3,&a3,&b3)),ebisection(f3,&a3,&b3),stshmeiou(f3,g3,&z01),f3(stshmeiou(f3,g3,&z01)),estshmeiou(f3,g3,&z01),newton(f3,parf3,&z01),f3(newton(f3,parf3,&z01)),enewton(f3,parf3,&z01),temnousas(f3,&z01,&z11),f3(temnousas(f3,&z01,&z11)),etemnousas(f3,&z01,&z11)); printf("\n F3||-|%.2f|%2.f|%d||4.3|%.2f|%2.f|%d||4.3|%.2f|%2.f|%d||4.3|4.5|%.2f|%2.f|%d||",bisection(f3,&a3,&b3),f3(bisection(f3,&a3,&b3)),ebisection(f3,&a3,&b3),stshmeiou(f3,g3,&z02),f3(stshmeiou(f3,g3,&z02)),estshmeiou(f3,g3,&z02),newton(f3,parf3,&z02),f3(newton(f3,parf3,&z02)),enewton(f3,parf3,&z02),temnousas(f3,&z02,&z12),f3(temnousas(f3,&z02,&z12)),etemnousas(f3,&z02,&z12)); printf("\n ||-|%.2f|%2.f|%d||4.5|%.2f|%2.f|%d||4.5 |%.2f|%2.f|%d||4.5|4.8|%.2f|%2.f|%d||",bisection(f3,&a3,&b3),f3(bisection(f3,&a3,&b3)),ebisection(f3,&a3,&b3),stshmeiou(f3,g3,&z03),f3(stshmeiou(f3,g3,&z03)),estshmeiou(f3,g3,&z03),newton(f3,parf3,&z03),f3(newton(f3,parf3,&z03)),enewton(f3,parf3,&z03),temnousas(f3,&z03,&z13),f3(temnousas(f3,&z03,&z13)),etemnousas(f3,&z03,&z13)); printf("\n--------------------------------------------------------------------"); printf("\n--------------------------------------------------------------------"); break; case 6: a1=-1; b1=2; a2=0; b2=1; a3=4; b3=5; w1=nbisection(f1,&a1,&b1); w2=nbisection(f2,&a2,&b2); w3=nbisection(f3,&a3,&b3); printf("\nF1:"); printf("\nH proseggistiki timh ths rizas j einai:"); printf("\n%lf",nnewton(f1,parf1,&w1)); printf("\n-----------------------------------------"); printf("\nF2:"); printf("\nH proseggistiki timh ths rizas j einai:"); printf("\n%lf",nnewton(f2,parf2,&w2)); printf("\n-----------------------------------------"); printf("\nF3:"); printf("\nH proseggistiki timh ths rizas j einai:"); printf("\n%lf",nnewton(f3,parf3,&w3)); printf("\n-----------------------------------------"); break; default: printf("O arithmos den brisketai metaji 1 kai 6"); break; } printf("\nGia synexeia patiste n(ai) diaforetika o(xi):"); scanf("\n%c",&xar); getchar(); } return 0; } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- float bisection(float (*f)(float),float *a,float *b) { int m; float x,g,h; float c,r; c=(*a+*b)/2; //printf("%lf",c); m=0; while(((*b-*a)>0) && ((*f)(c)!=0) && m<MAX) { //printf("%lf",c); c=(*a+*b)/2; g=(*f)(*a); r=(*f)(*b); h=(*f)(c); m++; if(h==0) { x=c; } else if(g*h<0 ) { *b=c; } else if(r*h<0) { *a=c; } else if(g*h>0 && r*h>0) { printf("h methodos tis dix den paizei"); return (*a+*b)/2; } } x=(*b+*a)/2; return (x); } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- float stshmeiou(float (*f)(float),float (*g)(float),float *x) { int m=0; float p,k; p=(*g)(*x); //printf("p=%lf",p); while(fabs((*f)(p))>0 && m<MAX) { //printf("p=%lf",p); m++; k=(*g)(p); p=k; } return p; } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- float newton(float (*f)(float),float (*parf)(float),float *x) { float o,p; float k,l; int m=0; o=(*x); if((*parf)(o)!=0) { p=o-(*f)(o)/(*parf)(o); //printf("p=%lf",p); while(fabs((p-o)/o)>0 && fabs((*f)(p))>0 && m<MAX && (*parf)(o)!=0) { //printf("p=%lf",p); m++; k=(*f)(p); l=(*parf)(p); o=p; p=o-k/l; } } else { printf("H methodos NR den paizei"); } return p; } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- float temnousas(float(*f)(float),float *x,float *y) { float o,p,k,w,l; int m=0; o=(*x); w=(*y); if((*f)(o)!=(*f)(w)) { p=o-((*f)(o)*(o-w)/((*f)(o)-(*f)(w))); while(fabs((p-o)/o)>0 && fabs((*f)(p))>0 && (*f)(o)!=(*f)(w)) { //printf("p=%lf",p); k=(*f)(p); l=(*f)(o); w=o; o=p; p=o-(k*(o-w)/(k-l)); } } else { printf("H methodos tis temnousas den paizei"); } return p; } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- int ebisection(float (*f)(float),float *a,float *b) { int m; float x,g,h; float c,r; c=(*a+*b)/2; //printf("%lf",c); m=0; while(((*b-*a)>0) && ((*f)(c)!=0) && m<MAX) { //printf("%lf",c); c=(*a+*b)/2; g=(*f)(*a); r=(*f)(*b); h=(*f)(c); m++; if(h==0) { return (m); x=c; } else if(g*h<0 ) { *b=c; } else if(r*h<0) { *a=c; } else if(g*h>0 && r*h>0) { return (m); printf("h methodos tis dix den paizei"); //return (*a+*b)/2; } } x=(*b+*a)/2; return (m); } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- int estshmeiou(float (*f)(float),float (*g)(float),float *x) { int m; float p,k; p=(*g)(*x); //printf("p=%lf",p); m=0; while(fabs((*f)(p))>0 && m<MAX) { //printf("p=%lf",p); m++; k=(*g)(p); p=k; } return m; } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- int enewton(float (*f)(float),float (*parf)(float),float *x) { float o,p; float k,l; int m; o=(*x); if((*parf)(o)!=0) { p=o-(*f)(o)/(*parf)(o); //printf("p=%lf",p); m=0; while(fabs((p-o)/o)>0 && fabs((*f)(p))>0 && m<MAX && (*parf)(o)!=0) { //printf("p=%lf",p); m++; k=(*f)(p); l=(*parf)(p); o=p; p=o-k/l; } } else { printf("H methodos NR den paizei"); } return m; } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- int etemnousas(float(*f)(float),float *x,float *y) { float o,p,k,w,l; int m; o=(*x); w=(*y); if((*f)(o)!=(*f)(w)) { p=o-((*f)(o)*(o-w)/((*f)(o)-(*f)(w))); m=0; while(fabs((p-o)/o)>0 && fabs((*f)(p))>0 && (*f)(o)!=(*f)(w)) { //printf("p=%lf",p); m++; k=(*f)(p); l=(*f)(o); w=o; o=p; p=o-(k*(o-w)/(k-l)); } } else { printf("H methodos tis temnousas den paizei"); } return m; } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- float nnewton(float (*f)(float),float (*parf)(float),float *x) { float o,p; float k,l; int m=0; o=(*x); if((*parf)(o)!=0) { p=o-(*f)(o)/(*parf)(o); //printf("p=%lf",p); while(fabs((p-o)/o)>ENR && fabs((*f)(p))>ENR && m<MAX && (*parf)(o)!=0) { //printf("p=%lf",p); m++; k=(*f)(p); l=(*parf)(p); o=p; p=o-k/l; } } else { printf("H methodos NR den paizei"); } return p; } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- float nbisection(float (*f)(float),float *a,float *b) { int m; float x,g,h; float c,r; c=(*a+*b)/2; //printf("%lf",c); m=0; while(((*b-*a)>ED) && ((*f)(c)!=0) && m<MAX) { //printf("%lf",c); c=(*a+*b)/2; g=(*f)(*a); r=(*f)(*b); h=(*f)(c); m++; if(h==0) { x=c; } else if(g*h<0 ) { *b=c; } else if(r*h<0) { *a=c; } else if(g*h>0 && r*h>0) { printf("h methodos tis dix den paizei"); return (*a+*b)/2; } } x=(*b+*a)/2; return (x); } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- float f1(float x) { return (x*x*x*x-x*x*x-3*x*x+5*x-2); } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- float f2(float x) { return (exp(x)-5*x*x); } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- float f3(float x) { return (tan(x)-(0.5)*x); } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- float g1(float x) { return (((-4)*x*x*x+3*x*x+6*x)/5); } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- float g2(float x) { return (sqrt((exp(x)/5))); } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- float g3(float x) { return (tan(x)/0.5); } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- float parf1(float x) { return (4*x*x*x-3*x*x-6*x+5); } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- float parf2(float x) { return (exp(x)-10*x); } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- float parf3(float x) { return (1/(cos(x)*cos(x))-(0.5)); } //-------------------------------------------------------------------------------------------- //-------------------------------------------------------------------------------------------- //------------------------------------- END ------------------------------------------------