C Acil Yardım (Bisection Metoduyla Kök Bulma
-
arkadaşlar bisection metoduyla 4. dereceden bi denklemin köklerini bulan bi program yapmak zorundayım.bi program yaptım ama sadece 1 kökü bulabilio.programın çalışma mantığı şöyle kullanıcı kökün hangi aralıkta aranacağını belirtior ve hata payını giriyo sonra prog bu aralıkta fonksiyonun işaretinin değiştiği değere yaklaşarak çok yaklaşık bir değer buluyor(hata payını göz önünde tutarak).bir kökü bulan c programın kodları şöyle:
program için test değeleri( -1.0 1.0 error tolerance için 0.001 girin)
sonuc şöyle olcak g(-0.7290039) = -2.697494e-05
/* Finds roots of the equations
g(x)=0 and h(x)=0
on a specified intervel [xLeft ,xRight] using the bisection method.*/
#include <stdio.h>
#include <math.h>
#define FALSE 0;
#define TRUE 1;
double bisect(double xLeft, double xRight, double epsilon,
double f(double farg), int *errp);
double g(double x);
int
main(void)
{
double xLeft, xRight, /* left, right endpoints of intervel */
epsilon, /* error tolerance */
root;
int error;
/* Get endpoints and error tolerance from user */
printf("\nEnter interval endpoints> ");
scanf("%lf%lf", &xLeft, &xRight);
printf("\nEnter tolerance> ");
scanf("%lf", &epsilon);
/* Use bisect function to look for roots of g and h */
printf("\n\nFunction g");
root = bisect(xLeft, xRight, epsilon, g, &error);
if(!error)
printf("\n g(%.7f) = %e\n", root, g(root));
return (0);
}
/*
* Implements the bisection method for finding a root of a function f.
* Finds a root (and sets output parameter error flag to FALSE) if
* signs of fp(xLeft) and fp(xRight) are different.Otherwise sets
* output parameter error flag to TRUE
*/
double bisect(double xLeft, /* input - endpoints of interval in */
double xRight, /* which to look for a root */
double epsilon, /* input - error tolerance */
double f(double farg), /* input - the function */
int *errp) /* output- error flag */
{
double xMid, /* midpoint of interval */
fLeft, /* f(xLeft) */
fMid, /* f(xMid) */
fRight; /* f(xRight) */
int rootFound = FALSE;
/* Computes function values at initial and endpoints of interval */
fLeft = f(xLeft);
fRight= f(xRight);
/* If no change of sign occurs on the interval there is not a
unique root.Searches for the unique root if there is one .*/
if(fLeft * fRight>0){ /* same sign */
*errp = TRUE;
printf("\nMay be no root in [%.7f, %.7f]\n\n", xLeft, xRight);
}
else{
*errp = FALSE;
/* Searches as long as interval size is large enough
and no root has been found */
while(fabs(xRight - xLeft) > epsilon && !rootFound){
/* Computes midpoint and function value at midpoint */
xMid = (xLeft + xRight) / 2.0;
fMid = f(xMid);
if(fMid == 0.0){ /* Here's the root */
rootFound = TRUE;
}else if(fLeft * fMid < 0.0){ /* Root in [xLeft, xMid] */
xRight = xMid;
}else{
xLeft = xMid;
fLeft = fMid;
}
/* Prints root and interval or new interval */
if(rootFound)
printf("\nRoot found at x=%.7f, midpoint of [%.7f,%.7f]", xMid, xLeft, xRight);
else
printf("\nNew interval is [%.7f, %.7f]", xLeft, xRight);
}
}
/* If there is a root, it is the midpoint of [xLeft, xRight] */
return ((xLeft + xRight) / 2.0);
}
/* Functions for which roots are sought */
/* 3 2
* 5x - 2x + 3
*/
double g(double x)
{
return(5 * pow(x, 3.0) - 2 * pow(x, 2.0) + 3);
}
şimdi 4. dereceden bir denklemin köklerini bulan bir program yazmalıyım.Bu sefer kullanıcıdan sadece sol limiti ve hata payını alcak yani belirli bir aralık almıcak.sonra da 4 tane köküde ekrana yazdıracak.ark lar prog mı ıstemıom sadce fikir edinmek istiom.algoritmada nasıl bir yol izlemeliyim.şimdiden tşkler
-
sakincali baslik bkz. forum kurallari.
