# Trying to solve 1D wave equation but not getting the plot correct

**URL:** https://community.freefem.org/t/trying-to-solve-1d-wave-equation-but-not-getting-the-plot-correct/3905
**Category:** General Discussion
**Created:** [May 7, 2025, 2:32pm UTC](https://community.freefem.org/t/trying-to-solve-1d-wave-equation-but-not-getting-the-plot-correct/3905 "2025-05-07T14:32:19Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![ds2201](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@ds2201](https://community.freefem.org/u/ds2201)
#### Post date: [May 7, 2025, 2:32pm UTC](https://community.freefem.org/t/trying-to-solve-1d-wave-equation-but-not-getting-the-plot-correct/3905/1 "2025-05-07T14:32:20Z")

</div>

I am trying to solve the 1D wave equation. When I am trying to plot the approximated solution I am getting a straight line. Can anyone please check what is wrong with the plot command.  
As I am unable to attach the edp file, I am pasting the code here.

// Problem definition  
//1d pde on the boundary term u\_t=u\_{xx} 0\<x\<1, 0\<t\<1  
// u(x,0)=sin(pi\*x)  
// u(0,t)=0,u(1,t)=0

verbosity=0;  
macro uex(t) (sin(pi\*x)_exp(t))// Exact Solution  
macro f(t) (sin(pi_x)_exp(t)_(pi^2+1))//Source Function

int nref=2;  
real[int] L2error(nref); // initialize the L2 error array  
real[int] Dx(nref); // initialize the space discretization array  
real[int] DT(nref); // initialize the time discretization array

for(int n=0; n\<nref;n++) {  
int R=2^(n+3);// grid points on x-interval [0,1]  
real h=1./R;  
Dx[n]=h;  
real dt=h^2;  
DT[n]=dt;  
real t, T=1;

// Technical data to recreate OX and OY axis  
border OX(t=0,1){x=t;y=0;}  
border OY(t=0,1){x=0;y=t;}

meshL Th=segment(R);

plot(Th, cmm="NO ADAPTED MESH ",wait=0);//,ps=“reactiondiffusionnoadapted2dmesh.eps”);  
fespace Uh(Th,P1);

Uh u, v, uold;

u=sin(pi\*x);// initial condition

problem Wave(u,v)=int1d(Th)(u\*v/dt+dx(u)_dx(v))  
-int1d(Th)(uold_v/dt)  
-int1d(Th)(f(t+dt/2.)\*v)  
+on(1,2,u=0);

// time loop  
int m, M = T/dt;  
for (m = 0; m \< M; m++) {  
uold=u;  
Wave;  
t+=dt;  
plot(u,cmm=“Wave Equation Appox soln. u at time t=”+t,wait=0,value=1);  
}// end of time loop

L2error[n]=sqrt(int1d(Th)(abs(u-uex(t))^2));  
cout \<\< "L2 error " \<\< L2error[n] \<\< endl;

}// end of loop on n\<nref

---

<div class="post-metadata">

### Author: ![goutham2023](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/goutham2023/32/1759_2.png) [@goutham2023](https://community.freefem.org/u/goutham2023)
#### Post date: [May 7, 2025, 7:06pm UTC](https://community.freefem.org/t/trying-to-solve-1d-wave-equation-but-not-getting-the-plot-correct/3905/2 "2025-05-07T19:06:20Z")

</div>

Hi,  
The problem statement in the code appears to be incorrect for a 1D wave equation. The formulation shown corresponds to the weak form of a parabolic equation, such as the heat equation. For the linear wave equation with a wave speed of 1, the correct weak form should involve dx(u)\*v. I have made the necessary correction and attached the updated version. Please let me know if my understanding is incorrect.  
[trial.edp](https://community.freefem.org/uploads/short-url/2ajqcVwCdQZsUfPvxkvVZgAn6zb.edp) (1.5 KB)

---

<div class="post-metadata">

### Author: ![ds2201](https://avatars.discourse-cdn.com/v4/letter/d/48db29/32.png) [@ds2201](https://community.freefem.org/u/ds2201)
#### Post date: [May 8, 2025, 5:11am UTC](https://community.freefem.org/t/trying-to-solve-1d-wave-equation-but-not-getting-the-plot-correct/3905/3 "2025-05-08T05:11:21Z")

</div>

Hi,  
This code is not working as the error started increasing. For my code I was getting the correct error. I was having the problem in the plot of the approximate solution. While plotting the solution I am getting a straight line which should not happen because exact solution is a sine function. Please let me know if anything wrong with the plot command for 1D problem.

---

<div class="post-metadata">

### Author: ![goutham2023](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/goutham2023/32/1759_2.png) [@goutham2023](https://community.freefem.org/u/goutham2023)
#### Post date: [May 8, 2025, 9:01am UTC](https://community.freefem.org/t/trying-to-solve-1d-wave-equation-but-not-getting-the-plot-correct/3905/4 "2025-05-08T09:01:48Z")

</div>

Hi,  
please check the code i am getting a sine function as output and the error is also decreasing. But i am not sure if this is the weak form for a 1D wave equation  
[trial.edp](https://community.freefem.org/uploads/short-url/tmXvsdZc8eTowwZXY3JIMi8IKR7.edp) (1.4 KB)

---

<div class="post-metadata">

### Author: ![fb77](https://yyz2.discourse-cdn.com/flex030/user_avatar/community.freefem.org/fb77/32/3796_2.png) [@fb77](https://community.freefem.org/u/fb77)
#### Post date: [May 9, 2025, 7:49am UTC](https://community.freefem.org/t/trying-to-solve-1d-wave-equation-but-not-getting-the-plot-correct/3905/5 "2025-05-09T07:49:05Z")

</div>

The plot command as you write is not supposed to work for 1d.  
Instead you can do as

```auto
real[int] xx(R+1),yy(R+1),yyexact(R+1);
Uh uexe=uex(t);
for (int i=0;i<R+1;i++)
{
xx[i]=i*1./R;
yy[i]=u[](i);
yyexact[i]=uexe[](i);
}
plot([xx,yy],[xx,yyexact],wait =0,bb=[[0,0],[1,3]]);

```

But I don’t know how to plot the axis.

About your equation, you are solving the heat equation, not the wave equation.
