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!