{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# p13: Solve linear BVP \n",
"\n",
"$$\n",
"u_{xx} = \\exp(4x), \\qquad u(-1)=u(1)=0\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
"%config InlineBackend.figure_format='svg'\n",
"from chebPy import *\n",
"from numpy import dot,exp,zeros,sinh,cosh,max,linspace,inf\n",
"from numpy.linalg import norm\n",
"from scipy.linalg import solve\n",
"from scipy.interpolate import barycentric_interpolate\n",
"from matplotlib.pyplot import title,plot,legend,xlabel"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"N = 16\n",
"D,x = cheb(N)\n",
"D2 = dot(D,D)\n",
"D2 = D2[1:N,1:N]\n",
"f = exp(4.0*x[1:N])\n",
"u = solve(D2,f)\n",
"s = zeros(N+1)\n",
"s[1:N] = u\n",
"\n",
"# Do Chebyshev interpolation to finer grid for plotting and error estimation\n",
"xx = linspace(-1.0,1.0,200)\n",
"uu = barycentric_interpolate(x,s,xx)\n",
"exact = (exp(4.0*xx) - sinh(4.0)*xx - cosh(4.0))/16.0\n",
"maxerr = norm(uu-exact,inf)\n",
"\n",
"title('max err = %e' % maxerr)\n",
"plot(x,s,'o',label='Chebyshev')\n",
"plot(xx,exact,label='Exact')\n",
"legend(), xlabel('x');"
]
}
],
"metadata": {
"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
}