# Heat flow in 1d

**URL:** <https://discourse.julialang.org/t/heat-flow-in-1d/81711>\
**Category:** Visualization\
**Tags:** question\
**Created:** [May 26, 2022, 12:09pm UTC](https://discourse.julialang.org/t/heat-flow-in-1d/81711 "2022-05-26T12:09:31Z")\
**Posts on this page:** 11\
**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:** [May 26, 2022, 12:09pm UTC](https://discourse.julialang.org/t/heat-flow-in-1d/81711/1 "2022-05-26T12:09:32Z")

</div>

![Screenshot from 2022-05-25 13-22-16](https://global.discourse-cdn.com/julialang/original/3X/b/e/be81ce4a3eb3c70509d73557fdcef7bf86ad1327.png)  
I want to Plot Theta vs index " i ". where θ[i-1]+b\*θ[i]+θ[i+1] =0 is the equation that i obtained after  
discretization . b is parameter. i took m=100 and delta g= 0.01

```julia
using Plots

begin
	Δg=0.01 ;m=100
	b=-2-(m^2)*(Δg)^2
end

θ=zeros(Int8,100)

begin
	θ[1]=0
	θ[100]=1
end

p=plot()

for i in 2:99
	θ[i-1]+b*θ[i]+θ[i+1] =0
	print(θ[i-1])
	plot(θ[:])
end

current()

```

 ![Screenshot from 2022-05-26 17-46-54](https://global.discourse-cdn.com/julialang/original/3X/4/6/46a078bd732f380057625f2bd3c16824768f3be6.png)

---

<div class="post-metadata">

**Author:** ![Karthik-d-k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karthik-d-k/32/35438_2.png) [@Karthik-d-k](https://discourse.julialang.org/u/Karthik-d-k)\
**Post date:** [May 26, 2022, 1:59pm UTC](https://discourse.julialang.org/t/heat-flow-in-1d/81711/2 "2022-05-26T13:59:36Z")

</div>

Hi @raman_kumar

There are two corrections i could find (i might be wrong though) →

1. In screenshot it is mentioned that θᵢ= 1 for i = 1 and θᵢ = 0 for i = n  
So we should get below code right ? → 

2. `θ[i-1] + b*θ[i] + θ[i+1] = 0` is not a valid julia syntax, if you need to update `θ` array by indexing, probably we need to do as follows ? → 

These are just my observations !!

---

<div class="post-metadata">

**Author:** ![Karthik-d-k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karthik-d-k/32/35438_2.png) [@Karthik-d-k](https://discourse.julialang.org/u/Karthik-d-k)\
**Post date:** [May 26, 2022, 4:03pm UTC](https://discourse.julialang.org/t/heat-flow-in-1d/81711/4 "2022-05-26T16:03:59Z")

</div>

Values of `θ` should be only `Int8` ? because if we change the array to hold `Float64`, we get the values from `θ[2] = 0.3333333333333333` to `θ[99] = 1.7462926957343605e-47` with the same graph as you posted (decreasing trend)

---

<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:** [May 28, 2022, 7:00am UTC](https://discourse.julialang.org/t/heat-flow-in-1d/81711/6 "2022-05-28T07:00:42Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/3/b/3b7dd264b8c2a14486065f03ba08322b8ebb9d42.png)  
i am not able to solve round error.

---

<div class="post-metadata">

**Author:** ![Karthik-d-k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karthik-d-k/32/35438_2.png) [@Karthik-d-k](https://discourse.julialang.org/u/Karthik-d-k)\
**Post date:** [May 28, 2022, 9:14am UTC](https://discourse.julialang.org/t/heat-flow-in-1d/81711/7 "2022-05-28T09:14:43Z")

</div>

If you change the `θ` array to hold `Float64` values, you won’t get this error…  
From →

```julia
θ=zeros(Int8,100)

```

to →

```julia
θ=zeros(Float64,100) # or θ=zeros(100), by default we get Float64 values

```

Currently it is trying to convert final calculated values (which are floats…) into `Int8` (bcz your array elements are of type `Int8`) values which is not possible, so by changing to Float64 - θ values you would get correct result without type conversions or rounding.

---

<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:** [May 28, 2022, 9:53am UTC](https://discourse.julialang.org/t/heat-flow-in-1d/81711/8 "2022-05-28T09:53:32Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/0/6/06679f24e01b4515a0d3af62f3f85a5f73c4dd7c.png)  
yes i have solved that issue of Float64. but still there is change in graph as i run the last code again and again as values of theta are increasing every time i want the plot at the end so that it looks exponentially decreasing.

---

<div class="post-metadata">

**Author:** ![Karthik-d-k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karthik-d-k/32/35438_2.png) [@Karthik-d-k](https://discourse.julialang.org/u/Karthik-d-k)\
**Post date:** [May 28, 2022, 10:10am UTC](https://discourse.julialang.org/t/heat-flow-in-1d/81711/9 "2022-05-28T10:10:44Z")

</div>

could you please paste the code as well, so that it is easy to copy paste and try,  
u can do so inside start and end triple back ticks (```) like shown below →   
```  
insert your code here  
```

---

<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:** [May 28, 2022, 10:15am UTC](https://discourse.julialang.org/t/heat-flow-in-1d/81711/10 "2022-05-28T10:15:15Z")

</div>

using Plots

begin  
Δg=0.000000001 ;m=90  
b=-2-(m^2)\*(Δg)^2  
end

θ=zeros(100)

begin  
θ[1]=1;θ[100]=0  
end

p=plot();

for i in 2:99

```
	while i<99
		θ[i] = round((-1/b)*(θ[i-1] + θ[i+1]),digits=9);
        print(θ[i]);
            i+=10^60
        plot(θ[:], seriestype=:scatter)
	end
end

```

- when you will run last code again and again then the values of theta will increase. \*

---

<div class="post-metadata">

**Author:** ![Karthik-d-k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karthik-d-k/32/35438_2.png) [@Karthik-d-k](https://discourse.julialang.org/u/Karthik-d-k)\
**Post date:** [May 28, 2022, 10:26am UTC](https://discourse.julialang.org/t/heat-flow-in-1d/81711/11 "2022-05-28T10:26:55Z")

</div>

Yes, you are right, because `θ` is an array which is mutable (we can modify its contents) so when you run the code for 2nd time, it would use the `θ` values from 1st iteration (which are not zeros anymore) and so on…  
so probably u just need to update the whole array once in one loop and then you can plot the complete `θ` values just once, which would look like exponential decreasing scatter plot.  
Code is as follows →

```julia
using Plots

begin
Δg=0.000000001 ;m=90
b=-2-(m^2)*(Δg)^2
end

θ=zeros(100)

begin
θ[1]=1;θ[100]=0
end

p=plot();

for i in 2:99
    θ[i] = round((-1/b)*(θ[i-1] + θ[i+1]),digits=9);
    print(θ[i]);
end

plot(θ[:], seriestype=:scatter)

```

---

<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:** [May 28, 2022, 10:35am UTC](https://discourse.julialang.org/t/heat-flow-in-1d/81711/12 "2022-05-28T10:35:41Z")

</div>

![image](https://global.discourse-cdn.com/julialang/original/3X/5/8/58db7a85f4c41e07260cb567899755646dc7f68b.png)

\*\* That Plot is fine and beautiful. Thank you Karthik \*\*

---

<div class="post-metadata">

**Author:** ![Karthik-d-k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/karthik-d-k/32/35438_2.png) [@Karthik-d-k](https://discourse.julialang.org/u/Karthik-d-k)\
**Post date:** [May 28, 2022, 10:44am UTC](https://discourse.julialang.org/t/heat-flow-in-1d/81711/13 "2022-05-28T10:44:09Z")

</div>

You are welcome 🙂
