{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# p14: Solve nonlinear BVP \n",
"\n",
"$$\n",
"u_{xx} = \\exp(u), \\qquad u(-1)=u(1)=0\n",
"$$\n",
"\n",
"by Picard iteration. Set $v_{old} = 0$ and solve\n",
"\n",
"$$\n",
"\\tilde{D}_N^2 v_{new} = \\exp(v_{old})\n",
"$$\n",
"\n",
"until convergence."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"%config InlineBackend.figure_format='svg'\n",
"from numpy import dot,exp,zeros,linspace,inf\n",
"from numpy.linalg import norm\n",
"from chebPy import cheb\n",
"from scipy.linalg import solve\n",
"from scipy.interpolate import barycentric_interpolate\n",
"from matplotlib.pyplot import title,plot,xlabel,ylabel,legend"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"N = 16 # N must be even\n",
"D,x = cheb(N)\n",
"D2 = dot(D,D)\n",
"D2 = D2[1:N,1:N]\n",
"\n",
"uold = zeros(N-1)\n",
"err = zeros(N-1)\n",
"change, it = 1.0, 0\n",
"\n",
"while change > 1.0e-15:\n",
" unew = solve(D2,exp(uold))\n",
" change = norm(unew-uold, inf)\n",
" uold = unew\n",
" it += 1\n",
"\n",
"# Add bounday values to u vector\n",
"u = zeros(N+1); u[1:N] = unew\n",
"\n",
"# Chebyshev interpolation to finer grid for plotting\n",
"xx = linspace(-1.0,1.0,201)\n",
"uu = barycentric_interpolate(x,u,xx)\n",
"\n",
"title('no. steps = %d, u(0) = %18.14f' %(it,u[N//2]) )\n",
"plot(x,u,'o',label='Chebyshev')\n",
"plot(xx,uu,'b',label='Interpolant')\n",
"xlabel('x'), ylabel('y'), legend();"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.12.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}