일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 직교행렬#정규직교행렬#orthonormal#reflection matrix#dcm
- 가중 최소제곱법
- 추정문제#normal equation#직교방정식#정사영#정사영행렬#정사영 변환
- 부분 분수분해
- 최소자승#least-square#목적함수#양한정#정점조건#최소조건
- dirichlet
- 미분방정식 #선형 미분방정식 #상미분 방정식
- 2계미방#모드#mod#특성방정식#characteristic eq#제차해
- reflection matrix
- 선형독립#기저벡터#선형확장#span#basis
- 내적 공간#적분
- 선형변환#contraction#expansions#shears#projections#reflection
- 오일러-코시 미방#계수내림법
- 선형시스템연산자#라이프니츠 법칙#fundamental theorem of algebra#erf
- 정규직교행렬
- 내적#duality#쌍대성#dot product
- 푸리에 정리
- weighted least-squares
- 여인수 행렬
- 변수분리#동차 미분방정식#완전 미분방정식
- 베르누이 미분방정식
- 선형 상수계수 미분방정식#lccode#sinusoidal input
- 계수내림법#reduction of order#wronskian#론스키안#아벨항등식#abel's identity
- 비제차#제차해#일반해#적분인자#적분인자법#homogeneous sol#nonhomogeneous sol#integrating factor
- 여인자
- 그람-슈미트 과정#gram-schmidt process
- 푸리에 급수
- linespectra#feurierseries#푸리에 급수
- 상태천이행렬#적분인자법#미정계수법#케일리-헤밀톤 정리
- 멱급수법
Archives
OnlyOne
Practice[Integral] 본문
Practice[Integral]
Intro
Today we will calculate integral by c programming.
Header File
/*
================================================
Handong Global University
------------------------------------------------
Name: Taesan Kim
ID: 22300203
Create: 2024.07.19
Modifier: 2024.07.19
------------------------------------------------
전역변수를 이용하여 적분을 지원한다.
================================================
*/
#ifndef MYINTEGRAL_H
#define MYINTEGRAL_H
#include <stdio.h>
#include <math.h>
/**
* @breif
*
* @f: 적분할 함수
* @a: 적분 시작값
* @b: 적분 끝값
* @n: 적분구간 분할개수
*
* @return: 적분값
*/
double rect(double(*f)(double), double a, double b, int n);
double trpz(double (*f)(double), double a, double b, int n);
double smps(double (*f) (double), double a, double b, int n);
double adapt(double(*f)(double), double a, double b, double tolerance);
#endif //MYINTEGRAL_H
Source File
#include "include/myIntegral.h"
double rect(double(*f)(double), double a, double b, int n)
{
int i;
double dt = (b - a) / n, sum = 0;
for (i = 0; i < n; i++) sum += f(a + dt / 2 + i * dt) * dt;
return sum;
}
double trpz(double (*f)(double), double a, double b, int n)
{
int i;
double dt = (b - a) / n, sum = 0;
for (i = 0; i < n; i++) sum += ((f(a + i * dt) + f(a + (i + 1) * dt)) / 2) * dt;
return sum;
}
double smps(double (*f) (double), double a, double b, int n)
{
int i;
double dt = (b - a) / n, sum = 0;
for (i = 0; i < n; i++)sum += (f(a+i*dt) + 4 * f(a + dt / 2 + i*dt) + f(i*dt + a + dt)) * dt / 6;
return sum;
}
double adapt(double(*f)(double), double a, double b, double tolerance)
{
double x = smps(f, a, b, 4);
double x2 = smps(f, a, b, 2);
if (fabs(x - x2) > tolerance)
{
double tegral_1 = adapt(f, a, (a + b) / 2, tolerance);
double tegral_2 = adapt(f, (a + b) / 2, b, tolerance);
return tegral_1 + tegral_2;
}
else if (fabs(x - x2) <= tolerance)
{
return x;
}
}
Main
#include "../include/myIntegral.h"
#define f(x) (4*x*x + 6*x + 9)
//매크로는 단순한 대체이므로 함수 포인터로 사용할 수 없다.
double f_function(double x)
{
return powf(x, 5.0) - powf(x, 4.0);
}
int main() {
printf("rectangular Rule : %f\n\n", rect(f_function, 0.0, 10.0, 10));
printf("trapezoid Rule : %f\n\n", trpz(f_function, 0.0, 10.0, 10));
printf("smpson Rule : %f\n\n", smps(f_function, 0.0, 10.0, 10));
printf("adapted Rule : %f\n\n", adapt(f_function, 0.0, 10.0, 0.01));
double input[11] = { 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0 };
double dt = 0.1;
double n;
double integral;
double pre_val = 0.0;
for (int i = 0; i < 10; i++)
{
n = (input[i+1] - input[i]) / dt;
integral = smps(f_function, input[i], input[i + 1], n);
pre_val += integral;
printf("integral[%d] : %f\n", i, pre_val);
}
return 0;
}
'Control Engineering > C Programming' 카테고리의 다른 글
배열(Array) 기초 (0) | 2024.09.04 |
---|---|
Practice[MAF] (1) | 2024.09.04 |
Practice[Root Formula] (0) | 2024.09.04 |
구조체(Derived Type) (0) | 2024.09.04 |
Operator(비트연산자와 삼항연산자) (0) | 2024.08.10 |