Loading required package: JuliaCall
Julia version 1.9.2 at location /home/apua/julia-1.9.2/bin will be used.
Loading setup script for JuliaCall...
Finish loading setup script for JuliaCall.
Loading required package: JuliaCall
Julia version 1.9.2 at location /home/apua/julia-1.9.2/bin will be used.
Loading setup script for JuliaCall...
Finish loading setup script for JuliaCall.
This exercise is about the IVP \(u'=\cos(t) - 200(u-\sin(t))\), \(u(0)=0\).
FNC.ab4
to solve the IVP over \(t\in[0,\pi/2]\) with \(n=800,850,900,\ldots,1200\) steps. By comparing to the exact solution, show that the method gets either no accurate digits or at least 11 accurate digits.= ODEProblem((u,p,t)-> cos(t)-200*(u-sin(t)),0.,(0.,pi/2));
ivp = 800:50:1200;
n = [];
err for n in n
= FNC.ab4(ivp,n);
t,u push!( err, norm(sin.(t)-u,Inf) );
end
pretty_table([n err],["n","inf-norm error"])
The equation \(u'=u^2-u^3\) is a simple model for combustion of a flame ball in microgravity. After “ignition,” the exact solution rapidly approaches 1.
FNC.rk4
with \(n=2000\) steps. Plot the solution.# (a)
= ODEProblem((u,p,t)-> u^2-u^3,0.001,(0.,2000.));
ivp = FNC.rk4(ivp,2000);
t, u plot(u)
# (c)
= 200:100:1000
n = []
err for n in n
= FNC.rk4(ivp,n)
t,u push!( err, u[n]-1 )
end
pretty_table([n err],["n","error at final time"])
The van der Pol equation is a famous nonlinear oscillator given by \[y'' - \mu(1-y^2)y' + y = 0,\] where \(\mu\ge 1\) is a constant.
solve
with \(\mu=500\), \(y(0) = y'(0) = 1\), for \(0\le t \le 2000\). Plot \(y(t)\) as a function of \(t\).function ode(u,p,t)
= [
f 2],
u[500*(1-u[1]^2*u[2])-u[1]
]return f
end
= ODEProblem(ode,[1., 1.],(0.,2000.));
ivp =solve(ivp);
solplot(sol)
= sol.t[1:2:end],sol.u[1:2:end];
t,u = fill(0.0im,length(t),2);
λ for (k,u) in enumerate(u)
= [0 1; -2*u[1]*500*u[2]-1 500*(1-u[1]^2)];
J :] .= eigvals(J);
λ[k,end
plot(t, minimum(real(λ), dims=2))
function parabolicHN(ϕ,xspan,m,tspan,init)
= FNC.diffcheb(m,xspan) # include FNC prefix here
x,Dₓ,Dₓₓ = 2:m # indexes of interior nodes
int function extend(v)
# linear system of equations
= -[-3/2 0 ; 0 3/2]\([2 -1/2 zeros(Float64, 1, m-3); zeros(Float64, 1, m-3) 1/2 -2]*v)
ubc return [ubc[1];v;ubc[2]]
end
function ode!(f,v,p,t)
= extend(v)
u = Dₓ*u,Dₓₓ*u
uₓ,uₓₓ = ϕ(t,x[int],u[int],uₓ[int],uₓₓ[int])
@. f end
= ODEProblem(ode!,init.(x[int]),float.(tspan))
ivp = solve(ivp)
u return x,t->extend(u(t))
end
function parabolicD(ϕ,xspan,m,alpha,beta,tspan,init)
= FNC.diffcheb(m,xspan) # include FNC prefix
x,Dₓ,Dₓₓ = 2:m # indexes of interior nodes
int function extend(v)
return [alpha;v;beta]
end
function ode!(f,v,p,t)
= extend(v)
u = Dₓ*u,Dₓₓ*u
uₓ,uₓₓ = ϕ(t,x[int],u[int],uₓ[int],uₓₓ[int])
@. f end
= ODEProblem(ode!,init.(x[int]),float.(tspan))
ivp = solve(ivp)
u return x,t->extend(u(t))
end
Use FNC.parabolic
to solve the heat equation for \(0\le x \le 5\) with initial condition \(u(x,0)=x(5-x)\) and subject to the boundary conditions \(u(0,t)=0\), \(u(5,t)-u_x(5,t)=5\). Plot the solution at \(t=1\) and find the value of \(u(2.5,1)\).
= (t,x,u,uₓ,uₓₓ) -> uₓₓ;
ϕ = x -> x*(5-x);
init = (u,uₓ) -> u;
g₁ = (u,uₓ) -> u-uₓ-5;
g₂ = FNC.parabolic(ϕ,(0,5),60,g₁,g₂,(0,1),init)
x,u plot(x,u(1), xaxis=(L"x"), yaxis=(L"u(x,1)",(0,10)))
# Code directly copied from Demo 11.5.6
= (t,x,u,uₓ,uₓₓ) -> u^2 + uₓₓ
ϕ = (u,uₓ) -> u
g₁ = (u,uₓ) -> uₓ
g₂ = x -> 400x^4*(1-x)^2
init # Modification next line
= FNC.parabolic(ϕ,(0,1),60,g₁,g₂,(0,1),init);
x,u = @animate for t in range(0,1,length=101)
anim plot(x,u(t), xaxis=(L"x"), yaxis=(L"u(x,t)",(0,10)),dpi=100,
="Heat equation with source",leg=:topleft)
titleend
# change FPS
mp4(anim,"boundaries-source.mp4",fps=10)
= (t,x,u,uₓ,uₓₓ) -> uₓₓ
ϕ = x -> 1 + sin(π*x/2) + 3*(1-x^2)*exp(-4x^2);
init = parabolicD(ϕ,(-1,1),60,0,2,(0,0.75),init)
x,u = plot(xlabel=L"x",ylabel=L"u(x,t)",legend=:topleft,
plt ="Solution of the heat equation")
titlefor t in 0:0.1:0.4
plot!(x,u(t),label=@sprintf("t=%.2f",t))
end
plt
= (t,x,u,uₓ,uₓₓ) -> uₓₓ
ϕ = x -> x^2*(4-x)^4;
init = parabolicHN(ϕ,(0,4),60,(0,1),init)
x,u = plot(xlabel=L"x",ylabel=L"u(x,t)",legend=:topleft,
plt ="Solution of the heat equation")
titlefor t in 0:0.05:1
plot!(x,u(t),label=@sprintf("t=%.2f",t))
end
plt