Facing problem in plotting u^2 vs dt

Hello everyone

I want to plot the u^2 vs dt. Can you help me in resolving this issue?

from fenics import *
import numpy as np
import matplotlib.pyplot as plt
import numpy as np
set_log_level(LogLevel.ERROR)
T = 1
nx=  4
nmax = 5
Dt = []
Tm = []
for j in range(nmax):
    num_steps = 2**(j+1)
    dt = T/num_steps
    Dt.append(dt)
    alpha = 3          # parameter alpha
    beta = 1.2
    mesh = UnitSquareMesh(nx, nx)
    V = FunctionSpace(mesh, 'P', 1)
    u_D = Expression('1 + x[0]*x[0] + alpha*x[1]*x[1] + beta*t', degree=2, alpha=alpha, beta=beta, t=0)
    def boundary(x, on_boundary):
    return on_boundary
    bc = DirichletBC(V, u_D, boundary)
    u_n = interpolate(u_D, V)
    u = TrialFunction(V)
    v = TestFunction(V)
    f = Constant(beta - 2 - 2*alpha)
    F = u*v*dx + dt*dot(grad(u), grad(v))*dx - (u_n + dt*f)*v*dx
    a, L = lhs(F), rhs(F)
    u = Function(V)
    t = 0
    for n in range(num_steps):
        t += dt
        u_D.t = t
        solve(a == L, u, bc)
        u_n.assign(u)
    T_m = dot(u,u)
    Tm.append(T_m)


plt.plot(Dt, Tm, 'o', label='Dt vs Tm', linestyle='solid') 
plt.title('Curve between Dt vs Tm and Tc')  # Title of the plot
plt.xlabel('Dt')  # Label for the x-axis
plt.ylabel('Tm and Tc')  # Label for the y-axis
plt.grid(True)  # Enable grid
plt.legend()  # Show legend with label 'X vs Y'
plt.show()  # Display the plot
   

Please read Read before posting: How do I get my question answered?. You must actually say what the issue is, otherwise people will have a harder time helping you.

My own guess of what your issue is: drop

plt.plot(Dt, Tm, 'o', label='Dt vs Tm', linestyle='solid') 

and replace it with

import dolfin
dolfin.plot(project(Tm[-1], V), 'o', label='Dt vs Tm', linestyle='solid')

Thank you sir for the reply

I am getting plot like that: (there is no effect of writing dashed or solid line style and I am not able to conclude anything about T_m with dt)

but I want plot in this way

As dt changes from 0.5, ,0.25,0.125, 0.0625, 0.03125. I want these values on the x axis and wanted to plot T_m corresponding to these dt on y axis.

You can create arrays; one for dt and one for the values at those time points. Inside the time loop, append those arrays with values if time and the solution when dt takes on the values you want.