Problem with ploting the solution

the équation is given by : \

-\epsilon u^{''}+ \lambda u^{'} = f \

the boundaries conditions are:\

u(1) = u(0) = 0 \
u is define in (0,1)
we have to find a numérical with finite élément P1 and P2
for P1 i have made this code :
‘’’ from future import print_function
import matplotlib.pyplot as plt
from fenics import *
import matplotlib.pyplot as plt
from dolfin import *
import numpy as np

from mshr import *

Parameters of the problem

nx = 10
epsilon = 0.1
lamda = 1.0
f = 1.0

Create mesh and define function space

mesh = UnitIntervalMesh(nx)

Q = FunctionSpace(mesh,“P”,1)

Define variational problem

plt.figure(figsize=(10, 5))
plot(mesh, title=“Maillage pour n = 10”)

Define the boundaries

u = TrialFunction(Q)

v = TestFunction(Q)

bord1 = ‘near(x[0], 0)’
bord2 = ‘near(x[0], 0)’

define boundary conditions

bcu_bord1 = DirichletBC(Q, Constant((0)), bord1)
bcu_bord2 = DirichletBC(Q, Constant((0)), bord2)
a = epsilon*inner(grad(u), grad(v))dx + lamdainner(u.dx(0), v)*dx
L = dot(f,v)*dx

solve the problem

bcu = [bcu_bord1, bcu_bord2]
w = Function(Q)
solve(a == L, w, bcu)

I want to plot the numérical solution of u but it doesnt work

You need to be more specific. What ways have you tried to plot the solution? Using the plot command and matplotlib.plyplot.show() or writing the data to pvd/XDMF using Paraview to visualize?

Do you get an error message?

Use triple quotes to make teh code readable. This seems to plot fine:

from dolfin import *

# Parameters of the problem
nx = 10
epsilon = 0.1
lamda = 1.0
f = 1.0

# Create mesh and define function space
mesh = UnitIntervalMesh(nx)

Q = FunctionSpace(mesh,"P",1)

# Define the boundaries
u = TrialFunction(Q)
v = TestFunction(Q)

bord1 = 'near(x[0], 0)'
bord2 = 'near(x[0], 0)'

# define boundary conditions
bcu_bord1 = DirichletBC(Q, Constant((0)), bord1)
bcu_bord2 = DirichletBC(Q, Constant((0)), bord2)
a = epsilon*inner(grad(u), grad(v))*dx + lamda*inner(u.dx(0), v)*dx
L = dot(f,v)*dx

# solve the problem
bcu = [bcu_bord1, bcu_bord2]
w = Function(Q)
solve(a == L, w, bcu)

import matplotlib.pyplot as plt
plt.figure(figsize=(10, 5))
plot(mesh, title="Maillage pour n=10")
plot(w)
plt.show()
# from vedo.dolfin import plot
# plot(w, xtitle="Maillage pour n=10", warpYfactor=0.8, lw=3, scalarbar=0)
2 Likes

Make sure to change the boundary condition

to this

bord2 = 'near(x[0], 1)'

as I pointed out in the original post