# PDE heat equation

**URL:** <https://discourse.julialang.org/t/pde-heat-equation/64428>\
**Category:** Numerics\
**Tags:** question, homework\
**Created:** [July 10, 2021, 4:24pm UTC](https://discourse.julialang.org/t/pde-heat-equation/64428 "2021-07-10T16:24:46Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 10, 2021, 4:24pm UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/1 "2021-07-10T16:24:46Z")

</div>

i want to solve the matrix ,but i can’t do it. i am new to Julia. please help me in finding error .i have to plot the last equation given in picture below. what is MethodError i am making?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/f/dff1879b42d8d3d837a25f9c7df2224456343910.png)

---

<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:** [July 10, 2021, 4:36pm UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/2 "2021-07-10T16:36:52Z")

</div>

I think the error is reasonably clear:

```julia
julia> n = 1:10
1:10

julia> n + 1
ERROR: MethodError: no method matching +(::UnitRange{Int64}, ::Int64)

```

You can’t add an integer to a range. The way to do this is by broadcasting the addition:

```julia
julia> n .+ 1
2:11

```

But I don’t think this is what you actually want to do here.

(As an aside, I notice you have adapted my unicode notation from the other thread, just FYI the lowerscript letters can be written with `\_x<TAB>`)

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 10, 2021, 4:40pm UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/3 "2021-07-10T16:40:17Z")

</div>

i want to get the value of theta at many different indices. so that i can plot it at different time and position.  
should i change range to array ,by putting in [1:10] ?

 ![image](https://global.discourse-cdn.com/julialang/original/3X/d/f/df02dbf9d02f0debda08e03d208e37705219f080.png)

---

<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:** [July 10, 2021, 5:05pm UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/4 "2021-07-10T17:05:23Z")

</div>

I maintain my advice from the other thread - you should really spend some time reading the Julia manual and/or do some basic Julia tutorials, a lot of the things you are trying here indicate that you’re not sufficiently familiar with the basic workings of the language to really use it productively.

I take it that you essentially have a 2-dimensional array `θ` which you are trying to fill, where the values at each index depends on values in adjacent cells. To do this, you want to iterate over the indices like

```julia
for n in 1:size(θ, 1), j in 1:size(θ, 2)
     θ[n, j] = ...
end

```

although you need to think about what you do with boundaries (as your `n+1`, `j+1`, `j-1` indices will go out of bounds in the first or last iteration).

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 11, 2021, 4:00am UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/5 "2021-07-11T04:00:30Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/9/3/932b581d3fd1f43d1ec3138ad241de541dd18b16.png)

---

<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:** [July 11, 2021, 6:43am UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/6 "2021-07-11T06:43:48Z")

</div>

I would encourage you to read my last post again, especially the part following the code block!

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 15, 2021, 6:20am UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/7 "2021-07-15T06:20:55Z")

</div>

i am not able to move forward. please help me. i changed range of n and j, but it is still showing BoundError

---

<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:** [July 15, 2021, 6:33am UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/8 "2021-07-15T06:33:07Z")

</div>

I’m afraid I can’t help you with this, as this is not a Julia/coding problem but a conceptual problem.

The Julia bit is easy, you have a matrix that is 10 by 10 in size, and then do

```julia
for n in 1:size(θ, 1), j in 1:size(θ, 2)
     θ[n, j] = 1.6*θ[n+1, j] ...
end

```

(as an aside note that again your parentheses are not necessary here). Now your loop specification says `n in 1:size(θ, 1)`, which means n goes up to `size(θ, 1)`, which is 10 (as that’s the number of rows in your matrix `θ`). When `n=10` in your loop, you are therefore trying to calculate `1.6*θ[11, j]` on the right hand side of your equation, which fails as there is no 11th row in `θ`, which only has 10 rows.

You will hopefully see now why this isn’t a Julia problem: your algorithm basically says “calculate every element of `θ` as 1.6 times the element in the same column, one row below, minus 0.3 times the element one row down, one column to the right, minus 0.3 times the element one row down, one column to the left”. This algorithm obviously doesn’t work for the boundaries of your matrix, as e.g. in the last row, there is no element in the row below as that row doesn’t exist. What you want to do in that case is a conceptual question and depends on the actual real world goal of your computation, and figuring that out is - I would assume - part of your homework (plus I have no idea what a PDE heat equation is or does, so even if I wanted to solve your homework for you I couldn’t!)

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 15, 2021, 6:44am UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/9 "2021-07-15T06:44:01Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/5/9/590325774c84be557fb3a2c6e1a8ad750e365aa7.png)

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 15, 2021, 6:55am UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/10 "2021-07-15T06:55:10Z")

</div>

please solve it completely so that i can understand my mistake and be able to solve other problems. where i have replaced T by \theta

---

<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:** [July 15, 2021, 7:47am UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/11 "2021-07-15T07:47:18Z")

</div>

I think you misunderstood my post above - while I and many others on this forum are happy to help with Julia-specific coding questions, as people have done in this thread and the other thread you opened on the same topic, you will not get a solution to your homework problem here. I should also note again that you are violating the rules set out on your assignment (as posted [here](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/3)) which clearly state that the solution should be your own.

I have explained above what your “mistake” is as far as the Julia coding part goes, working out what these boundary conditions should be to avoid the `BoundsError` seems to be exactly what the question is asking for.

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 15, 2021, 7:52am UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/12 "2021-07-15T07:52:49Z")

</div>

date of homework submission is over. i was not able to do it at that time.

---

<div class="post-metadata">

**Author:** ![fredrikekre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fredrikekre/32/1688_2.png) [@fredrikekre](https://discourse.julialang.org/u/fredrikekre)\
**Post date:** [July 15, 2021, 8:13am UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/13 "2021-07-15T08:13:46Z")

</div>

Homework aside, you can’t dump equations from a paper/assignment like that and expect people to do the work for you.

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 19, 2021, 10:55am UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/14 "2021-07-19T10:55:38Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/d/3/d34ff99561a79b4e714b19877e1a8f2dbfc6e0ca.png)

now i have modified it to real problem,and have done changes.

---

<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:** [July 19, 2021, 11:24am UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/15 "2021-07-19T11:24:47Z")

</div>

Good to see you’re making progress - you’ve now run into another Julia specific issue: `plot` expects you to pass a vector of `x` and `y` coordinates to plot, not a single number:

```julia
julia> using Plots

julia> plot(1.0, 2.0)
ERROR: Cannot convert Float64 to series data for plotting

```

This works if you do

```julia
julia> plot([1.0], [2.0])

```

Although the plot will be emtpy (as `plot` tries to plot a line, and here only one point is passed so there’s nothing to be connected).

I’m not sure what you expect your plot to look like - θ is a matrix, and you’re currently plotting each cell in that matrix separately which is probably not what you want. You need to think about whether you want to plot each row or each column as a line (or something else entirely?)

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 19, 2021, 11:38am UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/16 "2021-07-19T11:38:30Z")

</div>

I want to plot theta as a function of time n. I mean how temperature is varying with time.

---

<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:** [July 19, 2021, 1:23pm UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/17 "2021-07-19T13:23:29Z")

</div>

Well like I said above, you just need to think about what each cell in θ represents, and then create a vector to plot. I probably shouldn’t have said matrix above, as it looks like θ has now turned into a four-dimensional array. Still,

```julia
plot(θ[:, 1, 1, 1])

```

Will plot the vector that’s at point 1, 1, 1 of the second, third, and fourth dimension of your array,

```julia
plot(θ[1, :, 1, 1])

```

will plot the vector at 1, 1, 1 in dimensions 1, 3 and four. It is up to you to decide which of these (if any) make sense to represent the evoluation of values over time.

---

<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:** [July 19, 2021, 2:12pm UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/19 "2021-07-19T14:12:27Z")

</div>

`for` loops do not return anything, you need to either instantiate the plot object before the loop and display it after

```julia
p = plot()

for i in 1:10
    plot!(rand(5))
end

display(p)

```

or show it by calling `current()` after the loop.

---

<div class="post-metadata">

**Author:** ![raman\_kumar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raman_kumar/32/26782_2.png) [@raman\_kumar](https://discourse.julialang.org/u/raman_kumar)\
**Post date:** [July 19, 2021, 3:07pm UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/20 "2021-07-19T15:07:49Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/f/a/faea3dfdc7017999f043c53fbec8dbcdd0eada16.png)  
i got plot

---

<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:** [July 19, 2021, 3:40pm UTC](https://discourse.julialang.org/t/pde-heat-equation/64428/21 "2021-07-19T15:40:20Z")

</div>

Great, but make sure that this does what you want it to do. By calling `plot` in your loop (rather than `plot!`, note the bang), you are creating a new plot at every loop iteration. So what you see in your plot is just `θ[:, 9, 9, 9]`, which might not be what you’re after (and if it is, you can just write `plot(θ[:, 9, 9, 9])` without any loop)

[Next page](https://discourse.julialang.org/t/pde-heat-equation/64428.md?page=2)
