Commit ed8fe53c authored by Taddeüs Kroes's avatar Taddeüs Kroes

Added assignment 3.

parent d3de400b
......@@ -10,6 +10,9 @@ q1: q1.o
q2: q2.o
$(CC) $(CFLAGS) $(LFLAGS) -o $@ $^
q3: q3.o
$(CC) $(CFLAGS) $(LFLAGS) -o $@ $^
%.o: %.c
$(CC) $(CFLAGS) $(LFLAGS) -o $@ -c $^
......
#define EPSILON 1e-11
typedef double (*func_ptr)(double x);
double bisec(func_ptr f, double left, double right, int *steps) {
int i;
double mid;
for( i = 1; fabs(right - left) > 2 * EPSILON; i++ ) {
mid = (right + left) / 2;
if( f(left) * f(mid) < 0 )
right = mid;
else if( f(right) * f(mid) < 0 )
left = mid;
else
break;
}
*steps = i;
return mid;
}
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define EPSILON 1e-11
typedef double (*func_ptr)(double x);
double bisec(func_ptr f, double left, double right, int *steps) {
int i;
double mid;
for( i = 1; fabs(right - left) > 2 * EPSILON; i++ ) {
mid = (right + left) / 2;
if( f(left) * f(mid) < 0 )
right = mid;
else if( f(right) * f(mid) < 0 )
left = mid;
else
break;
}
*steps = i;
return mid;
}
#include "bisec.h"
double func(double x) {
return x * sin(x) - 1;
......
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "bisec.h"
#define EPSILON 1e-11
typedef double (*func_ptr)(double x);
double bisec(func_ptr f, double left, double right, int *steps) {
int i;
double mid;
for( i = 1; fabs(right - left) > 2 * EPSILON; i++ ) {
mid = (right + left) / 2;
if( f(left) * f(mid) < 0 )
right = mid;
else if( f(right) * f(mid) < 0 )
left = mid;
else
break;
}
*steps = i;
return mid;
}
double func(double x) {
return x * sin(x) - 1;
double quad(double x) {
return x * x - 2;
}
int main(void) {
int steps;
printf("zero point: %.20f\n", bisec(&func, 0, 2, &steps));
printf("Square root of 2: %.11f\n", bisec(&quad, 1.4, 1.5, &steps));
printf("Steps: %d\n", steps);
return 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment