# Solving a PDE but no plots are returned

**URL:** <https://discourse.julialang.org/t/solving-a-pde-but-no-plots-are-returned/123493>\
**Category:** New to Julia\
**Created:** [December 5, 2024, 12:03pm UTC](https://discourse.julialang.org/t/solving-a-pde-but-no-plots-are-returned/123493 "2024-12-05T12:03:54Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jax\_Xu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jax_xu/32/213903_2.png) [@Jax\_Xu](https://discourse.julialang.org/u/Jax_Xu)\
**Post date:** [December 5, 2024, 12:03pm UTC](https://discourse.julialang.org/t/solving-a-pde-but-no-plots-are-returned/123493/1 "2024-12-05T12:03:55Z")

</div>

Hi Community! I am practicing solving a basic one-dimensional diffusion equation in Julia and hoping to get familiar with Julia asap.  
I fixed up the local scope parameters by using ‘global’ to define ‘u’ and ‘b’ in my loop. However, no plots were returned after I ran my codes. Could you please give me some advice? I would really appreciate your help!

> ##solve the very basic 1-D diffusion equation with no-flux BC on left and right sides.  
> using DifferentialEquations  
> using Plots  
> using LinearAlgebra  
> using Latexify  
> L=1.0  
> NX=500  
> dx=L/NX  
> nx=NX+1  
> x=(0:NX)\*dx
> 
> D=1e-6  
> dt=dx^2/(2_D)  
> nsteps=10000  
> alpha=dt_D/(dx^2)
> 
> diags1=2\*(1+alpha)_ones(nx,1)  
> diags2=ones(nx-1,1)_(-alpha)  
> A=sparse(Matrix(2\*(1+alpha)I, nx, nx))
> 
> ##B=Tridiagonal(diags1,diags2,diags1) ##tried to directly define a tridiagonal matrix but failed.
> 
> for i in 1:nx, j in 1:nx  
> abs(i-j)\<=1 && (A[i,j]+=-alpha)  
> i==j && (A[i,j]=2\*(1+alpha))  
> end
> 
> #######Boundary conditions  
> A[1,1:3]=[-3,4,1]  
> A[nx,nx-2:nx]=[1,-4,3]
> 
> sigma=L/16  
> u=1/(sigma_sqrt(2_pi))_exp.(-0.5_(x/sigma).^2) #initial value: Gaussian function  
> plot(x,u,color=“blue”)  
> #####solving the PDE  
> for m in 1:1:nsteps  
> global b=[0; (alpha_u[1:nx-2]+2_(1-alpha)_u[2:nx-1]+alpha_u[3:nx]); 0]  
> global u=A\b  
> if mod(m,100)==0  
> plot!(x,u,color=“red”)  
> end  
> end

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 5, 2024, 12:28pm UTC](https://discourse.julialang.org/t/solving-a-pde-but-no-plots-are-returned/123493/2 "2024-12-05T12:28:55Z")

</div>

Loops return `nothing`. You need to either assign your plot to a variable and call that:

```julia
p = plot(x, u)
for m in 1:nsteps
    plots!(p, x, u)
end
p

```

or call `current()` after your loop if you don’t assign the plot to a variable.

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 5, 2024, 12:34pm UTC](https://discourse.julialang.org/t/solving-a-pde-but-no-plots-are-returned/123493/3 "2024-12-05T12:34:16Z")

</div>

Unrelated to this, I would recommend you stop working in global scope as that’s terrible for performance, and bad practice in general. It looks like the parameters that vary in your code are `L`, `NX`, `D`, and `nsteps`, so you can do

```julia
function solve_pde(L, NX, D, nsteps)
    # your code here
end

```

you can then either return the plot from this function as I said above, or do `display(p)` in your function if you just want to show it without returning anything.

(Also no need to do `1:1:nsteps`, `1:nsteps` will do as 1 is the default step)

---

<div class="post-metadata">

**Author:** ![Jax\_Xu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jax_xu/32/213903_2.png) [@Jax\_Xu](https://discourse.julialang.org/u/Jax_Xu)\
**Post date:** [December 5, 2024, 2:46pm UTC](https://discourse.julialang.org/t/solving-a-pde-but-no-plots-are-returned/123493/4 "2024-12-05T14:46:35Z")

</div>

Hi Nishg! 😁 Thanks for your advice! I tried and it runs faster! Unrelated to this question, I found that sometimes I changed the parameters or even deleted the plot syntax, Julia still returns the old results and plots. Why would this happen and how should I correctly use Julia?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 5, 2024, 3:38pm UTC](https://discourse.julialang.org/t/solving-a-pde-but-no-plots-are-returned/123493/5 "2024-12-05T15:38:25Z")

</div>

Hard to say without an example of how this happens.

---

<div class="post-metadata">

**Author:** ![Jax\_Xu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jax_xu/32/213903_2.png) [@Jax\_Xu](https://discourse.julialang.org/u/Jax_Xu)\
**Post date:** [December 5, 2024, 4:43pm UTC](https://discourse.julialang.org/t/solving-a-pde-but-no-plots-are-returned/123493/6 "2024-12-05T16:43:36Z")

</div>

Is there a workspace in Julia, just as what Matlab does? For example, after I run the codes, all solutions and parameters are stored temporarily in ‘workspace’, including the parameter x. Even if I delete the syntaxes relative to x, the value of x can still be called. Can I empty the ‘workspace’ and where is it if it exists?

(I just testified some simple examples. The ‘workspace’ does exist. if I define a=12, b=13, c=a+b. Run. Then, delete all syntaxes. Display(c). It returns 25.)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [December 5, 2024, 4:47pm UTC](https://discourse.julialang.org/t/solving-a-pde-but-no-plots-are-returned/123493/7 "2024-12-05T16:47:21Z")

</div>

Without going into too much detail about modules, namespaces etc. the short answer is no, there’s nothing like `clear` in Matlab. Which is another reason to keep your code in functions and not pollute the global namespace with lots of definitions.

---

<div class="post-metadata">

**Author:** ![Jax\_Xu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jax_xu/32/213903_2.png) [@Jax\_Xu](https://discourse.julialang.org/u/Jax_Xu)\
**Post date:** [December 6, 2024, 2:02am UTC](https://discourse.julialang.org/t/solving-a-pde-but-no-plots-are-returned/123493/8 "2024-12-06T02:02:28Z")

</div>

Sound and Clear!! 👍 👍 👍 👍 😃

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 6, 2024, 2:26am UTC](https://discourse.julialang.org/t/solving-a-pde-but-no-plots-are-returned/123493/9 "2024-12-06T02:26:26Z")

</div>

> [@nilshg](#):
>
> the short answer is no, there’s nothing like `clear` in Matlab.

The closest thing is to set all the variables in `Main` to `nothing`, which will allow their memory to be freed.

See e.g. [How to clear variables and/or whole work space - #27 by heliosdrm](https://discourse.julialang.org/t/how-to-clear-variables-and-or-whole-work-space/10149/27) or this one-liner:

```julia
setproperty!.((Main,), filter(n -> !isconst(Main, n), names(Main)), nothing);

```

but this doesn’t eliminate constants, including function definitions and new methods, or other global state that might be affecting your code.

---

<div class="post-metadata">

**Author:** ![Jax\_Xu](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jax_xu/32/213903_2.png) [@Jax\_Xu](https://discourse.julialang.org/u/Jax_Xu)\
**Post date:** [December 7, 2024, 2:44am UTC](https://discourse.julialang.org/t/solving-a-pde-but-no-plots-are-returned/123493/10 "2024-12-07T02:44:45Z")

</div>

Thank you! The link is very helpful!
