Line 36. Balancing the plot vectors. 'Error using plot Vectors must be the same length.'
1 回表示 (過去 30 日間)
古いコメントを表示
T1=40;
T0=20;
a=10e-5;
L=0.1;
n=101; %No. of nodes
t=100;
dt=0.1;
time=0:dt:L;
dx=L/(n-1);
x=0:dx:L;
X0=T0*ones(n,1);
X0(1,1)=T1;
X(:,1)=X0;
d=ones(1,n);
for i=2:n-1
d(1,i)=2+(dx^2/(a*dt));
end
dd=-ones(1,n-1);
A=diag(d)+diag(dd,1)+diag(dd,-1);
A(1,2)=0;
A(n,n-1)=0;
for i=2:1+t/dt
b=(dx^2/(a*dt))*X0;
b(1,1)=T1;
b(n,1)=T0;
X(:,i)=inv(A)*b;
X0=X(:,i);
end
m=(n+1)/2;
figure();
plot (time,X(m,:),'Linewidth',3)
hold on
title('Transient temp distribution at center of the rod')
legend('Temp at the center of rod')
xlabel('Time,t(s)');
ylabel('Temperature,T(^oC)');
ylim([20 32])
0 件のコメント
回答 (1 件)
Walter Roberson
2022 年 11 月 30 日
for i=2:1+t/dt
t is 100 and dt is 0.1. You are expecting 100/0.1 to be exactly 1000. However 0.1 cannot be exactly represented in any finite length binary floating point representation, for exactly the same mathematical reason why 0.3333333.....3 can never exactly represent 1/3 in decimal no matter how many (finite) number of 3 you use. So 100/0.1 does not exactly equal an integer, and the : operator makes no attempt to round the number.
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!