Create a 1D row vector (5,1) with middle elements = T.

조회 수: 2 (최근 30일)
Ellie Matlab
Ellie Matlab 2022년 6월 28일
댓글: Ellie Matlab 2022년 6월 28일
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')

답변 (1개)

Karim
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
Ellie Matlab
Ellie Matlab 2022년 6월 28일
ah! i got it! I need to change the name...
Ellie Matlab
Ellie Matlab 2022년 6월 28일
are you able to help me with adding an extra term to the vector b? This is my codes. But i failed the requirements, to my understanding as long i declare a varibale i sould be able to use it.
N = 3;
a1 =-2;
a2 = 1;
a3 = 1;
w=200000;
k=80.2;
deltax= 0.8/5-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-(w/k)*deltax^2; b(3,1) = -20-(w/k)*deltax^2; b(2,1)=-(w/k)*deltax^2;
T=A\b;
x=linspace(0,0.8,5);
Temperature = ones(1,5);
Temperature(1,1)=40;
Temperature(1,5)=20;
Temperature(2:end-1)=T;
Temperature = transpose(Temperature);
plot(x,Temperature)
title('Temperature distribution between x=0 and x=0.8 ')
xlabel('x')
ylabel('Temperature')

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

카테고리

Help CenterFile Exchange에서 Signal Operations에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by