INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Some ideas? No actual code needed...

## Some ideas? No actual code needed...

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

### RE: Some ideas? No actual code needed...

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.

### RE: Some ideas? No actual code needed...

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: Eng-Tips.com Forum Policies forum1529: Translation Assistance for Engineers

### RE: Some ideas? No actual code needed...

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.

### RE: Some ideas? No actual code needed...

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

### RE: Some ideas? No actual code needed...

(OP)
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 --> 95

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?

### RE: Some ideas? No actual code needed...

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: Eng-Tips.com Forum Policies forum1529: Translation Assistance for Engineers

### RE: Some ideas? No actual code needed...

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

### RE: Some ideas? No actual code needed...

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

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: Eng-Tips.com Forum Policies forum1529: Translation Assistance for Engineers

### RE: Some ideas? No actual code needed...

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

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!