NaN error fills up my zeros array despite the hand calcs checking out.

4 次查看(过去 30 天)
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 个)

Jon
Jon 2023-11-14
编辑:Jon 2023-11-14
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
x_til = 0.1000
t_til = 0.1
t_til = 0.1000
sum2 = 0
sum2 = 0
gamma = 0.1
gamma = 0.1000
m = 1
m = 1
lambda = (2*(m-1))*pi/2
lambda = 0
sum2 = sum2 + sin(lambda*x_til)/lambda/(lambda^2-gamma)*(exp(-(lambda^2)*t_til)-exp(-gamma*t_til))
sum2 = NaN
  1 个评论
Jon
Jon 2023-11-14
Also, note that gamma is the gamma function in MATLAB, so you may want to use a different variable name to avoid confusion

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Gamma Functions 的更多信息

产品


版本

R2023b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by