create hw2 and some commit
This commit is contained in:
88
hws/hw2/main.c
Normal file
88
hws/hw2/main.c
Normal file
@@ -0,0 +1,88 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "nr.h"
|
||||
|
||||
void dbessj0(float x, float *y, float *dy) {
|
||||
*y = bessj0(x);
|
||||
*dy = -bessj1(x);
|
||||
}
|
||||
|
||||
int main() {
|
||||
/*
|
||||
1. using zbrak, find all possible intervals with roots.
|
||||
2. and use routines and enumerate all roots.
|
||||
*/
|
||||
|
||||
float a = 1.0, b = 10.0;
|
||||
float xacc = 0.000001;
|
||||
|
||||
int n = 23;
|
||||
float *xb1 = calloc(sizeof(float), n);
|
||||
float *xb2 = calloc(sizeof(float), n);
|
||||
int nb;
|
||||
|
||||
int i;
|
||||
zbrak(bessj0, a, b, n, xb1, xb2, &nb);
|
||||
|
||||
for (i = 1; i <= nb; i++) {
|
||||
printf("[%f, %f]\n", xb1[i], xb2[i]);
|
||||
}
|
||||
|
||||
float *roots_bisect = calloc(sizeof(float), nb);
|
||||
float *roots_linter = calloc(sizeof(float), nb);
|
||||
float *roots_secant = calloc(sizeof(float), nb);
|
||||
float *roots_newt = calloc(sizeof(float), nb);
|
||||
float *roots_newtsafe = calloc(sizeof(float), nb);
|
||||
|
||||
for (i = 0; i < nb; i++) {
|
||||
float l = xb1[i + 1];
|
||||
float r = xb2[i + 1];
|
||||
roots_bisect[i] = rtbis(bessj0, l, r, xacc);
|
||||
roots_linter[i] = rtflsp(bessj0, l, r, xacc);
|
||||
roots_secant[i] = rtsec(bessj0, l, r, xacc);
|
||||
roots_newt[i] = rtnewt(dbessj0, l, r, xacc);
|
||||
roots_newtsafe[i] = rtsafe(dbessj0, l, r, xacc);
|
||||
}
|
||||
|
||||
printf("besection roots: ");
|
||||
for (i = 0; i < nb; i++) {
|
||||
printf("%f ", roots_bisect[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
printf("lin-interpoalation roots: ");
|
||||
for (i = 0; i < nb; i++) {
|
||||
printf("%f ", roots_linter[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
printf("secant roots: ");
|
||||
for (i = 0; i < nb; i++) {
|
||||
printf("%f ", roots_secant[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
printf("newton-raphson roots: ");
|
||||
for (i = 0; i < nb; i++) {
|
||||
printf("%f ", roots_newt[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
printf("newton with bracketing roots: ");
|
||||
for (i = 0; i < nb; i++) {
|
||||
printf("%f ", roots_newtsafe[i]);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
/* free */
|
||||
free(xb1);
|
||||
free(xb2);
|
||||
|
||||
free(roots_bisect);
|
||||
free(roots_linter);
|
||||
free(roots_secant);
|
||||
free(roots_newt);
|
||||
|
||||
return 0;
|
||||
}
|
||||
0
hws/hw2/muller.c
Normal file
0
hws/hw2/muller.c
Normal file
0
hws/hw2/muller.h
Normal file
0
hws/hw2/muller.h
Normal file
Reference in New Issue
Block a user