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.ivp = ODEProblem((u,p,t)-> cos(t)-200*(u-sin(t)),0.,(0.,pi/2));
n = 800:50:1200;
err = [];
for n in n
t,u = FNC.ab4(ivp,n);
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)
ivp = ODEProblem((u,p,t)-> u^2-u^3,0.001,(0.,2000.));
t, u = FNC.rk4(ivp,2000);
plot(u)
# (c)
n = 200:100:1000
err = []
for n in n
t,u = FNC.rk4(ivp,n)
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 = [
u[2],
500*(1-u[1]^2*u[2])-u[1]
]
return f
end
ivp = ODEProblem(ode,[1., 1.],(0.,2000.));
sol=solve(ivp);
plot(sol)
t,u = sol.t[1:2:end],sol.u[1:2:end];
λ = fill(0.0im,length(t),2);
for (k,u) in enumerate(u)
J = [0 1; -2*u[1]*500*u[2]-1 500*(1-u[1]^2)];
λ[k,:] .= eigvals(J);
end
plot(t, minimum(real(λ), dims=2))function parabolicHN(ϕ,xspan,m,tspan,init)
x,Dₓ,Dₓₓ = FNC.diffcheb(m,xspan) # include FNC prefix here
int = 2:m # indexes of interior nodes
function extend(v)
# linear system of equations
ubc = -[-3/2 0 ; 0 3/2]\([2 -1/2 zeros(Float64, 1, m-3); zeros(Float64, 1, m-3) 1/2 -2]*v)
return [ubc[1];v;ubc[2]]
end
function ode!(f,v,p,t)
u = extend(v)
uₓ,uₓₓ = Dₓ*u,Dₓₓ*u
@. f = ϕ(t,x[int],u[int],uₓ[int],uₓₓ[int])
end
ivp = ODEProblem(ode!,init.(x[int]),float.(tspan))
u = solve(ivp)
return x,t->extend(u(t))
endfunction parabolicD(ϕ,xspan,m,alpha,beta,tspan,init)
x,Dₓ,Dₓₓ = FNC.diffcheb(m,xspan) # include FNC prefix
int = 2:m # indexes of interior nodes
function extend(v)
return [alpha;v;beta]
end
function ode!(f,v,p,t)
u = extend(v)
uₓ,uₓₓ = Dₓ*u,Dₓₓ*u
@. f = ϕ(t,x[int],u[int],uₓ[int],uₓₓ[int])
end
ivp = ODEProblem(ode!,init.(x[int]),float.(tspan))
u = solve(ivp)
return x,t->extend(u(t))
endUse 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ₓₓ;
init = x -> x*(5-x);
g₁ = (u,uₓ) -> u;
g₂ = (u,uₓ) -> u-uₓ-5;
x,u = FNC.parabolic(ϕ,(0,5),60,g₁,g₂,(0,1),init)
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ₓₓ
g₁ = (u,uₓ) -> u
g₂ = (u,uₓ) -> uₓ
init = x -> 400x^4*(1-x)^2
# Modification next line
x,u = FNC.parabolic(ϕ,(0,1),60,g₁,g₂,(0,1),init);
anim = @animate for t in range(0,1,length=101)
plot(x,u(t), xaxis=(L"x"), yaxis=(L"u(x,t)",(0,10)),dpi=100,
title="Heat equation with source",leg=:topleft)
end
# change FPS
mp4(anim,"boundaries-source.mp4",fps=10)ϕ = (t,x,u,uₓ,uₓₓ) -> uₓₓ
init = x -> 1 + sin(π*x/2) + 3*(1-x^2)*exp(-4x^2);
x,u = parabolicD(ϕ,(-1,1),60,0,2,(0,0.75),init)
plt = plot(xlabel=L"x",ylabel=L"u(x,t)",legend=:topleft,
title="Solution of the heat equation")
for t in 0:0.1:0.4
plot!(x,u(t),label=@sprintf("t=%.2f",t))
end
pltϕ = (t,x,u,uₓ,uₓₓ) -> uₓₓ
init = x -> x^2*(4-x)^4;
x,u = parabolicHN(ϕ,(0,4),60,(0,1),init)
plt = plot(xlabel=L"x",ylabel=L"u(x,t)",legend=:topleft,
title="Solution of the heat equation")
for t in 0:0.05:1
plot!(x,u(t),label=@sprintf("t=%.2f",t))
end
plt