As indicated in the subject, would some body please indicate the problem in my coding.
What am I missing here?
my code:
""" This solves:
y''(x) + 4y(x) = 0
y(0) = -2, y(Pi/4) = 10
The Exact solution to this problem is:
y(x) = -2cos(2x) + 10sin(2x))
"""
from dolfin import *
import numpy
a = pi/4.0
mesh = IntervalMesh(60, 0, a)
V = FunctionSpace(mesh, 'CG', 4)
# For left boundary, meaning for y(0) = - 2
ul = Expression("-2- x[0]")
def left_boundary(x, on_boundary):
tol = 1e-14
return on_boundary and abs(x[0])<tol
bc1 = DirichletBC(V, ul, left_boundary)
# For right boundary, meaning for y(pi/4) = 10
ur = Expression("10 - x[0] ")
def right_boundary(x, on_boundary):
tol = 1e-14
return on_boundary and abs(x[0] - pi/4) < tol
bc2 = DirichletBC(V, ur, right_boundary)
# combing bc1 and bc2 in the form of list.
bc = [bc1, bc2]
u = TrialFunction(V)
v = TestFunction(V)
f = Constant(0.0)
a = inner(grad(u), grad(v))*dx + 4.0*u*v*dx
L = f*v*dx
u = Function(V)
solve( a == L, u, bc)
#plot(u, interactive = True)
# Examing the discrete solution
u_nodal_values = u.vector() # the result is a dolfin vector, having all nodal values.
u_array = u_nodal_values.array() # converting the dolfin vector to python array for further processing.
# printing the solution at nodes, meaning u(x_i)
coor = mesh.coordinates()
# comparing the finite element solution with the exact solution.
# finding the maximum error.
# First defining the exact solution.
ue = Expression("-2*cos(2*x[0]) + 10*sin(2.0*x[0])")
u_e = interpolate(ue, V)
u_e_array = u_e.vector().array()
print "Max. error:", numpy.abs(u_e_array - u_array).max()
# now printing the approxmiate solution, the exact solution and error side by side.
#if mesh.num_vertices() == len(u_array):
# for i in range(mesh.num_vertices()):
# print 'u(%8g)=%g \t %g \t %8g' %(u(coor[i][0]), u_array[i], u_e_array[i], u_e_array[i]-u_array[i])