NaN error fills up my zeros array despite the hand calcs checking out.
    3 views (last 30 days)
  
       Show older comments
    
I am trying to plot the nondimensionalized time-dependent temperture distribution. The function checks out and the code works, except that my zeros array is being filled with NaN. I am not sure where the issue lies because the hand calculations return values.
Here is the code:
close all 
clear all
clc 
x_til = [0 : 0.2 : 1];
dt = 0.01;
tmax = 1;
t_til = [dt : dt : tmax];
nx = length(x_til);
nt = length(t_til);
gamma = 0.1;
TX10Bexp0 = zeros(nx,nt);
for ix = 1:nx
    for it = 1:nt
        TX10Bexp0(ix,it) = fX10Bexpo0T0(x_til(ix),t_til(it),gamma);
    end
end
figure
clf;
hold on 
for ix = 1:nx
    plot(t_til, TX10Bexp0(ix,:), 'LineWidth',3);
end
xlabel('dimensionless time')
ylabel('dimensionless temperature')
And here is the function:
function [T_til] = fX10Bexpo0T0 (x_til, t_til, gamma)
A = 6;
MAX = 1000;
mmax = ceil(sqrt(A*log(10)/t_til)/pi + 0.5);
if mmax > MAX
    mmax = MAX;
end
sum = 0;
sum2 = 0;
for m=1:mmax
    lambda = (2*(m-1))*pi/2;
    sum = sum + sin(lambda*x_til)/lambda*exp(-(lambda^2)*t_til);
    sum2 = sum2 + sin(lambda*x_til)/lambda/(lambda^2-gamma)*(exp(-(lambda^2)*t_til)-exp(-gamma*t_til));
end
T_til = exp(-gamma*t_til) - (2*sum) - (2*gamma*sum2);
end
1 Comment
  KALYAN ACHARJYA
      
      
 on 14 Nov 2023
				
      Edited: KALYAN ACHARJYA
      
      
 on 14 Nov 2023
  
			Please verify, as TX10Bexp0(ix, it) reflects only NaN values
Answers (1)
  Jon
      
 on 14 Nov 2023
        
      Edited: Jon
      
 on 14 Nov 2023
  
      The term lambda in the denominator of the expression for sum2 is zero when m = 1, so sum2 will be NaN and so will T_til
x_til = 0.1
t_til = 0.1
sum2 = 0
gamma = 0.1
m = 1
lambda = (2*(m-1))*pi/2
sum2 = sum2 + sin(lambda*x_til)/lambda/(lambda^2-gamma)*(exp(-(lambda^2)*t_til)-exp(-gamma*t_til))
1 Comment
  Jon
      
 on 14 Nov 2023
				Also, note that gamma is the gamma function in MATLAB, so you may want to use a different variable name to avoid confusion
See Also
Categories
				Find more on Creating and Concatenating Matrices 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!

