Skip to content

Instantly share code, notes, and snippets.

@hasayvaz
Created April 10, 2012 12:52
Show Gist options
  • Save hasayvaz/2351157 to your computer and use it in GitHub Desktop.
Save hasayvaz/2351157 to your computer and use it in GitHub Desktop.
sayısal çözümleme ödevi-secant, newton, bisection ve regular falsi yöntemi kullanarak denklem kökü bulma.
#include<stdio.h>
#include<math.h>
#define BOYUT 50
#define TUREV_K (double)0.0000001
double f(double x)
{
return 3 * x * x + 12 * x + 7;
//return x * x * x - 5 * x * x - 2 * x + 10;
}
double turev(double x, double(*f)(double), double k)
{
return (double)((f(x + k) - f(x))/k);
}
double newton (double x0, double eps)
{
int i;
double f(double);
double x[BOYUT];
for (x[0] = x0, i = 0;;i++) {
x[i+1] = x[i] - (f(x[i])/turev(x[i], f, TUREV_K));
if (fabs(x[i+1] - x[i]) < eps)
break;
}
return x[i+1];
}
double secant(double x0, double x1, double eps)
{
int i;
double f(double);
double x[BOYUT];
for (x[0] = x0, x[1] = x1, i = 0;;i++) {
x[i+2] = x[i+1] - (f(x[i+1]) * (x[i+1] - x[i]))/(f(x[i+1]) - f(x[i]));
if (fabs(x[i+2] - x[i+1]) < eps)
break;
}
return x[i+2];
}
double bisection(double a, double b, double eps)
{
double c, f(double);
if (f(a) * f(b) < 0) {
c = (a + b)/2;
while (!(f(c) == 0)) {
c = (a + b)/2;
if (f(a) * f(b) < 0) {
b = c;
}
else {
a = c;
}
if (f(c) < eps)
break;
}
}
return c;
}
double regfal(double a, double b, double eps)
{
int i;
double f(double);
double x[BOYUT];
if (f(a) * f(b) < 0) {
for (i = 0;;i++) {
x[i] = (a * f(b) - b * f(a))/ (f(b) - f(a));
if (f(a) * f(x[i]) < 0)
b = x[i];
else
a = x[i];
if ((x[i] - x[i-1]) < eps)
break;
}
}
return x[i];
}
void main(void)
{
double a = 1, b = 3, x0 = 4, x1 = 1;
double eps = pow(10, -5);
//printf("bisec_kok = %.5f\n", bisection(a, b, eps));
//printf("regfal_kok = %.5f\n", regfal(a, b, eps));
//printf("newton_kok = %.5f\n", newton(x0, eps));
//printf("secant_kok = %.5f\n", secant(x0, x1, eps));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment