Create a 1D row vector (5,1) with middle elements = T.
1 次查看(过去 30 天)
显示 更早的评论
This is my code: I have problems with assigning the variable T to the 1D row vector.
N = 3;
a1=-2;
a2 = 1;
a3 = 1;
A = diag(a1*ones(1,N)) + diag(a2*ones(1,N-1),1) + diag(a3*ones(1,N-1),-1);
b = zeros(3,1); b(1,1) = -40; b(3,1) = -20;
T=A\b;
x=linspace(0,0.8,5);
Temperature= ones(5,1); Temperature(1,1)=40; Temperature (5,1)=20;
Temperature(2:n-1)=T;
plot(x,Temperature)
title('Temperature distribution between x=0 and x=0.8 ')
xlabel('x')
ylabel('Temperature')
0 个评论
回答(1 个)
Karim
2022-6-28
编辑:Karim
2022-6-28
I guess this was only a typographical error, where you inserted the variable 'T' into 'temperature', see below for the adjusted code:
n = 100; % gridpoints
T1 = 100; % temp T1
T2 = 0; % temp T2
%construct a tridiagonal matrix
A=2*eye(n);
A=A-diag(ones(n-1,1),1);
A=A-diag(ones(n-1,1),-1);
A(1,:)=0;
A(1,1)=1; %Since the temperature at node 1 is known
A(n,:)=0;
A(n,n)=1; %Since the temperature at node n is known
% Construct a vector b with known temperatures
b=zeros(n,1);
b(1)=T1;
b(end)=T2;
x = linspace(0,0.8,n);
Temperature = A\b;
T = Temperature(2:end-1); % extract the middle temperatues for the ex
figure
plot(x,Temperature)
title('Temperature distribution between x=0 and x=0.8 ')
xlabel('x')
ylabel('Temperature')
16 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!