PDE heat equation

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?

I think the error is reasonably clear:

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> 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>)

2 Likes

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] ?

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

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).

5 Likes

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

5 Likes

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

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

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!)

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

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) 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.

2 Likes

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

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

8 Likes

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

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> using Plots

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

This works if you do

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?)

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

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,

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,

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.

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

p = plot()

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

display(p)

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


i got plot

2 Likes

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)