{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"toc-hr-collapsed": false
},
"source": [
"# Chapter2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## コメント"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1"
]
}
],
"source": [
"# コメントです \n",
"print(1) # 1を表示"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"#===\n",
"\n",
"複数行のコメント\n",
"こめんと\n",
"こめんと\n",
"\n",
"===#"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 1"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"x = 1; # セミコロンをつけると結果が表示されない"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"## Print文"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1"
]
}
],
"source": [
"print(x)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Control"
]
}
],
"source": [
"print(\"Control\")"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"制御工学"
]
}
],
"source": [
"print(\"制御\") # 改行なし\n",
"print(\"工学\")"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"制御\n",
"工学"
]
}
],
"source": [
"println(\"制御\") # 改行する場合\n",
"print(\"工学\")"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"制御\n",
"工学"
]
}
],
"source": [
"print(\"制御\\n工学\")"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"制御工学"
]
}
],
"source": [
"print(\"制御\", \"工学\")"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"10\n",
"x = 10\n",
"x = 10\n"
]
}
],
"source": [
"x = 10\n",
"println(x)\n",
"println(\"x = \", x)\n",
"println(\"x = $x\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"## Unicode"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"変数にUnicodeが使える"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"制御 = 2\n",
"3制御"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"LaTeXコードが使える"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"π = 3.1415926535897..."
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"π #\\pi"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9.42477796076938"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3π"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ℯ = 2.7182818284590..."
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ℯ #\\euler"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"α = 1 # \\alpha"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"P₀ = 1 # P\\_0"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Pₘ = 10"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"G⁰ = 1 # G\\^0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## カウント"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"length(\"せいぎょこうがく\")"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ℯ = 2.7182818284590..."
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ℯ #\\euler"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"α = 1 # \\alpha"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"P₀ = 1 # P\\_0"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"G⁰ = 1 # G\\^0"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"## 数値"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Int64"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"typeof(1)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Float64"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"typeof(1.0)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.0"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2 * 1.0 "
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.0"
]
},
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"10 / 5"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"17 ÷ 5 # \\div"
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"17 % 5"
]
},
{
"cell_type": "code",
"execution_count": 31,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"15//4"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"15 // 4"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 1\n",
"x += 1\n",
"# x = x + 1"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 2\n",
"x *= 3"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"243"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3^5"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.030000000000000006"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3*10^-2"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.03"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3e-2"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"300.0"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"3e2"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"演算子の省略"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 1\n",
"2x"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2(x+2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 変数"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"Int64\n"
]
}
],
"source": [
"x = 1\n",
"println(x)\n",
"println(typeof(x))"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.0\n",
"Float64\n"
]
}
],
"source": [
"y = 1.0\n",
"println(y)\n",
"println(typeof(y))"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 + 2im\n",
"Complex{Int64}\n"
]
}
],
"source": [
"z = 1 + 2im\n",
"println(z)\n",
"println(typeof(z))"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Control\n",
"String\n"
]
}
],
"source": [
"msg = \"Control\"\n",
"println(msg)\n",
"println(typeof(msg))"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"C\n",
"Char\n"
]
}
],
"source": [
"chr = 'C'\n",
"println(chr)\n",
"println(typeof(chr))"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"true\n",
"Bool\n"
]
}
],
"source": [
"ok = true\n",
"println(ok)\n",
"println(typeof(ok))"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Nothing"
]
},
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# nothing\n",
"typeof(nothing)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Missing"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# missing\n",
"typeof(missing)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 型変換"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"Int64\n"
]
}
],
"source": [
"z = convert(Int, y)\n",
"println(z)\n",
"println(typeof(z))"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"String\n"
]
}
],
"source": [
"word = string(x)\n",
"println(word)\n",
"println(typeof(word))"
]
},
{
"cell_type": "code",
"execution_count": 50,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 50,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 1\n",
"2x"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2(x+2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## リスト"
]
},
{
"cell_type": "code",
"execution_count": 52,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6-element Vector{Int64}:\n",
" 3\n",
" 5\n",
" 2\n",
" 4\n",
" 6\n",
" 1"
]
},
"execution_count": 52,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data1 = [3, 5, 2, 4, 6, 1]"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Vector{Int64}\n",
"Int64\n"
]
}
],
"source": [
"println(typeof(data1))\n",
"println(eltype(data1))"
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2-element Vector{Vector{Int64}}:\n",
" [3, 5, 2]\n",
" [4, 6, 1]"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data2 = [ [3, 5, 2], [4, 6, 1] ]\n",
"data2"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3"
]
},
"execution_count": 55,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data1[1]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### スライス"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2-element Vector{Int64}:\n",
" 3\n",
" 5"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data1[1:2]"
]
},
{
"cell_type": "code",
"execution_count": 57,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2-element Vector{Int64}:\n",
" 2\n",
" 4"
]
},
"execution_count": 57,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data1[3:4]"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4-element Vector{Int64}:\n",
" 2\n",
" 4\n",
" 6\n",
" 1"
]
},
"execution_count": 58,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data1[3:end]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### リスト用の関数"
]
},
{
"cell_type": "code",
"execution_count": 59,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7-element Vector{Int64}:\n",
" 1\n",
" 3\n",
" 5\n",
" 2\n",
" 4\n",
" 6\n",
" 1"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data2 = [3, 5, 2, 4, 6, 1] \n",
"\n",
"# 先頭に要素を追加\n",
"pushfirst!(data2, 1)"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 先頭の要素を取り出す\n",
"popfirst!(data2)"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7-element Vector{Int64}:\n",
" 3\n",
" 5\n",
" 2\n",
" 4\n",
" 6\n",
" 1\n",
" 10"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 末尾に要素を追加\n",
"push!(data2, 10)"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 末尾の要素を取り出す\n",
"pop!(data2)"
]
},
{
"cell_type": "code",
"execution_count": 63,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"7-element Vector{Int64}:\n",
" 3\n",
" 20\n",
" 5\n",
" 2\n",
" 4\n",
" 6\n",
" 1"
]
},
"execution_count": 63,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# i番目に要素を挿入する\n",
"insert!(data2, 2, 20)"
]
},
{
"cell_type": "code",
"execution_count": 64,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6-element Vector{Int64}:\n",
" 3\n",
" 5\n",
" 2\n",
" 4\n",
" 6\n",
" 1"
]
},
"execution_count": 64,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# i番目の要素を削除\n",
"deleteat!(data2, 2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## タプル"
]
},
{
"cell_type": "code",
"execution_count": 65,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(1, 2, 3, 4)\n",
"NTuple{4, Int64}\n"
]
}
],
"source": [
"tuple = (1,2,3,4) \n",
"println(tuple)\n",
"println(typeof(tuple))"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"ename": "LoadError",
"evalue": "MethodError: no method matching setindex!(::NTuple{4, Int64}, ::Int64, ::Int64)",
"output_type": "error",
"traceback": [
"MethodError: no method matching setindex!(::NTuple{4, Int64}, ::Int64, ::Int64)",
"",
"Stacktrace:",
" [1] top-level scope",
" @ In[66]:2",
" [2] eval",
" @ ./boot.jl:373 [inlined]",
" [3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)",
" @ Base ./loading.jl:1196"
]
}
],
"source": [
"# 書き換え不可\n",
"tuple[1] = 5"
]
},
{
"cell_type": "code",
"execution_count": 67,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4-element Vector{Int64}:\n",
" 5\n",
" 2\n",
" 3\n",
" 4"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"data = [1, 2, 3, 4]\n",
"data[1] = 5 #リストは書き換えができる\n",
"data"
]
},
{
"cell_type": "code",
"execution_count": 68,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(linestyle = \"-.\", color = \"k\")\n",
"NamedTuple{(:linestyle, :color), Tuple{String, String}}\n"
]
}
],
"source": [
"d = ( linestyle = \"-.\", color=\"k\") \n",
"println(d)\n",
"println(typeof(d))"
]
},
{
"cell_type": "code",
"execution_count": 69,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"-.\""
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d.linestyle"
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"k\""
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d.color"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 辞書"
]
},
{
"cell_type": "code",
"execution_count": 71,
"metadata": {},
"outputs": [],
"source": [
"dic = Dict();"
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [],
"source": [
"dic[\"linestyle\"] = \"-.\";\n",
"dic[\"color\"] = \"k\";"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"k\""
]
},
"execution_count": 73,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dic[\"color\"]"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"k\""
]
},
"execution_count": 74,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"dic2 = Dict( \"linestyle\" => \"-.\", \"color\" => \"k\" );\n",
"dic2[\"color\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## if文"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x is positive"
]
}
],
"source": [
"x = 1\n",
"\n",
"if x < 0\n",
" print(\"x is negative\")\n",
"elseif x == 0\n",
" print(\"x is zero\")\n",
"else\n",
" print(\"x is positive\")\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x and y are negative"
]
}
],
"source": [
"x ,y = -1, -1\n",
"\n",
"if x < 0 && y < 0\n",
" print(\"x and y are negative\")\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x or y is negative"
]
}
],
"source": [
"x, y = 1, -1\n",
"\n",
"if x < 0 || y < 0\n",
" print(\"x or y is negative\")\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 78,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x is in y\n",
"x is in y\n"
]
}
],
"source": [
"x = 2\n",
"y = [1, 2, 3] \n",
"\n",
"if x in y\n",
" println(\"x is in y\")\n",
"end\n",
"\n",
"if x ∈ y #\\in\n",
" println(\"x is in y\")\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 79,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"x is not in y"
]
}
],
"source": [
"x = 0\n",
"y = [1, 2, 3] \n",
"\n",
"if x ∉ y #\\notin\n",
" print(\"x is not in y\")\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3項演算子\n",
"条件 a が真(true)なら b を、偽なら c を値とする"
]
},
{
"cell_type": "code",
"execution_count": 80,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"x≤2\""
]
},
"execution_count": 80,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 1\n",
"\n",
"x > 2 ? \"x>2\" : \"x≤2\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## for文"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n"
]
}
],
"source": [
"for x in [0, 1, 2]\n",
" println(x)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n"
]
}
],
"source": [
"for x=0:1:2\n",
" println(x)\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 83,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1:a\n",
"2:b\n",
"3:c\n"
]
}
],
"source": [
"for (i, word) in enumerate([\"a\", \"b\", \"c\"])\n",
" println(i, \":\", word)\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 練習問題"
]
},
{
"cell_type": "code",
"execution_count": 84,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"julia\n",
"1\n",
"julia\n",
"2\n",
"julia\n"
]
}
],
"source": [
"# プログラム1\n",
"for x=0:1:2\n",
" println(x) \n",
" println(\"julia\")\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n"
]
}
],
"source": [
"# プログラム2\n",
"for x=0:1:2\n",
" println(x) \n",
" continue\n",
" println(\"julia\")\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 関数定義"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"say_hello (generic function with 1 method)"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function say_hello()\n",
" println(\"こんにちは\")\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 87,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"こんにちは\n"
]
}
],
"source": [
"say_hello()"
]
},
{
"cell_type": "code",
"execution_count": 88,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"subject (generic function with 1 method)"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function subject(name)\n",
" println(name * \"工学\")\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 89,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"制御工学\n"
]
}
],
"source": [
"subject(\"制御\")"
]
},
{
"cell_type": "code",
"execution_count": 90,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"add (generic function with 1 method)"
]
},
"execution_count": 90,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function add(a, b)\n",
" c = a + b\n",
" return c\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8"
]
}
],
"source": [
"result = add(3, 5)\n",
"print(result)"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"add2 (generic function with 1 method)"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add2(a, b) = a + b"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"8"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"add2(3, 5)"
]
},
{
"cell_type": "code",
"execution_count": 94,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"test (generic function with 2 methods)"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 引数の型で関数の中身を変えることができる\n",
"test(x::Int64) = \"せいすう\"\n",
"test(x::Float64) = \"しょうすう\""
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"せいすう\""
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test(2)"
]
},
{
"cell_type": "code",
"execution_count": 96,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"しょうすう\""
]
},
"execution_count": 96,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"test(2.0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## クロージャ"
]
},
{
"cell_type": "code",
"execution_count": 97,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"outer (generic function with 1 method)"
]
},
"execution_count": 97,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function outer(a, b)\n",
" function inner(c)\n",
" return c * (a + b) \n",
" end\n",
" return inner\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 98,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"9\n"
]
}
],
"source": [
"f = outer(1, 2) \n",
"r = f(3) \n",
"println(r)"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"21\n"
]
}
],
"source": [
"f2 = outer(3, 4)\n",
"r2 = f2(3)\n",
"println(r2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 無名関数"
]
},
{
"cell_type": "code",
"execution_count": 100,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"14"
]
},
"execution_count": 100,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"c = (a, b) -> 2*a + 3*b\n",
"c(1, 4)"
]
},
{
"cell_type": "code",
"execution_count": 101,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3-element Vector{Int64}:\n",
" 1\n",
" 4\n",
" 0"
]
},
"execution_count": 101,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"map(x -> x^2 + 2x + 1, [0, 1 ,-1])"
]
},
{
"cell_type": "code",
"execution_count": 102,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[32, 31, 30, 29, 28]\n"
]
}
],
"source": [
"data1 = [1, 2, 3, 4, 5]\n",
"data2 = [10, 9, 8, 7, 6]\n",
"result = map( (a, b) -> 2*a + 3*b, data1, data2)\n",
"println(result)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ジェネレータ"
]
},
{
"cell_type": "code",
"execution_count": 103,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"linestyle_generator (generic function with 1 method)"
]
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function linestyle_generator(lineID)\n",
" linestyle = [\"-\", \"--\", \"-.\", \":\"] \n",
" lineID = (lineID-1) % (length(linestyle)) + 1\n",
" return linestyle[lineID]\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 104,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-\n",
"--\n",
"-.\n",
":\n",
"-\n"
]
}
],
"source": [
"LS = (linestyle_generator(i) for i=1:1:5)\n",
"for i in LS\n",
" println(i)\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## リスト内包表記"
]
},
{
"cell_type": "code",
"execution_count": 105,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5]"
]
}
],
"source": [
"t = (1, 2, 3, 4, 5) \n",
"r1 = [i for i in t] \n",
"print(r1)"
]
},
{
"cell_type": "code",
"execution_count": 106,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 4]"
]
}
],
"source": [
"r2 = [i for i in t if i % 2 == 0] \n",
"print(r2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 練習問題"
]
},
{
"cell_type": "code",
"execution_count": 107,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"35.70714214271425\n",
"35.70714214271425\n",
"35.70714214271425"
]
}
],
"source": [
"# for文を使って足し合わせる \n",
"\n",
"s=0\n",
"for x = 1:1:50\n",
" s += x\n",
"end\n",
"println(sqrt(s))\n",
"\n",
"# sumを使う\n",
"s = sum(1:1:50) \n",
"println(sqrt(s))\n",
"\n",
"# generator式を使う\n",
"s = sum(x for x = 1:1:50) \n",
"print(sqrt(s))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## ライブラリ"
]
},
{
"cell_type": "code",
"execution_count": 108,
"metadata": {},
"outputs": [],
"source": [
"using LinearAlgebra"
]
},
{
"cell_type": "code",
"execution_count": 109,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2×2 Matrix{Int64}:\n",
" 1 2\n",
" -3 4"
]
},
"execution_count": 109,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A = [ 1 2 ; -3 4 ]"
]
},
{
"cell_type": "code",
"execution_count": 110,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2×2 adjoint(::Matrix{Int64}) with eltype Int64:\n",
" 1 -3\n",
" 2 4"
]
},
"execution_count": 110,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A'"
]
},
{
"cell_type": "code",
"execution_count": 111,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2×2 Matrix{Float64}:\n",
" 0.4 -0.2\n",
" 0.3 0.1"
]
},
"execution_count": 111,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"inv(A)"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"UniformScaling{Bool}\n",
"true*I"
]
},
"execution_count": 112,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"I"
]
},
{
"cell_type": "code",
"execution_count": 113,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2×2 Matrix{Int64}:\n",
" 1 2\n",
" -3 4"
]
},
"execution_count": 113,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A*I"
]
},
{
"cell_type": "code",
"execution_count": 114,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2×2 Matrix{Float64}:\n",
" 1.0 2.77556e-17\n",
" 0.0 1.0"
]
},
"execution_count": 114,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A*inv(A)"
]
},
{
"cell_type": "code",
"execution_count": 115,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2×2 Matrix{Float64}:\n",
" 1.0 -0.0\n",
" 0.0 1.0"
]
},
"execution_count": 115,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"A/A"
]
},
{
"cell_type": "code",
"execution_count": 116,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2×2 Diagonal{Int64, Vector{Int64}}:\n",
" 1 ⋅\n",
" ⋅ 4"
]
},
"execution_count": 116,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"Diagonal(A)"
]
},
{
"cell_type": "code",
"execution_count": 117,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"10.0"
]
},
"execution_count": 117,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"det(A)"
]
},
{
"cell_type": "code",
"execution_count": 118,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2"
]
},
"execution_count": 118,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"rank(A)"
]
},
{
"cell_type": "code",
"execution_count": 119,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2×1 adjoint(::Matrix{Int64}) with eltype Int64:\n",
" 1\n",
" 2"
]
},
"execution_count": 119,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = [1 2]'"
]
},
{
"cell_type": "code",
"execution_count": 120,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.23606797749979"
]
},
"execution_count": 120,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"norm(x)"
]
},
{
"cell_type": "code",
"execution_count": 121,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"2.0"
]
},
"execution_count": 121,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"norm(x, Inf)"
]
},
{
"cell_type": "code",
"execution_count": 122,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"3.0"
]
},
"execution_count": 122,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"norm(x, 1)"
]
},
{
"cell_type": "code",
"execution_count": 123,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"ComplexF64[2.5 - 1.9364916731037083im, 2.5 + 1.9364916731037083im]\n",
"ComplexF64[0.38729833462074165 + 0.5im 0.38729833462074165 - 0.5im; 0.7745966692414835 - 0.0im 0.7745966692414835 + 0.0im]\n"
]
}
],
"source": [
"w, v = eigen(A)\n",
"println(w)\n",
"println(v)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Plots"
]
},
{
"cell_type": "code",
"execution_count": 124,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Plots.GRBackend()"
]
},
"execution_count": 124,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using Plots\n",
"gr()"
]
},
{
"cell_type": "code",
"execution_count": 125,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 125,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 0 : 0.1 : 4π\n",
"y = sin.(x)\n",
"\n",
"plot(x, y,\n",
" xlabel=\"x\", #X軸のラベル\n",
" ylabel=\"y\", #Y軸のラベル\n",
" lc=:red, #線の色\n",
" lw=2, #線幅\n",
" ls=:solid, #線種\n",
" legend=false,\n",
" xticks=0:2:12,\n",
" yticks=-1.0:0.25:1.0,\n",
" size=(300,230) #プロットのサイズ \n",
" \n",
")"
]
},
{
"cell_type": "code",
"execution_count": 126,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 126,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = 0:0.1:4π\n",
"y = sin.(x)\n",
"z = cos.(x)\n",
"w = y + z\n",
"\n",
"p1 = plot(x, y,\n",
" xlabel=\"x\", #X軸のラベル\n",
" ylabel=\"y\", #Y軸のラベル\n",
" lw=2, #線幅\n",
" ls=:solid, #線種\n",
" label=\"sin\",\n",
" legend=false,\n",
" xticks=0:2:12,\n",
" yticks=-1.0:0.25:1.0,\n",
")\n",
"\n",
"p1 = plot!(x, z,\n",
" xlabel=\"x\", #X軸のラベル\n",
" ylabel=\"y\", #Y軸のラベル\n",
" lw=2, #線幅\n",
" ls=:dash, #線種\n",
" label=\"cos\",\n",
" legend=true,\n",
" xticks=0:2:12,\n",
" yticks=-1.0:0.25:1.0,\n",
")\n",
"\n",
"p2 = plot(x, w,\n",
" xlabel=\"x\", #X軸のラベル\n",
" ylabel=\"y\", #Y軸のラベル\n",
" lw=2, #線幅\n",
" ls=:dot, #線種\n",
" lc=:black,\n",
" legend=false,\n",
" xticks=0:2:12,\n",
" yticks=-1.0:0.25:1.0,\n",
")\n",
"\n",
"plot(p1, p2, layout=(2,1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### DifferentialEquations"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Plots.GRBackend()"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"using DifferentialEquations\n",
"using Plots; gr()"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"function system(y, p, t)\n",
" if t < 10.0\n",
" u = 0.0\n",
" else\n",
" u = 1.0\n",
" end\n",
" \n",
" dydt = (-y + u)/5.0 \n",
" return dydt\n",
"end\n",
"\n",
"y0 = 0.5\n",
"tspan = (0.0, 40.0)\n",
"\n",
"prob = ODEProblem(system,y0,tspan)\n",
"sol = solve(prob, DP5()) \n",
"\n",
"plot(sol)\n",
"\n",
"plot(sol,\n",
" xlabel=\"t\", #X軸のラベル\n",
" ylabel=\"u, y\", #Y軸のラベル\n",
" lw=2, #線幅\n",
" ls=:solid, #線種\n",
" legend=false,\n",
" size=(300,230) #プロットのサイズ \n",
")\n",
"\n",
"t = 0:0.01:40;\n",
"u = 1*(t.>10);\n",
"plot!(t, u, lc=:red)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.7.0",
"language": "julia",
"name": "julia-1.7"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.7.0"
},
"toc-autonumbering": false,
"toc-showcode": false,
"toc-showmarkdowntxt": false,
"toc-showtags": false
},
"nbformat": 4,
"nbformat_minor": 4
}