Vectorize MATLAB Matrices in \mathbf{v} for Spherical Heat Equation

  • Context: MATLAB 
  • Thread starter Thread starter hunt_mat
  • Start date Start date
  • Tags Tags
    Matrices Vector
Click For Summary

Discussion Overview

The discussion revolves around vectorizing a computation in MATLAB related to the Green's function for the spherical heat equation. Participants explore methods to efficiently compute this function for multiple positions in space, aiming to avoid the inefficiencies of nested loops.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Debate/contested

Main Points Raised

  • One participant inquires about vectorizing a vector of matrices in MATLAB to compute a Green's function for multiple spatial positions.
  • Another participant suggests that if the matrices are of the same size, they could be represented as a 3-dimensional matrix, which might facilitate vectorization.
  • Some participants propose using functions like ARRAYFUN and MESHGRID to handle multidimensional calculations and generate combinations of input arguments.
  • There is a discussion about the structure of the Green's function and how to compute it efficiently, with one participant noting the potential for vectorization using element-wise operations and summation over specific dimensions.
  • One participant expresses uncertainty about whether vectorization is feasible, suggesting that for loops may still be necessary.
  • A participant shares their experimental approach using ndgrid to create a grid of input values and discusses how to evaluate the Green's function using element-wise multiplication.
  • Another participant mentions confusion regarding the use of CONVN when dealing with multidimensional and one-dimensional arrays.

Areas of Agreement / Disagreement

Participants express differing views on the feasibility of vectorization, with some believing it is possible while others remain skeptical and suggest that for loops may be unavoidable. The discussion does not reach a consensus on the best approach.

Contextual Notes

Participants note the complexity of multidimensional calculations in MATLAB and the need for experimentation to determine the correct order of inputs and operations. There are unresolved questions about the specifics of implementing the convolution operation with CONVN.

hunt_mat
Homework Helper
Messages
1,816
Reaction score
33
TL;DR
Computing a Greens function in MATLAB.
Suppose I have a vector of matrices:
<br /> \mathbf{v}=(A_{1},\cdots,A_{n})<br />

How would I vectorise this in MATLAB?

This question comes from a requirement to compute a Greens function for the spherical heat equation. I can easily compute a single function for a single position in space, but can I do it for a vector of positions, for this I think I need a vector of Greens functions for each position.
 
Physics news on Phys.org
If the ##A##'s are all the same size then v could be a 3-dimensional matrix. That would be most likely to give you a path to vectorizing. It always takes me a lot of experimenting to get things right with multi-dimensional matrices in Matlab but it's pretty sweet when it works.

Otherwise v might have to be a cell array and I'm not sure what operations might be available to do what you want to do.

Does ARRAYFUN have what you need? It seems to handle a lot of different kinds of input objects.
 
So what I want is to compute the Greens function:
<br /> G(r,\bar{r},t)=\frac{3\bar{r}^{2}}{R^{3}}+\frac{2\bar{r}}{rR}\sum_{n=1}^{\infty}(1+\mu_{n}^{2})\sin\left(\frac{\mu_{n}\bar{r}}{R}\right)\sin\left(\frac{\mu_{n}r}{R}\right)e^{-\frac{D\mu_{n}^{2}t}{R^{2}}}<br />

From there I want to compute:

<br /> \int_{0}^{t}G(r,R,t-\tau)f(\tau)d\tau<br />

I could use a double loop no problem, but those tend to be rather slow and I wanted to vectorise it.

Mat
 
So your ##A##'s are slices of ##G(r,\bar r,t)## at different ##t## values? I think this is vectorizable with judicious use of MESHGRID to generate all the combinations of the input arguments, SUM over particular dimensions and CONVN to generate the convolution at the end.

As I said, I always have to do a lot of experimentation to get multidimensional calculations like this right. For instance what order to I want the inputs to MESHGRID? But I'm pretty sure those three functions have what you need to do this.

I will in fact do a little experimenting later for more specific guidance.
 
No, the A's are slices of G(r,\bar{r},t) at different r's. As I said, I can do it using a double loop, but it's slow like that, I wanted speed as I'll be using it lots and lots of times.

Mat
 
OK, I did a little experimenting as promised.

I started with this: [r, rbar, t, n] = ndgrid(r values, rbar values, t values, n values). You may want to use a different order of arguments.

You can then evaluate the expression under your summation sign using element-wise multiplication, i.e. B = sin(...r...) .* sin(...rbar...).*exp(...t...), etc.

Use SUM(B, 4) to sum over the index n, which is the 4th index for my example.

To finish evaluating the expression for G, you can access subsets of r and rbar for one value of n, i.e. r(:, :, :, 1) and rbar(:, :, :, 1).

That gives the entire 3-D array ##G(r, \bar r, t)## with ##r## being the first index, ##\bar r## being the second and ##t## being the third.

The convolution can then be done with CONVN, but I think you may need to build an array representing ##f(\tau)## which is the same shape as G. I'm a little confused from a quick reading on what exactly CONVN is doing when one argument is 3-dimensional and another is 1-dimensional.

But the bottom line is I'm 99% sure it can be vectorized in this way.
 
Okay, cheers. I'll play with that and see if it works.
 
I'll send you the transcript of my experiments if you're interested.
 
  • Like
Likes   Reactions: hunt_mat
Please. I've been ill with a very bad cold and haven't been able think straight but I'm better (not fully) to begin thinking again.
 
  • #10
I can't do this via vectorisation can I? It has to be for loops.
 
  • #11
This is the function I have:

[CODE lang="matlab" title="Greens function"]function G = Greens_2(D,mu_n,r,t,r_bar,n)
%This is the Green's function for spherical diffusion equation
%Solution can be found in A.D. Polyanin. The Handbook of Linear Partial Differential Equations
%for Engineers and Scientists, Chapman & Hall, CRC 17
n=length(mu_n);
N=1:n;
G_n=zeros(length(t),length(r},length(r_bar),n);
R=r_bar(end);
N=length(mu_n); %Number of terms used in Green's function series
N_r=length(r_bar);
for l=1:n
for k=1:N_r
for j=1:N_r
G_n(:,j,k,l)=(2*r_bar(k)/r(j)*R)*(1+(mu_n(l))^(-2))*sin(mu_n(l)*r_bar(k)/R)*sin(mu_n(l)*r(j)/R)*exp(-D*mu_{n}(l}^2*t/R^2);
end
end
end

G=sum(G_n,4)+3*r_bar.^2/R^3;[/CODE]
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 62 ·
3
Replies
62
Views
7K
Replies
3
Views
2K
Replies
15
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K