diff --git a/Makefile b/Makefile index 323f755..5b8227c 100644 --- a/Makefile +++ b/Makefile @@ -4,9 +4,8 @@ CFLAGS := -Wall -g -std=c90 LDFLAGS := -lm LIB_DIR := ./lib/nr/ansi -LIB_INCLUDE := $(LIB_DIR)/other -LIB_SRCS := $(LIB_DIR)/recipes/machar.c -LIB_OBJS := $(patsubst %.c,$(BUILD_DIR)/lib/%.o,$(notdir $(LIB_SRCS))) +LIB_INCLUDE := -I$(LIB_DIR)/other + HW_DIR := ./hws @@ -15,6 +14,15 @@ ifeq ($(strip $(hw)),) else TARGET_NAME := hw$(hw) SRC_DIR := $(HW_DIR)/hw$(hw) + ifeq ($(strip $(hw)),1) + LIB_SRCS := $(LIB_DIR)/recipes/machar.c + else ifeq ($(strip $(hw)),2) + LIB_SRCS := $(LIB_DIR)/other/nrutil.c $(LIB_DIR)/recipes/rtflsp.c $(LIB_DIR)/recipes/rtbis.c $(LIB_DIR)/recipes/rtsec.c $(LIB_DIR)/recipes/rtnewt.c $(LIB_DIR)/recipes/rtsafe.c $(LIB_DIR)/recipes/zbrak.c $(LIB_DIR)/recipes/bessj0.c $(LIB_DIR)/recipes/bessj1.c + else + LIB_SRCS := "" + endif + LIB_OBJS := $(patsubst %.c,$(BUILD_DIR)/lib/%.o,$(notdir $(LIB_SRCS))) + endif ifeq ($(strip $(TARGET_NAME)),) @@ -24,8 +32,10 @@ else TARGET_EXEC := $(BUILD_DIR)/$(TARGET_NAME).out TARGET_SRCS := $(wildcard $(SRC_DIR)/*.c) TARGET_OBJS := $(patsubst %.c,$(BUILD_DIR)/hw$(hw)/%.o,$(notdir $(TARGET_SRCS))) + TARGET_INCLUDE := -I$(SRC_DIR) OBJS := $(TARGET_OBJS) $(LIB_OBJS) SRCS := $(TARGET_SRCS) $(LIB_SRCS) + INCLUDE := $(TARGET_INCLUDE) $(LIB_INCLUDE) all: run endif @@ -43,13 +53,13 @@ build: init $(TARGET_EXEC) $(TARGET_EXEC): $(OBJS) @echo "링킹 중: $@" - $(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@ + $(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS) $(OBJS): %.o: @echo "컴파일 중: $@" $(eval TARGET_C_BASENAME := $(patsubst %.o,%.c,$(notdir $@))) $(eval FOUND_SRC := $(foreach src,$(SRCS),$(if $(filter $(TARGET_C_BASENAME),$(notdir $(src))),$(src)))) - $(CC) $(CFLAGS) $(LDFLAGS) -I $(LIB_INCLUDE) -c $(FOUND_SRC) -o $@ + $(CC) $(CFLAGS) $(INCLUDE) $(LDFLAGS) -c $(FOUND_SRC) -o $@ run: clean build @echo "실행 시작" @@ -58,4 +68,4 @@ run: clean build dist: mkdir -p out - zip -j -9 out/$(TARGET_NAME).zip $(TARGET_SRCS) \ No newline at end of file + zip -j -9 out/$(TARGET_NAME).zip $(TARGET_SRCS) \ No newline at end of file diff --git a/hws/hw2/main.c b/hws/hw2/main.c new file mode 100644 index 0000000..26efcd4 --- /dev/null +++ b/hws/hw2/main.c @@ -0,0 +1,88 @@ +#include +#include + +#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; +} \ No newline at end of file diff --git a/hws/hw2/muller.c b/hws/hw2/muller.c new file mode 100644 index 0000000..e69de29 diff --git a/hws/hw2/muller.h b/hws/hw2/muller.h new file mode 100644 index 0000000..e69de29