# コメントです
print(1) # 1を表示
1
#===
複数行のコメント
こめんと
こめんと
===#
x = 1
1
x = 1; # セミコロンをつけると結果が表示されない
print(x)
1
print("Control")
Control
print("制御") # 改行なし
print("工学")
制御工学
println("制御") # 改行する場合
print("工学")
制御 工学
print("制御\n工学")
制御 工学
print("制御", "工学")
制御工学
x = 10
println(x)
println("x = ", x)
println("x = $x")
10 x = 10 x = 10
変数にUnicodeが使える
制御 = 2
3制御
6
LaTeXコードが使える
π #\pi
π = 3.1415926535897...
3π
9.42477796076938
ℯ #\euler
ℯ = 2.7182818284590...
α = 1 # \alpha
1
P₀ = 1 # P\_0
1
Pₘ = 10
10
G⁰ = 1 # G\^0
1
length("せいぎょこうがく")
8
ℯ #\euler
ℯ = 2.7182818284590...
α = 1 # \alpha
1
P₀ = 1 # P\_0
1
G⁰ = 1 # G\^0
1
typeof(1)
Int64
typeof(1.0)
Float64
2 * 1.0
2.0
10 / 5
2.0
17 ÷ 5 # \div
3
17 % 5
2
15 // 4
15//4
x = 1
x += 1
# x = x + 1
2
x = 2
x *= 3
6
3^5
243
3*10^-2
0.030000000000000006
3e-2
0.03
3e2
300.0
演算子の省略
x = 1
2x
2
2(x+2)
6
x = 1
println(x)
println(typeof(x))
1 Int64
y = 1.0
println(y)
println(typeof(y))
1.0 Float64
z = 1 + 2im
println(z)
println(typeof(z))
1 + 2im Complex{Int64}
msg = "Control"
println(msg)
println(typeof(msg))
Control String
chr = 'C'
println(chr)
println(typeof(chr))
C Char
ok = true
println(ok)
println(typeof(ok))
true Bool
# nothing
typeof(nothing)
Nothing
# missing
typeof(missing)
Missing
z = convert(Int, y)
println(z)
println(typeof(z))
1 Int64
word = string(x)
println(word)
println(typeof(word))
1 String
x = 1
2x
2
2(x+2)
6
data1 = [3, 5, 2, 4, 6, 1]
6-element Vector{Int64}: 3 5 2 4 6 1
println(typeof(data1))
println(eltype(data1))
Vector{Int64} Int64
data2 = [ [3, 5, 2], [4, 6, 1] ]
data2
2-element Vector{Vector{Int64}}: [3, 5, 2] [4, 6, 1]
data1[1]
3
data1[1:2]
2-element Vector{Int64}: 3 5
data1[3:4]
2-element Vector{Int64}: 2 4
data1[3:end]
4-element Vector{Int64}: 2 4 6 1
data2 = [3, 5, 2, 4, 6, 1]
# 先頭に要素を追加
pushfirst!(data2, 1)
7-element Vector{Int64}: 1 3 5 2 4 6 1
# 先頭の要素を取り出す
popfirst!(data2)
1
# 末尾に要素を追加
push!(data2, 10)
7-element Vector{Int64}: 3 5 2 4 6 1 10
# 末尾の要素を取り出す
pop!(data2)
10
# i番目に要素を挿入する
insert!(data2, 2, 20)
7-element Vector{Int64}: 3 20 5 2 4 6 1
# i番目の要素を削除
deleteat!(data2, 2)
6-element Vector{Int64}: 3 5 2 4 6 1
tuple = (1,2,3,4)
println(tuple)
println(typeof(tuple))
(1, 2, 3, 4) NTuple{4, Int64}
# 書き換え不可
tuple[1] = 5
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
data = [1, 2, 3, 4]
data[1] = 5 #リストは書き換えができる
data
4-element Vector{Int64}: 5 2 3 4
d = ( linestyle = "-.", color="k")
println(d)
println(typeof(d))
(linestyle = "-.", color = "k") NamedTuple{(:linestyle, :color), Tuple{String, String}}
d.linestyle
"-."
d.color
"k"
dic = Dict();
dic["linestyle"] = "-.";
dic["color"] = "k";
dic["color"]
"k"
dic2 = Dict( "linestyle" => "-.", "color" => "k" );
dic2["color"]
"k"
x = 1
if x < 0
print("x is negative")
elseif x == 0
print("x is zero")
else
print("x is positive")
end
x is positive
x ,y = -1, -1
if x < 0 && y < 0
print("x and y are negative")
end
x and y are negative
x, y = 1, -1
if x < 0 || y < 0
print("x or y is negative")
end
x or y is negative
x = 2
y = [1, 2, 3]
if x in y
println("x is in y")
end
if x ∈ y #\in
println("x is in y")
end
x is in y x is in y
x = 0
y = [1, 2, 3]
if x ∉ y #\notin
print("x is not in y")
end
x is not in y
条件 a が真(true)なら b を、偽なら c を値とする
x = 1
x > 2 ? "x>2" : "x≤2"
"x≤2"
for x in [0, 1, 2]
println(x)
end
0 1 2
for x=0:1:2
println(x)
end
0 1 2
for (i, word) in enumerate(["a", "b", "c"])
println(i, ":", word)
end
1:a 2:b 3:c
# プログラム1
for x=0:1:2
println(x)
println("julia")
end
0 julia 1 julia 2 julia
# プログラム2
for x=0:1:2
println(x)
continue
println("julia")
end
0 1 2
function say_hello()
println("こんにちは")
end
say_hello (generic function with 1 method)
say_hello()
こんにちは
function subject(name)
println(name * "工学")
end
subject (generic function with 1 method)
subject("制御")
制御工学
function add(a, b)
c = a + b
return c
end
add (generic function with 1 method)
result = add(3, 5)
print(result)
8
add2(a, b) = a + b
add2 (generic function with 1 method)
add2(3, 5)
8
# 引数の型で関数の中身を変えることができる
test(x::Int64) = "せいすう"
test(x::Float64) = "しょうすう"
test (generic function with 2 methods)
test(2)
"せいすう"
test(2.0)
"しょうすう"
function outer(a, b)
function inner(c)
return c * (a + b)
end
return inner
end
outer (generic function with 1 method)
f = outer(1, 2)
r = f(3)
println(r)
9
f2 = outer(3, 4)
r2 = f2(3)
println(r2)
21
c = (a, b) -> 2*a + 3*b
c(1, 4)
14
map(x -> x^2 + 2x + 1, [0, 1 ,-1])
3-element Vector{Int64}: 1 4 0
data1 = [1, 2, 3, 4, 5]
data2 = [10, 9, 8, 7, 6]
result = map( (a, b) -> 2*a + 3*b, data1, data2)
println(result)
[32, 31, 30, 29, 28]
function linestyle_generator(lineID)
linestyle = ["-", "--", "-.", ":"]
lineID = (lineID-1) % (length(linestyle)) + 1
return linestyle[lineID]
end
linestyle_generator (generic function with 1 method)
LS = (linestyle_generator(i) for i=1:1:5)
for i in LS
println(i)
end
- -- -. : -
t = (1, 2, 3, 4, 5)
r1 = [i for i in t]
print(r1)
[1, 2, 3, 4, 5]
r2 = [i for i in t if i % 2 == 0]
print(r2)
[2, 4]
# for文を使って足し合わせる
s=0
for x = 1:1:50
s += x
end
println(sqrt(s))
# sumを使う
s = sum(1:1:50)
println(sqrt(s))
# generator式を使う
s = sum(x for x = 1:1:50)
print(sqrt(s))
35.70714214271425 35.70714214271425 35.70714214271425
using LinearAlgebra
A = [ 1 2 ; -3 4 ]
2×2 Matrix{Int64}: 1 2 -3 4
A'
2×2 adjoint(::Matrix{Int64}) with eltype Int64: 1 -3 2 4
inv(A)
2×2 Matrix{Float64}: 0.4 -0.2 0.3 0.1
I
UniformScaling{Bool} true*I
A*I
2×2 Matrix{Int64}: 1 2 -3 4
A*inv(A)
2×2 Matrix{Float64}: 1.0 2.77556e-17 0.0 1.0
A/A
2×2 Matrix{Float64}: 1.0 -0.0 0.0 1.0
Diagonal(A)
2×2 Diagonal{Int64, Vector{Int64}}: 1 ⋅ ⋅ 4
det(A)
10.0
rank(A)
2
x = [1 2]'
2×1 adjoint(::Matrix{Int64}) with eltype Int64: 1 2
norm(x)
2.23606797749979
norm(x, Inf)
2.0
norm(x, 1)
3.0
w, v = eigen(A)
println(w)
println(v)
ComplexF64[2.5 - 1.9364916731037083im, 2.5 + 1.9364916731037083im] ComplexF64[0.38729833462074165 + 0.5im 0.38729833462074165 - 0.5im; 0.7745966692414835 - 0.0im 0.7745966692414835 + 0.0im]
using Plots
gr()
Plots.GRBackend()
x = 0 : 0.1 : 4π
y = sin.(x)
plot(x, y,
xlabel="x", #X軸のラベル
ylabel="y", #Y軸のラベル
lc=:red, #線の色
lw=2, #線幅
ls=:solid, #線種
legend=false,
xticks=0:2:12,
yticks=-1.0:0.25:1.0,
size=(300,230) #プロットのサイズ
)
x = 0:0.1:4π
y = sin.(x)
z = cos.(x)
w = y + z
p1 = plot(x, y,
xlabel="x", #X軸のラベル
ylabel="y", #Y軸のラベル
lw=2, #線幅
ls=:solid, #線種
label="sin",
legend=false,
xticks=0:2:12,
yticks=-1.0:0.25:1.0,
)
p1 = plot!(x, z,
xlabel="x", #X軸のラベル
ylabel="y", #Y軸のラベル
lw=2, #線幅
ls=:dash, #線種
label="cos",
legend=true,
xticks=0:2:12,
yticks=-1.0:0.25:1.0,
)
p2 = plot(x, w,
xlabel="x", #X軸のラベル
ylabel="y", #Y軸のラベル
lw=2, #線幅
ls=:dot, #線種
lc=:black,
legend=false,
xticks=0:2:12,
yticks=-1.0:0.25:1.0,
)
plot(p1, p2, layout=(2,1))
using DifferentialEquations
using Plots; gr()
Plots.GRBackend()
function system(y, p, t)
if t < 10.0
u = 0.0
else
u = 1.0
end
dydt = (-y + u)/5.0
return dydt
end
y0 = 0.5
tspan = (0.0, 40.0)
prob = ODEProblem(system,y0,tspan)
sol = solve(prob, DP5())
plot(sol)
plot(sol,
xlabel="t", #X軸のラベル
ylabel="u, y", #Y軸のラベル
lw=2, #線幅
ls=:solid, #線種
legend=false,
size=(300,230) #プロットのサイズ
)
t = 0:0.01:40;
u = 1*(t.>10);
plot!(t, u, lc=:red)