create hw2 and some commit

This commit is contained in:
2025-09-24 00:08:00 +09:00
parent 60b7c68313
commit 79b9f0be14
4 changed files with 104 additions and 6 deletions

View File

@@ -4,9 +4,8 @@ CFLAGS := -Wall -g -std=c90
LDFLAGS := -lm LDFLAGS := -lm
LIB_DIR := ./lib/nr/ansi LIB_DIR := ./lib/nr/ansi
LIB_INCLUDE := $(LIB_DIR)/other LIB_INCLUDE := -I$(LIB_DIR)/other
LIB_SRCS := $(LIB_DIR)/recipes/machar.c
LIB_OBJS := $(patsubst %.c,$(BUILD_DIR)/lib/%.o,$(notdir $(LIB_SRCS)))
HW_DIR := ./hws HW_DIR := ./hws
@@ -15,6 +14,15 @@ ifeq ($(strip $(hw)),)
else else
TARGET_NAME := hw$(hw) TARGET_NAME := hw$(hw)
SRC_DIR := $(HW_DIR)/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 endif
ifeq ($(strip $(TARGET_NAME)),) ifeq ($(strip $(TARGET_NAME)),)
@@ -24,8 +32,10 @@ else
TARGET_EXEC := $(BUILD_DIR)/$(TARGET_NAME).out TARGET_EXEC := $(BUILD_DIR)/$(TARGET_NAME).out
TARGET_SRCS := $(wildcard $(SRC_DIR)/*.c) TARGET_SRCS := $(wildcard $(SRC_DIR)/*.c)
TARGET_OBJS := $(patsubst %.c,$(BUILD_DIR)/hw$(hw)/%.o,$(notdir $(TARGET_SRCS))) TARGET_OBJS := $(patsubst %.c,$(BUILD_DIR)/hw$(hw)/%.o,$(notdir $(TARGET_SRCS)))
TARGET_INCLUDE := -I$(SRC_DIR)
OBJS := $(TARGET_OBJS) $(LIB_OBJS) OBJS := $(TARGET_OBJS) $(LIB_OBJS)
SRCS := $(TARGET_SRCS) $(LIB_SRCS) SRCS := $(TARGET_SRCS) $(LIB_SRCS)
INCLUDE := $(TARGET_INCLUDE) $(LIB_INCLUDE)
all: run all: run
endif endif
@@ -43,13 +53,13 @@ build: init $(TARGET_EXEC)
$(TARGET_EXEC): $(OBJS) $(TARGET_EXEC): $(OBJS)
@echo "링킹 중: $@" @echo "링킹 중: $@"
$(CC) $(CFLAGS) $(LDFLAGS) $^ -o $@ $(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
$(OBJS): %.o: $(OBJS): %.o:
@echo "컴파일 중: $@" @echo "컴파일 중: $@"
$(eval TARGET_C_BASENAME := $(patsubst %.o,%.c,$(notdir $@))) $(eval TARGET_C_BASENAME := $(patsubst %.o,%.c,$(notdir $@)))
$(eval FOUND_SRC := $(foreach src,$(SRCS),$(if $(filter $(TARGET_C_BASENAME),$(notdir $(src))),$(src)))) $(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 run: clean build
@echo "실행 시작" @echo "실행 시작"

88
hws/hw2/main.c Normal file
View 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
View File

0
hws/hw2/muller.h Normal file
View File