Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Iterative solution from two 8x8 Matrices (please help a novice!)

Subject: Iterative solution from two 8x8 Matrices (please help a novice!)

From: Grufff

Date: 25 Mar, 2011 15:49:20

Message: 1 of 10

Short Version:

I'm new to matlab, writing my first m script to solve a temperature distribution problem iteratively.

I wish to use values from 8x8 matrix A to provide data to define 8x8 matrix B, using central differencing method. This involves using data from around a point of interest to generate a new value, eg:

B(2,2) = (A(1,2)+A(3,2)+A(2,1)+A(2,3))/4

I want to set this process in a for loop, creating values for each element of B similar to example above, then set A = B to map all newly generated values onto A, then repeat process through next loop iteration(s).

Does this sound a practical way to tackle the problem? Any tips or pointers you can offer a novice?

I've guessed at this syntax: B(2,2) = (A(1,2)+A(3,2)+A(2,1)+A(2,3))/4 so I'd be very grateful for a working example.

Can I build my 8x8 B matrix from a simple list of expressions similar to above, one for each element to be defined?

ie:

B(2,2) = (A(1,2)+A(3,2)+A(2,1)+A(2,3))/4
b(2,3) = (A(1,3)+A(3,3)+A(2,2)+A(2,4))/4
etc.

I'd really appreciate any advice or comments you experienced heads could throw my way to help ease me into Matlab.














Long Version:

(I wrote this first and realised it was overlong and would send most of you to sleep before you could help me, so wrote above short version. I'll leave this essay here though in case anyone wants some extra context. Sorry!)

I'd like to use Matlab to help me model and solve a simple 2D steady state heat conduction problem. I have no experience with Matlab and so have been reading through the help files etc and have a rough idea how I'll try to tackle this, but, I'd really appreciate any advice or tips at this stage in case my intended method is impractical/clumsy/stupid.

The problem (leaving out dimensions and other details, just the bullet points): A square section duct is buried in the earth some distance below the surface. This duct is at a steady temperature of 60 degrees C. I need to use the central divided difference method to show how the heat from the duct propagates into the surrounding earth, and provide temperatures at different points across a 2D section (end on) view.

Below is an 8x8 matrix representing the known and initial value guesses for temperatures: 4 degrees all across the surface, -17 degrees at lowest depth of interest, linear temperature gradient between those two values at some distance to the right of the duct, and the duct itself on the left hand side at 60 degrees (rows 4 & 5, columns 1 & 2). All of the 10 degree values are initial guesses to get the iterative process under way.

     4 4 4 4 4 4 4 4
    10 10 10 10 10 10 10 1
    10 10 10 10 10 10 10 -2
    60 60 10 10 10 10 10 -5
    60 60 10 10 10 10 10 -8
    10 10 10 10 10 10 10 -11
    10 10 10 10 10 10 10 -14
   -17 -17 -17 -17 -17 -17 -17 -17

So, my plan... write an m file script setting A = above matrix. Start a for loop with i = 1:(some value, however many iterations I might need).

Define a new 8x8 matrix B, using central divided differencing with data pulled from A to create each element of matrix B

Once this process is complete, set A = B (to map all the newly calculated values from B onto A), and return to start of loop to run through next iteration. Repeat until desired accuracy is reached.


Central differencing requires me to sum values from "north, south, east and west" of my point of interest and divide by 4 to find a new value. For example, to calculate value of B(2,2), I use values around that point from A as follows: B(2,2) = (A(1,2)+A(3,2)+A(2,1)+A(2,3))/4. With the values set as above this would give new value for B(2,2) as (4+10+10+10)/4 = 8.5

I'm just going to write a similar expression for each element in my B matrix which needs updating (seems long and clumsy, but *shrug*).

I'm not sure of the exact syntax i need for this element arithmetic, i'll look that up now, but if anyone has pointers of example to guide me I'd be very grateful.

Does this seem practical? Is there an easier or more efficient way? Apologies for the length of my post, I wanted to give some context so readers would understand what I was trying to do.

Subject: Iterative solution from two 8x8 Matrices (please help a novice!)

From: Florin

Date: 25 Mar, 2011 16:08:04

Message: 2 of 10

"Grufff" wrote in message <imidi0$1j1$1@fred.mathworks.com>...
> Short Version:
>
> I'm new to matlab, writing my first m script to solve a temperature distribution problem iteratively.
>
> I wish to use values from 8x8 matrix A to provide data to define 8x8 matrix B, using central differencing method. This involves using data from around a point of interest to generate a new value, eg:
>
> B(2,2) = (A(1,2)+A(3,2)+A(2,1)+A(2,3))/4
>
> I want to set this process in a for loop, creating values for each element of B similar to example above, then set A = B to map all newly generated values onto A, then repeat process through next loop iteration(s).
>
> Does this sound a practical way to tackle the problem? Any tips or pointers you can offer a novice?
>
> I've guessed at this syntax: B(2,2) = (A(1,2)+A(3,2)+A(2,1)+A(2,3))/4 so I'd be very grateful for a working example.
>
> Can I build my 8x8 B matrix from a simple list of expressions similar to above, one for each element to be defined?
>
> ie:
>
> B(2,2) = (A(1,2)+A(3,2)+A(2,1)+A(2,3))/4
> b(2,3) = (A(1,3)+A(3,3)+A(2,2)+A(2,4))/4
> etc.
>
> I'd really appreciate any advice or comments you experienced heads could throw my way to help ease me into Matlab.
>
Hi,

Not sure if it is the right way to approach the problem. Concerning
> B(2,2) = (A(1,2)+A(3,2)+A(2,1)+A(2,3))/4
and all the other iterations, I suggest to create and empty matrix B
>B=zeros(size(A));
and the have a look at "conv2"
> doc conv2
If you are familiar with convolutions, then you can easily create a 3x3 kernel that will provide you with what you are looking for.

N.B. take a look at the option 'same' for conv2.

Regards,
Florin

Subject: Iterative solution from two 8x8 Matrices (please help a novice!)

From: Grufff

Date: 25 Mar, 2011 16:23:04

Message: 3 of 10

"Florin" wrote in message <imiel4$leq$1@fred.mathworks.com>...

> Not sure if it is the right way to approach the problem. Concerning
> > B(2,2) = (A(1,2)+A(3,2)+A(2,1)+A(2,3))/4
> and all the other iterations, I suggest to create and empty matrix B
> >B=zeros(size(A));
> and the have a look at "conv2"
> > doc conv2
> If you are familiar with convolutions, then you can easily create a 3x3 kernel that will provide you with what you are looking for.
>
> N.B. take a look at the option 'same' for conv2.

I've not come across convolutions at all (and the kernel reference also goes over my head) but I appreciate the advice. I've brought up the document you pointed me towards and I'll have a good read of it now. Thanks for the input.

Subject: Iterative solution from two 8x8 Matrices (please help a novice!)

From: Florin

Date: 25 Mar, 2011 16:34:20

Message: 4 of 10

"Grufff" wrote in message <imifh8$7e3$1@fred.mathworks.com>...
> "Florin" wrote in message <imiel4$leq$1@fred.mathworks.com>...
>
> > Not sure if it is the right way to approach the problem. Concerning
> > > B(2,2) = (A(1,2)+A(3,2)+A(2,1)+A(2,3))/4
> > and all the other iterations, I suggest to create and empty matrix B
> > >B=zeros(size(A));
> > and the have a look at "conv2"
> > > doc conv2
> > If you are familiar with convolutions, then you can easily create a 3x3 kernel that will provide you with what you are looking for.
> >
> > N.B. take a look at the option 'same' for conv2.
>
> I've not come across convolutions at all (and the kernel reference also goes over my head) but I appreciate the advice. I've brought up the document you pointed me towards and I'll have a good read of it now. Thanks for the input.

Hi,

Try looking up 2d discrete convolutions and if you have time also take a look at image filters. The mathematics might be intimidating but if you find a good tutorial/document it changes everything.

Remember, this is just one way to do it. If it doesn't fit your needs don't spend too much time on it.
Regards,
Florin

Subject: Iterative solution from two 8x8 Matrices (please help a novice!)

From: Reese

Date: 25 Mar, 2011 21:49:05

Message: 5 of 10

Well depending on your thermal diffusivity and dimensions you may need to refine your grid because only 8 points seems an order of magnitude too few at least, if this is just for the example then you can disregard that part.

For your boundary condtions it seems like dirchlet conditions all around except for a symmetry condition at the center of the duct. The interface between the duct and ground I am assuming to be a conduction-condution interface

You could set this up a few different ways.
First you could set up a simple explicit itterative method looping through your y indices (I will use j) and x indicies (I will use i).

for j=1:jmax
  for i=1:imax
     if BC
         code to handle boundary conditions
     else
         code to handle interior
     end
  end
end
     
another option is to set up and implicit method and use a standard solver to solve the system (this looks to be the job for a block diagonal or SOR method)

It is hard to suggest a specific method that would work best without more information about the problem. It would be helpful to state the (hopefully) well posed PDE.

Subject: Iterative solution from two 8x8 Matrices (please help a novice!)

From: Grufff

Date: 26 Mar, 2011 12:24:04

Message: 6 of 10

Hello Reese, thanks for your comments.

Yes, I agree the resolution of the grid needs to be increased, but as I'm new to both this kind of heat problem and matlab I wanted to keep things simple until I'd made a little bit of progress. I've got a rudimentary script going and it converges neatly so I feel more confident now to try and and refine it some more.

The problem does indicate a symmetry around the (vertical) centre line of the duct. I hadn't heard the term "Dirichlet" when referring to BC's before, so it was interesting reading a little about that, thanks. I've not been given a specific PDE for the scenario, just temperatures and dimensions etc, and an instruction to determine temperatures using central differencing. I'm only just starting down this road so the problems are no doubt far less sophisticated than the wiser heads here in the group are more used to tackling. I'll include the question I'm working from below, in case you wish to see it.

Block diagonal and SOR methods don't mean anything to me at the moment so I'll leave those aside for now, but your suggestion for nesting loops and incrementing up through x and y indices makes sense. I'm not sure yet how to do that but I'll have a go. My problem will be writing the code to more elegantly deal with the repeating calculations than the clumsy longhand my first attempt uses.

As you suggest, set variables for row and column, then increment and decrement as needed and loop somehow. I'll have a bash at that but would be very grateful if anyone has suggestions to set me going.

I'll post my first effort below (I hope it formats okay here in the group, sorry if it comes out badly). I'm a bit embarrassed about how clumsy and long handed it is, but we all have to start somewhere, right?

% Define matrix A to represent given temperatures and initial estimates,
% and define "working" matrix B

A=[4 4 4 4 4 4 4 4;10 10 10 10 10 10 10 1;+...
    10 10 10 10 10 10 10 -2;60 60 10 10 10 10 10 -5;+...
    60 60 10 10 10 10 10 -8;10 10 10 10 10 10 10 -11;+...
    10 10 10 10 10 10 10 -14;-17 -17 -17 -17 -17 -17 -17 -17];

B=A;

% Number of iterations, n

n=50;

% Begin for loop

for i=1:n;
 
 % row 2
 
 B(2,2)=(A(1,2)+A(3,2)+A(2,1)+A(2,3))/4;
 B(2,3)=(A(1,3)+A(3,3)+A(2,2)+A(2,4))/4;
 B(2,4)=(A(1,4)+A(3,4)+A(2,3)+A(2,5))/4;
 B(2,5)=(A(1,5)+A(3,5)+A(2,4)+A(2,6))/4;
 B(2,6)=(A(1,6)+A(3,6)+A(2,5)+A(2,7))/4;
 B(2,7)=(A(1,7)+A(3,7)+A(2,6)+A(2,8))/4;
 
 B(2,1)=B(2,2);
 
 % row 3
 
 B(3,2)=(A(2,2)+A(4,2)+A(3,1)+A(3,3))/4;
 B(3,3)=(A(2,3)+A(4,3)+A(3,2)+A(3,4))/4;
 B(3,4)=(A(2,4)+A(4,4)+A(3,3)+A(3,5))/4;
 B(3,5)=(A(2,5)+A(4,5)+A(3,4)+A(3,6))/4;
 B(3,6)=(A(2,6)+A(4,6)+A(3,5)+A(3,7))/4;
 B(3,7)=(A(2,7)+A(4,7)+A(3,6)+A(3,8))/4;
 
 B(3,1)=B(3,2);
 
 % row 4
 
 B(4,3)=(A(3,3)+A(5,3)+A(4,2)+A(4,4))/4;
 B(4,4)=(A(3,4)+A(5,4)+A(4,3)+A(4,5))/4;
 B(4,5)=(A(3,5)+A(5,5)+A(4,4)+A(4,6))/4;
 B(4,6)=(A(3,6)+A(5,6)+A(4,5)+A(4,7))/4;
 B(4,7)=(A(3,7)+A(5,7)+A(4,6)+A(4,8))/4;
 
 % row 5
 
 B(5,3)=(A(4,3)+A(6,3)+A(5,2)+A(5,4))/4;
 B(5,4)=(A(4,4)+A(6,4)+A(5,3)+A(5,5))/4;
 B(5,5)=(A(4,5)+A(6,5)+A(5,4)+A(5,6))/4;
 B(5,6)=(A(4,6)+A(6,6)+A(5,5)+A(5,7))/4;
 B(5,7)=(A(4,7)+A(6,7)+A(5,6)+A(5,8))/4;
 
 % row 6
 
 B(6,2)=(A(5,2)+A(7,2)+A(6,1)+A(6,3))/4;
 B(6,3)=(A(5,3)+A(7,3)+A(6,2)+A(6,4))/4;
 B(6,4)=(A(5,4)+A(7,4)+A(6,3)+A(6,5))/4;
 B(6,5)=(A(5,5)+A(7,5)+A(6,4)+A(6,6))/4;
 B(6,6)=(A(5,6)+A(7,6)+A(6,5)+A(6,7))/4;
 B(6,7)=(A(5,7)+A(7,7)+A(6,6)+A(6,8))/4;
 
 B(6,1)=B(6,2);
 
 % row 7
 
 B(7,2)=(A(6,2)+A(8,2)+A(7,1)+A(7,3))/4;
 B(7,3)=(A(6,3)+A(8,3)+A(7,2)+A(7,4))/4;
 B(7,4)=(A(6,4)+A(8,4)+A(7,3)+A(7,5))/4;
 B(7,5)=(A(6,5)+A(8,5)+A(7,4)+A(7,6))/4;
 B(7,6)=(A(6,6)+A(8,6)+A(7,5)+A(7,7))/4;
 B(7,7)=(A(6,7)+A(8,7)+A(7,6)+A(7,8))/4;
 
 B(7,1)=B(7,2);
 
 A=B;
 
end;

% display final matrix

A

The question I'm working from is here:

http://img402.imageshack.us/f/temperaturegridquestion.jpg/

If anyone has further tips or advice to help me improve this and increase the resolution of my grid I'd be very grateful.

Thanks again for your helpful comments Reese.

Subject: Iterative solution from two 8x8 Matrices (please help a novice!)

From: Grufff

Date: 27 Mar, 2011 10:22:04

Message: 7 of 10

"Grufff" wrote in message <imklt4$h2k$1@fred.mathworks.com>...

>suggestion for nesting loops and incrementing up through x and y indices makes sense. I'm not sure yet how to do that but I'll have a go. My problem will be writing the code to more elegantly deal with the repeating calculations than the clumsy longhand my first attempt uses

Sorry to reply to my own message, but I'm making progress! I've cut out lots of the unnecessary long hand / manual steps and replaced with additional nested loops. Script is now much nicer (but still a long way to go).

I now need to increase the resolution of the grid and so scale everything up. I'll still be very glad of advice or tips on my efforts.

% Define Matrix T and T2 (identical)

T=[4 4 4 4 4 4 4 4;10 10 10 10 10 10 10 1;+...
    10 10 10 10 10 10 10 -2;60 60 10 10 10 10 10 -5;+...
    60 60 10 10 10 10 10 -8;10 10 10 10 10 10 10 -11;+...
    10 10 10 10 10 10 10 -14;-17 -17 -17 -17 -17 -17 -17 -17];

T2=T;

% Number of iterations, n

n=50;

% code loops

for iteration=1:n
    
    for j=2:3
        
        for i=2:7
            
            T2(j,i)=((T(j-1,i)+T(j+1,i)+T(j,i-1)+T(j,i+1))/4);
            
        end
        
    end
    
    for j=4:5
        
        for i=3:7
            
            T2(j,i)=((T(j-1,i)+T(j+1,i)+T(j,i-1)+T(j,i+1))/4);
        end
        
    end
    
    for j=6:7
        
        for i=2:7
            
            T2(j,i)=((T(j-1,i)+T(j+1,i)+T(j,i-1)+T(j,i+1))/4);
        end
        
    end
    
    % set 1st column identical to second column
    
    for j=1:8
        
        T2(j,1)=T(j,2);
    end
    
    T=T2;
    
end

T

Subject: Iterative solution from two 8x8 Matrices (please help a novice!)

From: ImageAnalyst

Date: 27 Mar, 2011 13:03:53

Message: 8 of 10

On Mar 27, 6:22 am, "Grufff " <gruff.mat...@mailinator.com> wrote:
> I now need to increase the resolution of the grid and so scale everything up. I'll still be very glad of advice or tips on my efforts.
[code snipped]
-------------------------------------------------
If you have the Image Processing Toolbox, simply use imresize().

Subject: Iterative solution from two 8x8 Matrices (please help a novice!)

From: Grufff

Date: 27 Mar, 2011 14:10:04

Message: 9 of 10

ImageAnalyst <imageanalyst@mailinator.com> wrote

> If you have the Image Processing Toolbox, simply use imresize().

I'm not sure what toolboxes come with my version, I haven't explored that yet. I'll have a look, thank you.

I've scaled it up manually anyway, it was straightforward even for me, as the script is so simple anyway. I now have a 42x42 matrix, giving me much smaller node size. Have neatened up the generation of my initial matrix too.

Subject: Iterative solution from two 8x8 Matrices (please help a novice!)

From: ImageAnalyst

Date: 27 Mar, 2011 14:43:37

Message: 10 of 10

To find out what toolboxes you have issue the "ver" command in the
command window. If you have the Image Processing Toolbox, I recommend
you use it, for a single line solution rather than the complicated
dozens of lines you have.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us