Thread: 1D Linear Convection Python View Single Post
Mentor
P: 21,397
 Quote by Jonny6001 Hi, thanks for taking the time to reply. I am pretty sure that in python, you can do 0.5<=x(i)<=1, I have seen a few documents and it doesn't complain about the syntax.
Nor does C, which is even worse. IOW, it's not a syntax error in C, so you don't hear from the compiler about it, but it is a semantic error.

Here's an example that shows how it works in C and might work in python, using a slightly simpler example.

result = 0.5 <= x <= 1

Suppose x has been set to .75. The expression 0.5 <= x is true, so the value of the expression (0.5 <= x) is 1. 1 <= 1 is true, so the overall expression has a value of 1, which is what is stored in result. That is the expected behavior.

Now suppose x has been set to 1.5. The expression 0.5 <= x is true again, so the value of the expression (0.5 <= x) is 1. 1 <= 1 is true, so the overall expression has a value of 1, which is what is stored in result. This is not what was expected.

One more. Suppose x has been set to 0.0. The expression 0.5 <= x is false this time, so the value of the expression (0.5 <= x) is 0. 0 <= 1 is true, so the overall expression has a value of 1, which is what is stored in result. This is not what was expected.

In other words, no matter what value x is, the expression 0.5 <= x <= 1 always comes out true - at least in C, and I would be very surprised if python behaves any differently.

I'll take a look at your other comments in a little while.

 Quote by Jonny6001 The issue I think is the loop doesn't seem to be running 'i' between the values in the brackets. The code below, I see it as being; increment 'i' between 1 and 'nx', if i<10, put y=10, if y==10 put y=20, if y>10 put y=50 So if I was to plot (i,y) I was expecting to see a graph with 'i' along the x axis ranging from 1-20, and the corresponding values of 'y' so 'y' would be 10, then when i==10, y goes up to 20 then its 50 for the remaining values of 'i'. For some reason the plot axis goes to the region of the final value as it it wasn't running and recording the whole loop. I won't paste the code in this post as its so short, you can see the code and the graph output in the attached image. I recall ages ago when I did more things like this, there was a commond at the end of the loop such as i++ or something to add 1 to the value of i, perhaps Python requires something similar? I'm sorry if my questions are a little boring but I really want to get heavily in to Scientific Programming and Mathematical Modelling. By the way, the pseudocode I wrote earlier is a way of approximating a partial differential equation describing linear convection, using finite difference method. Thanks so much for taking the time to bother with this.