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

ModSim: Finished ass4 code.

parent cf6a9736
...@@ -9,7 +9,7 @@ q%: q%.o ...@@ -9,7 +9,7 @@ q%: q%.o
q2: bisection.o q2: bisection.o
q3: bisection.o regula_falsi.o newton_raphson.o q3: bisection.o regula_falsi.o newton_raphson.o
q4: newton_raphson.o q4: bisection.o regula_falsi.o newton_raphson.o
q5: integral.o q5: integral.o
q6: integral.o q6: integral.o
......
...@@ -2,9 +2,11 @@ ...@@ -2,9 +2,11 @@
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>
#include "newton_raphson.h" #include "newton_raphson.h"
#include "bisection.h"
#include "regula_falsi.h"
#define EPSILON 1e-11 #define EPSILON 1e-11
#define BISEC_STEPS 5 #define MAX_STEPS 100000
/* /*
* Manually define the functions and their derivatives. * Manually define the functions and their derivatives.
...@@ -34,14 +36,44 @@ double df3(double x) { ...@@ -34,14 +36,44 @@ double df3(double x) {
return (x * x + 1) + 2 * x * (x - 4); return (x * x + 1) + 2 * x * (x - 4);
} }
int main(void) { #define PRINT_ZERO(f, df, x_start) { \
unsigned int steps; if( !isnan(root = newton_raphson(&f, &df, x_start, EPSILON, &steps, MAX_STEPS)) ) \
double root; printf(#f ": %.11f (%d steps from start point %.11f)\n", root, steps, (double)x_start); \
else \
printf(#f ": could not find a root after %d steps\n", steps); \
}
#define PRINT_ZERO_RANGE(f, df, method, lnbd, ubnd) { \
x_start = method(&f, lnbd, ubnd, EPSILON, &start_steps, max_start_steps); \
printf("Did first %d steps using " #method "\n", start_steps); \
PRINT_ZERO(f, df, x_start); \
}
int main(int argc, char *argv[]) {
unsigned int steps, start_steps, max_start_steps;
double root, x_start;
if( argc != 2 ) {
printf("Usage: %s START_STEPS\n", argv[0]);
return 1;
}
max_start_steps = atoi(argv[1]);
PRINT_ZERO(f1, df1, 0);
PRINT_ZERO(f2, df2, -10);
PRINT_ZERO(f2, df2, 10);
PRINT_ZERO(f3, df3, 0);
printf("\nNow using Bisection to determine the first %d steps.\n\n", max_start_steps);
PRINT_ZERO_RANGE(f2, df2, bisec, 0, 10);
PRINT_ZERO_RANGE(f3, df3, bisec, 0, 10);
printf("\nNow using Regula Falsi to determine the first %d steps.\n\n", max_start_steps);
if( !isnan(root = newton_raphson(&f2, &df2, 1000000, EPSILON, &steps, 100000)) ) PRINT_ZERO_RANGE(f2, df2, regula_falsi, 0, 10);
printf("f2: %.11f (%d steps)\n", root, steps); PRINT_ZERO_RANGE(f3, df3, regula_falsi, 0, 10);
else
printf("f2: could not find a root after %d steps\n", steps);
return 0; 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