# Pde heat diffusion solution

**URL:** <https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027>\
**Category:** General Usage\
**Tags:** question, homework\
**Created:** [July 4, 2021, 10:07am UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027 "2021-07-04T10:07:00Z")\
**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 4, 2021, 10:07am UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/1 "2021-07-04T10:07:00Z")

</div>

m = 100  
delta\_z = 0.015  
delta\_t = 0.15  
z= delta\_z : delta\_z : 1-delta\_z  
print(z)

j=1  
n=1  
while j\<=10  
f(j)=1/(j\*(delta\_z)-1)  
a(j) = (1+((2\*(delta\_t))/((delta\_z)^2))+(((delta\_t)_f(j))/(delta\_z))-((delta\_t)m(f(j))))  
b(j) = (-1)_(((delta\_t)/((delta\_z)^2))+((delta\_t)_f(j)/(delta\_z)))  
c(j) = (-1)_((delta\_t)/((delta\_z)^2))  
d(j) = (-1)  
print(f(j),a(j),b(j),c(j),d(j))  
j+=1

end  
u= zeros(4,4)  
u[1,end] = 0  
u[1,1] = 1  
a(j)\*u[n+1,j] + b(j)\*u[n+1,j+1] + c(j)\*u[n+1,j-1] + d(j)\*u[n,j] = 0  
using Plots  
j = 1:100; u(n,j);  
plot(j,u(n,j))

---

<div class="post-metadata">

**Author:** ![ess3sq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ess3sq/32/26834_2.png) [@ess3sq](https://discourse.julialang.org/u/ess3sq)\
**Post date:** [July 4, 2021, 11:39am UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/2 "2021-07-04T11:39:08Z")

</div>

You seem to have forgotten to include your question. Also maybe can you put your code in code blocks? It’s not legible and the \* get parsed as markdown italics

---

<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 4, 2021, 3:21pm UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/3 "2021-07-04T15:21:56Z")

</div>

![Screenshot from 2021-07-04 20-48-53](https://global.discourse-cdn.com/julialang/original/3X/e/c/ec069c44f2bc97d19643850ea4e5e95fe12a8e1c.png)

---

<div class="post-metadata">

**Author:** ![ess3sq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ess3sq/32/26834_2.png) [@ess3sq](https://discourse.julialang.org/u/ess3sq)\
**Post date:** [July 4, 2021, 3:52pm UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/4 "2021-07-04T15:52:57Z")

</div>

With “question” I don’t mean your assignment. I mean what are you expecting from your code and what happens instead. What exactly do you need help on? Few people will solve the entire exercise for you. Maybe using a few words to explain what you are trying to do doesn’t hurt either…

---

<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 4, 2021, 3:56pm UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/5 "2021-07-04T15:56:25Z")

</div>

It looks like you might have overlooked the paragraph right underneath the due date?

This forum doesn’t have an official homework policy, but it’s unlikely that someone will solve this for you. You might have more luck with a very targeted question if you run into a specific error, and post reproducible code.

---

<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 6, 2021, 12:08pm UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/6 "2021-07-06T12:08:56Z")

</div>

![Screenshot from 2021-07-06 17-38-11](https://global.discourse-cdn.com/julialang/original/3X/2/8/28929b88e9f78f430b4bb237f5b30a36f720eaea.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 6, 2021, 12:14pm UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/7 "2021-07-06T12:14:49Z")

</div>

![Screenshot from 2021-07-06 17-38-20](https://global.discourse-cdn.com/julialang/original/3X/1/6/16d66e77c2f2efaf688ed74756e66f0bc95ad080.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 6, 2021, 12:15pm UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/8 "2021-07-06T12:15:45Z")

</div>

is there need to add linalg

---

<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 6, 2021, 12:16pm UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/9 "2021-07-06T12:16:35Z")

</div>

Please make sure you read [Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757), particularly the bit about providing copy-pastable MWEs rather than screenshots.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [July 6, 2021, 12:22pm UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/10 "2021-07-06T12:22:50Z")

</div>

Regarding Homework Policy, there was an attempt to establish one, but it fizzled out a while ago. Maybe we should revive that effort.

> [@Homework policy](https://discourse.julialang.org/t/homework-policy/2342):
>
> There have been a few questions in the past few days which are most likely homework. As the language grows, more and more courses will adopt it, so it may be a good idea to formulate some kind of policy that aligns expectations of students looking for help here (and other forums), community members who are willing to help out, and those who just consider this noise. To get the discussion started: I think that asking for help on problem sets is OK, if it is clearly indicated. A homework tag w…

I don’t think it’s been “formalized” in the way the “Make it easier to help you” post has been.

That said, this post feels to be more in the category of “please solve this for me” instead of a clear “this is what’s going wrong, can you help me?” or “I expected _this_, but _that_ happens instead, why?”…

---

<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 6, 2021, 12:24pm UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/11 "2021-07-06T12:24:35Z")

</div>

m = 10000  
delta\_z = 0.015  
delta\_t = 0.15  
z= delta\_z : delta\_z : 1-delta\_z  
print(z)

j=1  
n=1  
while j\<=10  
f(j)=1/(j\*(delta\_z)-1)  
a(j) = (1+((2\*(delta\_t))/((delta\_z)^2))+(((delta\_t)_f(j))/(delta\_z))-((delta\_t)m(f(j))))  
b(j) = (-1)_(((delta\_t)/((delta\_z)^2))+((delta\_t)_f(j)/(delta\_z)))  
c(j) = (-1)_((delta\_t)/((delta\_z)^2))  
d(j) = (-1)  
print(f(j),a(j),b(j),c(j),d(j))  
j+=1

end

u= zeros(4,4)  
u[1,end] = 0  
u[1,1] = 1

a(j)\*u[n+1,j] + b(j)\*u[n+1,j+1] + c(j)\*u[n+1,j-1] + d(j)\*u[n,j] = 0

using Plots  
j = 1:100; u(n,j);  
plot(j,u(n,j))

---

<div class="post-metadata">

**Author:** ![ess3sq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ess3sq/32/26834_2.png) [@ess3sq](https://discourse.julialang.org/u/ess3sq)\
**Post date:** [July 6, 2021, 12:33pm UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/12 "2021-07-06T12:33:40Z")

</div>

> [@ess3sq](#):
>
> I mean what are you expecting from your code and what happens instead. What exactly do you need help on?

@raman_kumar I don’t think there are any magicians among us who can divine your thoughts. Please explain what your question is and where you are stuck.

---

<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 6, 2021, 3:26pm UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/13 "2021-07-06T15:26:49Z")

</div>

> [@raman\_kumar](#):
>
> a(j)\*u[n+1,j] + b(j)\*u[n+1,j+1] + c(j)\*u[n+1,j-1] + d(j)\*u[n,j] = 0

i am stuck at this line

---

<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 6, 2021, 3:30pm UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/14 "2021-07-06T15:30:53Z")

</div>

And what are you trying to do? `a(j)` is a function call, is that what you’re after? Your error can be minimally reproduced by this:

```julia
julia> a(i) + b(j) = 0
ERROR: syntax: "a(i)" is not a valid function argument name around REPL[20]:1
Stacktrace:
 [1] top-level scope
   @ REPL[20]:1

```

Which tells you that when seeing `a(i)` on the left hand side of an equal sign, Julia expects you to define a function, but adding to function calls on the LHS is not valid syntax. What are you actually trying to do with this line?

---

<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 6, 2021, 3:37pm UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/15 "2021-07-06T15:37:39Z")

</div>

i want to plot u[n,j]

---

<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 6, 2021, 3:41pm UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/16 "2021-07-06T15:41:07Z")

</div>

In your code above you have `plot(j, u(n, j))`, rather than `u[n, j]` - which one is it? It appears that `u` is a 4x4 matrix, so `u(n, j)` won’t work because it tries to call a function `u` (that’s what round brackets do), while `u[n, j]` wouldn’t work because you define `j = 1:100` and `u` doesn’t have 100 columns.

---

<div class="post-metadata">

**Author:** ![Nathan\_Boyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nathan_boyer/32/14825_2.png) [@Nathan\_Boyer](https://discourse.julialang.org/u/Nathan_Boyer)\
**Post date:** [July 6, 2021, 3:44pm UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/17 "2021-07-06T15:44:38Z")

</div>

> [@raman\_kumar](#):
>
> > [@raman\_kumar](#):
> >
> > a(j)\*u[n+1,j] + b(j)\*u[n+1,j+1] + c(j)\*u[n+1,j-1] + d(j)\*u[n,j] = 0
> 
> i am stuck at this line

All indexing must be with square brackets. Parentheses are for function calls. Replace `a(j)`, `b(j)`, … with `a[j]`, `b[j]`, … in this line and the definitions above it.

---

<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 6, 2021, 3:45pm UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/18 "2021-07-06T15:45:32Z")

</div>

syntax: “(a(j) \* u[(n + 1), j])” is not a valid function argument name around In[5]: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 6, 2021, 3:47pm UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/19 "2021-07-06T15:47:03Z")

</div>

but i have defined it as a(j)

---

<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 6, 2021, 3:48pm UTC](https://discourse.julialang.org/t/pde-heat-diffusion-solution/64027/20 "2021-07-06T15:48:16Z")

</div>

I’m not sure that `a`, `b` etc are supposed to be containers being indexed rather than functions - in the loop above it just says `a(j) = ...` so maybe it’s supposed to define a function?

In any case @raman_kumar the error about invalid function argument names I have explained above, I think to make progress you should start by reading the Julia documentation to understand the basic concepts of working with functions, vectors, matrices, indexing etc. in Julia.

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