How to vectorize this code and make it efficient?
3 views (last 30 days)
Show older comments
So I am working on a 2D grid Finite Difference Method problem on steady heat conduction where I need to create one equation for each node. In terms of 2D Cartestian coordinates (x,y), my domain is rectangular and bounded by (0,0), (0.5,0), (0,1) and (0.5,1). Hence a rectangle with x length along x half of length along y.
I have N nodes along x and 2*N-1 nodes along y to get a uniformly spaced grid. My main idea is to first write the Finite Difference Method equations as symbolic equations in MATLAB and then let MATLAB form the matrix equation that can be further solved. You may have a better alternative to writing symbolic equations but please bear with me on this one.
My code is given below. I have used for loops as I used to do in C and C++. But recently I found out that MATLAB is not efficient when it comes to large loops. So basically I wanted to know how to convert the below given loops to "vectorized form" so that these for loops are not needed. I tried to go through vectorized code examples but I am unable to convert my code into vectorized form.
X axis has been discretized with variable 'i' - i=1,2,3,....N.
Y axis has been discretized with variable 'j' - j=1,2,3,....,2N-1.
The code becomes slow for large values of N. I am trying to optimize the code so that I may be able to work with larger grid size for accurate FDM results.
Thank You in advance!
N=3;%3x5 grid desired
del = 0.5/(N-1);%Equal spacing between nodes along x and y directions
x = 0:del:0.5;%Making nodes and discretizing x=0 to 0.5 into N nodes
y = 0:del:1;%Making nodes and discretizing y=0 to 1.0 into 2N-1 nodes
Total_Points = N*(2*N-1)
T = sym('T',[N,2*N-1]);
%Symbolic array to store temperature of each point
%The symbolic equations will be linear in T_11,T_12, etc.
EQNs = sym('Eqn',[N,(2*N-1)]);
%{
EQNs matrix will store the symbolic equation for every single point
For example EQNs(3,4) will store the equation coresponding to the point (3,4)
%}
%Equations for all interior points
for i = 2:N-1
for j = 2:2*N-2
EQNs(i,j) = T(i+1,j) + T(i,j+1) - 4*T(i,j) + T(i,j-1) + T(i-1,j) == 0;
end
end
%Lower most wall : i=1 to N with j=1 fixed
for i = 1:N
EQNs(i,1) = T(i,1)==100*sin(pi*x(i));%N equations for N points
end
%Left wall : i=1 fixed and j=2 to 2N-2
for j = 2:(2*N-2)
EQNs(1,j) = 2*T(2,j) + T(1,j+1) - 4*T(1,j) + T(1,j-1) == 0;
end
%Top wall : j=2N-1 fixed and x = 2 to N-1
for i = 2:N-1
EQNs(i,2*N-1) = T(i+1,2*N-1) - 4*T(i,2*N-1) + 2*T(i,2*N-2) + T(i-1,2*N-1) == 0;
end
%Right wall : i=N fixed and j = 2 to 2N-2
for j = 2:2*N-2
EQNs(N,j) = T(N,j+1) - 4*T(N,j) + T(N,j-1) + 2*T(N-1,j) == 0;
end
EQNs(1,2*N-1) = 2*T(2,2*N-1) - 4*T(1,2*N-1) + 2*T(1,2*N-2) == 0;
EQNs(N,2*N-1) = 2*T(N,2*N-2) - 4*T(N,2*N-1) + 2*T(N-1,2*N-1) == 0;
%Special Equations for left and right corner points
3 Comments
Answers (1)
Matt J
on 26 Feb 2022
Edited: Matt J
on 26 Feb 2022
Perhaps with pcg()?
N=3;%3x5 grid desired
del = 0.5/(N-1);%Equal spacing between nodes along x and y directions
x = 0:del:0.5;%Making nodes and discretizing x=0 to 0.5 into N nodes
[m,n]=deal(N,2*N-1);
b=zeros(m,n+1); b(:,1)=100*sin(pi*x(:));
I=[2,1:m,m-1];
J=[1:n,n-1];
k=[0 1 0;
1 -4 1;
0 1 0];
A=@(T) [T(:,1) conv2(T(I,J),k,'valid')];
T=pcg(A,b);
0 Comments
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!