# Random mátrix
A = rand(1:4,3,3)
# Egyesekkel teli vektor
x = fill(1.0, (3)) # vagy `ones(3)`
b = A*x # Szorzás
# Konjugált transzponált
A'
# Transzponált
transpose(A)
# Lineáris egyenletrendszerek megoldása
A\b
using LinearAlgebra
# Random mátrix
A = rand(3,3)
eigmax(A)
eigen(A)
qr(A)
lu(A)
using LinearAlgebra
n = 1000
A = randn(n,n);
# Szimmetria ellenőrzése
Asym = A + A'
issymmetric(Asym)
# Numerikus zajjal terhelt mátrix
Asym_noisy = copy(Asym)
Asym_noisy[1,2] += 5eps()
issymmetric(Asym_noisy)
# Szimmetrikus mátrixok direkt definiálása
Asym_explicit = Symmetric(Asym_noisy);
@time eigvals(Asym);
@time eigvals(Asym_noisy);
@time eigvals(Asym_explicit);
n = 1_000_000;
A = SymTridiagonal(randn(n), randn(n-1));
@time eigmax(A)
A
Differentialequations.jl - Beállítások, és részletek a megoldókról általában
using DifferentialEquations
using Plots
# Probléma definiálása (itt most Közönséges DE kezdeti érték problémája)
f(u,p,t) = 1.01*u
u0=1/2
tspan = (0.0,1.0)
prob = ODEProblem(f,u0,tspan)
# Megoldása
sol = solve(prob,Tsit5(),reltol=1e-8,abstol=1e-8)
# Ábrázolása
plot(sol,linewidth=5,title="Solution to the linear ODE with a thick line",
xaxis="Time (t)",yaxis="u(t) (in μm)",label="My Thick Line!") # legend=false
plot!(sol.t, t->0.5*exp(1.01t),lw=3,ls=:dash,label="True Solution!")
Lorentz egyenletek
\begin{align} \frac{dx}{dt} &= σ(y-x) \\ \frac{dy}{dt} &= x(ρ-z) - y \\ \frac{dz}{dt} &= xy - βz \\ \end{align}u0 = [1.0,0.0,0.0]
tspan = (0.0,1.0)
p = [10.0,28.0,8/3]
prob = ODEProblem(parameterized_lorenz,u0,tspan,p)
function lorenz(du,u,p,t)
du[1] = p[1]*(u[2]-u[1])
du[2] = u[1]*(p[2]-u[3]) - u[2]
du[3] = u[1]*u[2] - (p[3])*u[3]
end
u0 = [1.0;0.0;0.0]
tspan = (0.0,100.0)
p=(15.,28.,8. /3.)
prob = ODEProblem(lorenz,u0,tspan,p)
@time sol = solve(prob);
plot(sol,vars=(1,2,3))
plot(sol,vars=(0,2))
using NLsolve
# Probléma
function f!(F, x)
F[1] = (x[1]+3)*(x[2]^3-7)+18
F[2] = sin(x[2]*exp(x[1])-1)
end
# Probléma Jacobi mátrixa
function j!(J, x)
J[1, 1] = x[2]^3-7
J[1, 2] = 3*x[2]^2*(x[1]+3)
u = exp(x[1])*cos(x[2]*exp(x[1])-1)
J[2, 1] = x[2]*u
J[2, 2] = u
end
# Kezdeti megoldásvektor
init=[ 0.1; 1.2];
solu=nlsolve(f!, j!, init)
using IntervalArithmetic, IntervalRootFinding
using StaticArrays
g(x) = (x^2-2)^2 * (x^2 - 3)
rts=roots(g, -10..10)
function g(x)
(x1, x2) = x
SVector((x1+3)*(x2^3-7)+18,
sin(x2*exp(x1)-1))
end
interval=-2..2
@time rts = roots(g, interval × interval) # × = \times
https://github.com/bachrathyd/MDBM.jl
Megjegyzés: Ez a módszer inkább ponthalmazok megtalálására alkalmas, pl stabilitási határok
using MDBM
function foo(x1,x2)
SVector((x1+3)*(x2^3-7)+18,
sin(x2*exp(x1)-1))
end
ax1=Axis(-2.0:0.1:2.0) # initial grid in x1 direction
ax2=Axis(-2.0:0.1:2.0) # initial grid in x2 direction
mymdbm=MDBM_Problem(foo,[ax1,ax2])
iteration=3 #number of refinements (resolution doubling)
MDBM.solve!(mymdbm,iteration)
getinterpolatedsolution(mymdbm)
scatter(getinterpolatedsolution(mymdbm)...,size=(300,200),label="")
using SemiDiscretizationMethod
using MDBM
using Plots
using LaTeXStrings
Elsőrendű formára átírva:
$$\dot{\mathbf{x}}(t) = \mathbf{A}(t) \mathbf{x}(t) + \sum_{j=1}^g \mathbf{B}(t) \mathbf{x}(t-\tau_j(t))+\mathbf{c}(t)$$
$$ \mathbf{x}(t) = \begin{bmatrix}x(t) \\ \dot{x}(t)\end{bmatrix}, \quad
\mathbf{A}(t) = \begin{bmatrix} 0 & 1 \\ -\delta - \varepsilon \cos(t) & -a_1 \end{bmatrix},
\quad \mathbf{B}_1(t) = \begin{bmatrix}0 & 0 \\ b_0 & 0\end{bmatrix},
\quad \tau_1(t) \equiv 2\pi,
\quad \text{and} \quad \mathbf{c}(t) = \begin{bmatrix} 0 \\ \sin(2t) \end{bmatrix}.$$
function createMathieuProblem(δ,ε,b0,a1;T=2π)
AMx = ProportionalMX(t->@SMatrix [0. 1.; -δ-ε*cos(2π/T*t) -a1]);
τ1=2π # if function is needed, the use τ1 = t->foo(t)
BMx1 = DelayMX(τ1,t->@SMatrix [0. 0.; b0 0.]);
cVec = Additive(t->@SVector [0.,sin(4π/T*t)])
LDDEProblem(AMx,[BMx1],cVec)
end;
τmax=2π # the largest τ of the system
P=2π #Principle period of the system (sin(t)=sin(t+P))
mathieu_lddep=createMathieuProblem(3.,2.,-0.15,0.1,T=P); # LDDE problem for Hayes equation
method=SemiDiscretization(1,0.01) # 3rd order semi discretization with Δt=0.1
# if P = τmax, then n_steps is automatically calculated
mapping=calculateMapping(mathieu_lddep,method,τmax,
n_steps=Int((P+100eps(P))÷method.Δt),calculate_additive=true); #The discrete mapping of the system
a1=0.1;
ε=1;
τmax=2π;
T=1π;
method=SemiDiscretization(2,T/40);
foo(δ,b0) = log(spectralRadiusOfMapping(calculateMapping(createMathieuProblem(δ,ε,b0,a1,T=T),method,τmax,
n_steps=Int((T+100eps(T))÷method.Δt)))); # No additive term calculated
axis=[Axis(-1:0.2:5.,:δ),
Axis(-2:0.2:1.5,:b0)];
iteration=3;
@time stab_border_points=getinterpolatedsolution(MDBM.solve!(MDBM_Problem(foo,axis),iteration));
scatter(stab_border_points...,xlim=(-1.,5),ylim=(-2.,1.5),
label="",title="Stability border of the delay Mathieu equation",xlabel=L"\delta",ylabel=L"b_0",
guidefontsize=14,tickfont = font(10),markersize=2,markerstrokewidth=0)