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.
In your code above you have
All indexing must be with square brackets. Parentheses are for function calls. Replace
b(j), … with
b[j], … in this line and the definitions above it.
syntax: “(a(j) * u[(n + 1), j])” is not a valid function argument name around In:1
but i have defined it as a(j)
I’m not sure that
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.
should i use using Linearalgebra
No, as has been said a few times now all your errors here are very basic syntax errors, which will not be helped by any packages. I don’t think you’ll get very far without reading the documentation, maybe starting with
d are only functions of
j (and constants) so I think they are meant to be evaluated into vectors. Otherwise, the function definitions should be placed outside of the
@raman_kumar Are you trying to use functions or vectors in your implementation? You can use either.
Raman, you need to take a step back. The line you are “stuck” at below is just the first line that Julia
couldn’t make any sense of. The lines above that didn’t error, but I don’t think they are doing what you think they are doing either.
You can’t just declare that equation above and expect
u to be solved for you. That’s not how computers work. You need to “write a numerical algorithm” as stated in Part 1 of your assignment which will define the elements of
u in a loop. It should take the form
u[n+1,j] = right-hand-side such that you define
u at the next timestep and current spatial location based on
u at the current timestep and surrounding spatial locations.
You have some fundamental misunderstandings about the Julia language and numerical methods which are not all going to be solved in this thread. Read the Julia documentation and your course material and then formulate some more specific questions that are deeper than just get me my final plot so I can turn it in.
I second that.
Additionally, @raman_kumar from what I gather you seem to want to fill some kinds of vectors/arrays/whatever in a loop (vectors
f looping over index
j). To do this, not only do you need
a[i] = ... instead of
a(i) = ... (as several people pointed out before), but you also have to define the vectors
f first, ie. using
a = zeros(10).
I believe the loop could be replaced by dot syntax, but suggesting that might add to OP’s confusion.
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)
i replaced ( ) with ,but getting error
syntax: missing comma or ) in argument list
Again the error is a very basic syntax error, and the error message is telling you exactly what’s wrong - you have unbalanced parentheses somewhere. Indeed it appears that almost all brackets you are using above aren’t actually doing anything and can be removed. Replacing the delta symbols with unicode for legibility (note I had to use a lowerscript
x here because Unicode doesn’t have lowerscript
j=1; n=1 while j<=10 f[j] = 1/(j*δₓ - 1) a[j] = 1 + 2*δₜ/δₓ^2 + δₜ*f[j]/δₓ - δₜ*m(f[j]) b[j] = -δₜ/δₓ^2 + δₜ*f[j]/δₓ c[j] = -δₜ/δₓ^2 d[j] = -1 print(f[j],a[j],b[j],c[j],d[j]) j+=1 end
Please use three backticks ``` to start and end a block of code. Then it will be correctly formatted.
@raman_kumar What @PetrKryslUCSD was suggesting is how to show your code in a reply here. You use your mouse to select the lines of code. Then type three backticks characters in the in the reply.
Then paste the code into the reply window. finally you type three backticks characters.
Now - look carefully at the window to the right of the window you just posted in.
This is what we see in the forum when you press reply.Look at how the text appears.
@raman_kumar As people are saying here you need to take a step back.
Write what is called pseudo-code which is the words which describe your intention.
These pseudo-code lines can be written as comments.
Once you understand what the ‘flow’ of the algorithm is then you can start to put in lines of real code.
Also do NOT write all the lines of code at one time. Get a line correct. Get the next line correct.
Construct a loop - but make the contents of that loop simple. When you are confident the loop is working add more lines within the loop. Remember your pseudocode?
Use the power of the Julia REPL or the notebook system you are using - look how the REPL prints out what the last line returned.
Here is a pseudocode example from my very first day at Glasgow University which was taught by the late Dr Jennifer Haselgrove. We were taught top-down programming, if anyone stil remembers it!
# Teach your cat to make tea
# fill kettle with water # boil the water on the stove # place tea in the teapot # pour boiling water into the teapot # wait two minutes # pour tea into a cup
You refuse to read our replies. I already told you why this wouldn’t work:
People have been telling you to take a step back and figure out the basics first but you seem to stubbornly refuse to, which is a shame because these are suggestions that would benefit you, not us.
@raman_kumar You need to invest more time into your posts here if you’re to expect any help. It is not sufficient to just post code and errors and expect others to invest their time and energy into figuring out what you’re doing. Use words — describe what you’re doing, where you’re having trouble, what you think is the problem, etc. Folks here have asked you to explain more and given tips on how to do so, but you’ve refused.
Beyond that, though, it appears as though your professor has explicitly disallowed asking for help. This may be a very serious violation of your academic integrity policies and I wouldn’t be surprised if your professor were here.
For both those reasons, I’m closing this topic. In the future, the way to ask questions about code is to actually ask them with words. You’re interacting with real people here — you need to talk to us like we’re human. Please read the guidance in Please read: make it easier to help you before posting again.