{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# p32: Solve linear BVP\n",
"\n",
"$$\n",
"u_{xx} = \\exp(4x)\n",
"$$\n",
"\n",
"$$\n",
"u(-1)=0, \\qquad u(1)=1\n",
"$$\n",
" \n",
"Similar to p13 but with non-homogeneous bc."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"%config InlineBackend.figure_format='svg'\n",
"from chebPy import *\n",
"from numpy import dot,exp,zeros,sinh,cosh,max,linspace,polyval,polyfit,inf\n",
"from numpy.linalg import norm\n",
"from scipy.linalg import solve\n",
"from matplotlib.pyplot import title,plot,xlabel,ylabel,grid\n",
"\n",
"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",
"s = s + (x + 1.0)/2.0 # Correction for bc\n",
"\n",
"xx = linspace(-1.0,1.0,200)\n",
"uu = polyval(polyfit(x,s,N),xx) # interpolate grid data\n",
"exact = (exp(4.0*xx) - sinh(4.0)*xx - cosh(4.0))/16.0 + (xx + 1.0)/2.0\n",
"maxerr = norm(uu-exact,inf)\n",
"\n",
"title('max err = %e' % maxerr)\n",
"plot(x,s,'o',xx,exact)\n",
"xlabel('x'); ylabel('u'); grid(True);"
]
}
],
"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.8"
}
},
"nbformat": 4,
"nbformat_minor": 4
}