Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations KootK on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Some ideas? No actual code needed...

Status
Not open for further replies.

Mechaero2006

Aerospace
Jan 26, 2014
29
Hello all,

I have this simple numerical problem to solve, for which I know what to do (done it in 1D) and I am only asking which -in your opinion- would be the best way to arrange and present my data in order to plot them using gnuplot or similar. A brief description of the problem follows:

A nxn grid will be created, say 41 points on x axis and 41 points on y axis. The initial values for our function will be stored in a 41 x 2 array (call it u(i,j)). Conditions applied on x,y axes values will apply on u. Thus we will have the x,y coordinates and two columns of "zeros" for the corresponding u(i,j) values. Then the numerical scheme will be applied to solve the problem. Something like: u(i,j) = 2*u(i-1,j-1) +.... etc.

My first thought is to create a single matrix with four columns: x, y, u(i), u(j).Then store the initial values and write the code to solve the problem. Is that OK? Would it be better to have separate arrays to store the values for x,y and u?

I sincerely hope that the description of the problem does not cause any confusion. If so, please ask for clarifications.

Thank you in advance for your assistance.
 
Replies continue below

Recommended for you

Yes, it is a bit confusing; or, you are missing something...

what's that about a matrix with four columns with x, y, u(i), u(j)? That does not make sense, does it? I didn't think there was such thing as a separate u(i) or u(j)...the only thing I thought existed was u(i,j)...in other words, you have 41 x-values and 41 y-values and you have one u value per every combination of x and y...meaning, u is a 2D matrix of 41x41 and can be plotted as a surface (the z height) on top of the x-y plane.

Anyway, that's what I thought.

 
Your logic doesn't seem make sense; your n x n grid 41 points on a side means that your basic calculation matrix is 41x41, not 41x2. So i and j each range from 0 to 40, and the indices address a specific location in the 41x41 matrix. Since this is a evenly spaced grid, what you would normally be looking at is Δx and Δy, which are only two numbers and there's usually no need to know the actual x and y coordinates. The only other numbers of interest are probably 41*Δx and 41*Δy, which are the physical dimensions of the region covered by the grid.

TTFN
I can do absolutely anything. I'm an expert!
faq731-376 forum1529
 
I think the question was more about the best way to store the various data to cycle over the elements of the matrix. x and y being the coordinates of 2D plot space and u the solution, is it best to assign a single 2D array for all these data, or have separate vectors, or separate partitions.

My take would be: always think about the scalability and performance bottlenecks and try to consider cases you that don't immediately spring to mind. What happens if the matrix is symmetric. You can compute half the terms and then just use pointers for the lower triangle into the upper triangle. What happens if the matrix is sparse, or block diagonal. If you are going to handle huge data, you might not actually want to store zeros. Keeping separate vectors would allow packed methods to allocate only enough memory for the non-zero terms.

On the other hand, if the problem is always going to be of the order of 40x40, use the method which is easiest to maintain. When you look at your code in 6 months, will you understand what you did?

Just a few thoughts.
 
Thank you very much for taking the time to reply to my question. Yes, you are very right, my logic was wrong and I though about it a few hours after writing the question, however I didn't rectify it (my bad!) to assit you. So a big thanks to you all for pointing out my mistake and sorry for the late reply.

Indeed, it is a surface plot, so there will be simply an x value, a y value and the u(i,j) value (the z value in a 3D cartesian coordinate system as correctly pointed).

Thank you for sharing your thoughts with me and sorry for the confusion caused.
 
I promised "no coding..." (as per original title) however I couldn't resist the temptation...

Here is something easy for you: Since my x (or y) values have an non integer step for the do loop and this is not always acceptable, I have used the following code to fill my x values (aka DO-WHILE loop):

Code:
    step=(x_max-x_min)/(nx-1)
    x=x_min
    i=1
    u=0.0 !fill array with zeros
    do while (x < (x_max+step))
        u(i,1)=x
        x=x+step
        i=i+1
    end do

Any other (faster) suggestions?
Thanks in advance!
 
Is this for school?

You know EXACTLY how many iterations this will take, and if you're filling with zeros, you don't care what the x value is, so why calculate it? You should look up FOR loops in your textbook or on the internet

TTFN
I can do absolutely anything. I'm an expert!
faq731-376 forum1529
 
Hi IRstuff,

No, this is absolutely NOT a school/university/college etc. project. I am a just self learner. Nothing more, nothing less. The problem I am studying has an initial condition for u (or whatever you call it) depending on x,y values. Thus, I am filling it with zeros and then based on explicitely declared limits on x,y some values of u will change. That will be my IC which will be "feed" to the numerical solution to proceed with the calculations. I hope this answers your question. Maybe (I repeat maybe) the fact that my code had u (again!) for storing the x values confused you.


 
No, that wasn't the question. I am not confused, maybe you are.

Your code should look like:

do i=0,40
do j=0,40
u(i,j)=0
end do
end do

There should be no calculations needed; you already know how big the array is and you know what value to put into.

TTFN
I can do absolutely anything. I'm an expert!
faq731-376 forum1529
 
IRstuff,

I do not disagree with you. Your code is fine if you want a step size for i,j of one (or an integer). But what if you want to go from 0 to 40 with a step size of say 0.5?

Based on the above conversation (even with my "blurry" description of the problem) I have a clear view of what to do, therefore there is no need to continue this thread. In any case thank you and the other contributors for your interest.
 
I said this 3 times, now; the physical distance is irrelevant, since you're filling with zeros. Where in your setting of the array to 0 do you use the physical distance?

TTFN
I can do absolutely anything. I'm an expert!
faq731-376 forum1529
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor