NaN error fills up my zeros array despite the hand calcs checking out.
3 次查看(过去 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 个评论
KALYAN ACHARJYA
2023-11-14
编辑:KALYAN ACHARJYA
2023-11-14
Please verify, as TX10Bexp0(ix, it) reflects only NaN values
回答(1 个)
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
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 个评论
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 Center 和 File Exchange 中查找有关 Gamma Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!