Improve Matlab Code Performance for Insulated Bar Internal Temperatures

In summary, the code calculates the internal temperatures of an insulated bar with left and right side boundary conditions of 350 K, with an initial temperature of 250 K. The user is trying to optimize their code and is looking for ways to improve the performance of the loops. The code uses a single FOR loop to assemble the coefficient matrix and the answer matrix for the interior nodes, and then computes and inserts the solved interior temperatures into the temperature matrix. Finally, the non-dimensionalized temperature matrix is plotted against distance for specific time intervals.
  • #1
roldy
237
2
This code calculates the internal temperatures of an insulated bar with left and right side boundary conditions of 350 K. The initial temperature of the bar is 250. This is not part of the assignment, but I want to get in the habit of optimizing my code. Is there any way to further improve the performance of the loops? For instance, instead of two FOR loops, combine them to make one FOR loop. I'm not looking for anything in terms of pre-allocating memory to arrays or matrices, that's basic. I'm just looking for other tips.

Basically what's going on here is that I'm solving for the interior temperatures at each time step using A\B. This is equivalent to solving these linear equations by hand using Gaussian elimination row operations. After solving for these temperatures. They are inserted into the main temperature matrix where the procedure is repeated. They are inserted into the main temperature matrix so that when the answer matrix, B, is calculated the temperatures can be pulled.

Code:
%AE 339 Homework 8, Part B


clear all
clc

delta_tau=0.01;
delta_zeta=0.05;

%delta_t=0.05;
%delta_x=0.05;

endtime=.35;
barlength=1;
int=barlength/delta_zeta;


x=0:delta_zeta:barlength;
time=delta_tau:delta_tau:endtime;


lambda=delta_tau/delta_zeta^2;


M=int+1;              %number of spatial nodes
N=length(time);     %number of time nodes

U=zeros(N,M);
U0=250;
U1=350;
U2=350;

U(:,1)=U1;
U(:,int+1)=U2;
U_0=U0*ones(1,int+1);

U=cat(1,U_0, U);

for n=1:N
    
%Assemble the coefficient matrix
D=2*(1+lambda)*ones(1,int-1);               %Main diagonal
Dlambda=-lambda*ones(1,int-2);              %Upper and lower diagonal
A=diag(D)+diag(Dlambda,1)+diag(Dlambda,-1);

%U(n,i)=[(U(n,i-1)+U(n,i+1)+U(n+1,i-1)+U(n+1,i+1))*lambda+2*(1-lambda)*U(n,i)]/(2*(1+lambda))

    %Assemble the answer matrix for the interior nodes i=2 to i=5
    for i=2:M-1     

        B(i-1,1)=[U(n,i-1)+U(n,i+1)+U(n+1,i-1)+U(n+1,i+1)]*lambda+2*(1-lambda)*U(n,i);

    end

%Compute the interior temperatures
C=A\B;                      
C=C';

    %Insert solved interior temperatures into the temperature matrix
    for k=1:length(C)

        U(n+1,k+1)=C(k);        

    end


%non-dimensionalize temperature matrix
    for y=1:n+1
        
        for s=1:int+1
            Theta(y,s)=(U(y,s)-U0)/(U1-U0);     
        end
        
    end

end

Theta2=Theta(2,:);      %Row corresponding to t=.1
Theta5=Theta(5,:);      %Row corresponding to t=.25
Theta7=Theta(7,:);      %Row corresponding to t=.35
 
plot(x,Theta2,x,Theta5,x,Theta7)

xlabel('Distance (non-dimensional)')
ylabel('Non-Dimensional Temperature (K)')
title('Non-Dimensional Plate Temperature vs. Distance along plate')
h = legend('time=0.1','time=0.25 sec','time=0.35 sec','location','Best');
set(h,'Interpreter','none')
 
Physics news on Phys.org
  • #2
Yes, there is a way to further improve the performance of the loops. Instead of using two FOR loops, you can combine them into one FOR loop. The combined FOR loop would look like this: for n=1:N %Assemble the coefficient matrix D=2*(1+lambda)*ones(1,int-1); %Main diagonal Dlambda=-lambda*ones(1,int-2); %Upper and lower diagonal A=diag(D)+diag(Dlambda,1)+diag(Dlambda,-1); %Assemble the answer matrix for the interior nodes i=2 to i=5 B=[]; for i=2:M-1 B(i-1,1)=[U(n,i-1)+U(n,i+1)+U(n+1,i-1)+U(n+1,i+1)]*lambda+2*(1-lambda)*U(n,i); end %Compute the interior temperatures C=A\B; C=C'; %Insert solved interior temperatures into the temperature matrix for k=1:length(C) U(n+1,k+1)=C(k); end %non-dimensionalize temperature matrix for s=1:int+1 Theta(n+1,s)=(U(n+1,s)-U0)/(U1-U0); endend
 

Related to Improve Matlab Code Performance for Insulated Bar Internal Temperatures

1. How can I improve the performance of my Matlab code for calculating internal temperatures of insulated bars?

There are several ways to improve the performance of your Matlab code for this task. One approach is to vectorize your code, which involves rewriting your calculations to operate on entire arrays instead of individual elements. This can greatly reduce the number of operations and improve the speed of your code. Additionally, you can use built-in functions and avoid loops whenever possible. Finally, optimizing your code by reducing unnecessary calculations and minimizing memory usage can also improve performance.

2. Are there any specific functions or techniques that can help improve the performance of my Matlab code?

Yes, there are several functions and techniques that can help improve the performance of your Matlab code. One useful function is tic and toc, which can be used to measure the execution time of different parts of your code. This can help you identify areas that need improvement. Other techniques include preallocating arrays, using parallel computing, and avoiding unnecessary memory copies.

3. How can I determine the bottleneck in my Matlab code's performance?

There are a few ways to determine the bottleneck in your Matlab code's performance. One method is to use the profile function, which provides information on the execution time of each line of code. This can help you identify which parts of your code are taking the longest to run. You can also use the tic and toc functions mentioned earlier to measure the execution time of specific sections of your code.

4. Can I use external libraries or toolboxes to improve the performance of my Matlab code?

Yes, you can use external libraries or toolboxes to improve the performance of your Matlab code. For example, the fft function in the Signal Processing Toolbox is often faster than the built-in fft function. However, it is important to carefully evaluate the performance benefits and compatibility of external libraries before incorporating them into your code.

5. How can I ensure that my code remains efficient as I make changes or add new features?

To ensure that your code remains efficient as you make changes or add new features, it is important to regularly test and profile your code. This will help you identify any areas that may be causing slowdowns and allow you to make necessary optimizations. Additionally, it can be helpful to document your code and regularly review and update it to maintain its efficiency.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
41
Views
8K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
10K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
997
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
2K
Back
Top