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

조회 수: 4 (최근 30일)
Matthew Palermo
Matthew Palermo 2023년 11월 14일
댓글: Jon 2023년 11월 14일
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
KALYAN ACHARJYA 2023년 11월 14일
편집: KALYAN ACHARJYA 2023년 11월 14일
Please verify, as TX10Bexp0(ix, it) reflects only NaN values

댓글을 달려면 로그인하십시오.

답변 (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