Commit 3459d63b authored by Taddeüs Kroes's avatar Taddeüs Kroes

Added assignment 1-3 and Makefile.

parent c958a4f7
q1
q2
q3
q4
q5
*.o
CC=clang
CFLAGS=-Wall -Wextra -pedantic -std=c99 -D_GNU_SOURCE
LFLAGS=-lm
all: q1 q2 q3
q1: q1.o
$(CC) $(CFLAGS) $(LFLAGS) -o $@ $^
q2: q2.o
$(CC) $(CFLAGS) $(LFLAGS) -o $@ $^
%.o: %.c
$(CC) $(CFLAGS) $(LFLAGS) -o $@ -c $^
clean:
for i in `seq 5`; do \
rm -vf q$$i; \
done;
rm -vf *.o
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#define H 1e-3
#define TABLE_LINE(func, x) (printf("%-24s%.12f\t%.12f\n", #x, \
slope_right(func, x, H), slope_central(func, x, H)))
typedef double (*func_ptr)(double x);
double slope_right(func_ptr func, double x, double h) {
return (func(x + h) - func(x)) / h;
}
double slope_central(func_ptr func, double x, double h) {
return (func(x + h) - func(x - h)) / (2 * h);
}
int main(void) {
puts("x\t\t\tright\t\tcentral");
TABLE_LINE(&sin, M_PI / 3);
TABLE_LINE(&sin, 100 * M_PI + M_PI / 3);
TABLE_LINE(&sin, 1e12 * M_PI + M_PI / 3);
return 0;
}
#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;
}
double func(double x) {
return x * sin(x) - 1;
}
int main(void) {
int steps;
printf("zero point: %.20f\n", bisec(&func, 0, 2, &steps));
printf("Steps: %d\n", steps);
return 0;
}
#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;
}
double func(double x) {
return x * sin(x) - 1;
}
int main(void) {
int steps;
printf("zero point: %.20f\n", bisec(&func, 0, 2, &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