1 ヘッダ項目一覧

2 R markdownの設定方法

  1. メニューバーの歯車マークのプルダウンメニュー→「Preview in Viewer pain」にチェックをいれるとペイン内にドキュメントが表示されるようになる。

  2. メニューバーの「knit on Save」にチェックをいれると保存したときにドキュメントを生成する。

3 数値積分

\(f(x)=x^2, 0 \le x \le 1とx軸で囲まれる面積\)

\[ \int_0^1 f(x) dx = \int_0^1 x^2 dx = \left[\frac{1}{3}x^3\right]_0^1=\frac{1}{3}=0.33\cdots \]

f <- function(x) x^2
integrate(f, 0, 1)
## 0.3333333 with absolute error < 3.7e-15

一行で書く場合

integrate(function(x) x^2, 0, 1)
## 0.3333333 with absolute error < 3.7e-15

グラフ

x <- seq(-1, 2, 0.01)
matplot(x = x, y = f(x), type = "l", col = "blue")
abline(v = c(0, 1), col = gray(0.2), lty = 2)

3.1 Pythonで数値積分する場合

from scipy import integrate

def f(x):
  return x**2

integrate.quad(f, 0, 1)
## (0.33333333333333337, 3.700743415417189e-15)

一行で書く場合

integrate.quad(lambda x: x**2, 0, 1)
## (0.33333333333333337, 3.700743415417189e-15)

3.2 積分区間に無限大を含む場合

\[ f(x) = e^{-x}\\ 1 \le x \le \infty \] 無限大の積分記号(Inf)を使う。

f <- function(x) exp(-x)
integrate(f, 1, Inf)
## 0.3678794 with absolute error < 2.1e-05
x <- seq(1, 10, 0.1)

matplot(x = x, y = f(x), type = "l")

グラフ描画練習

f <- function(x) 1/(x^2 - 4)

x <- seq(-5, 5, 0.1)

matplot(x = x, y = f(x), xlim = c(-5, 5), type = "l", col = "blue")
abline(h = seq(-5, 5, 1), v = seq(-5, 5, 1), col = gray(0.2), lty = 2)
abline(h = 0, v = 0)
abline(v = c(-2, 2), col = "red")