Create a 1D row vector (5,1) with middle elements = T.
显示 更早的评论

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 个)
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
2022-6-28
Ellie Matlab
2022-6-28
Karim
2022-6-28
i updated the answer so that it works with as many grid points as you want
Ellie Matlab
2022-6-28
编辑:Ellie Matlab
2022-6-28
so you want to interpolate in the results?
in that case you can do somethinge like:
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(N,1); b(1,1) = -40; b(3,1) = -20;
T=A\b;
x_sol = linspace(0,0.8,5);
Temperature = ones(5,1);
Temperature(1,1)=40;
Temperature(2:end-1)=T; % <-- here you had a typo, you used 'n' instead of 'end'
Temperature(end,1)=20;
% interpolate for more points
x_fine = linspace(0,0.8,100);
temp_fine = interp1(x_sol,Temperature,x_fine);
figure
plot(x_fine,temp_fine)
title('Temperature distribution between x=0 and x=0.8 ')
xlabel('x')
ylabel('Temperature')
Ellie Matlab
2022-6-28
Ellie Matlab
2022-6-28
编辑:Ellie Matlab
2022-6-28
Karim
2022-6-28
Try not to change your question so many times, it is very time demanding to answer the same thing many times over with a small varient in the question. Add all information up front and do not add it later assuming that everyone knows what you want to acchieve.
Anyhow, i updated the answer to match the changes.
Ellie Matlab
2022-6-28
by transposing the data:
A = rand(100,1)
A = transpose(A)
Ellie Matlab
2022-6-28
编辑:Ellie Matlab
2022-6-28
Karim
2022-6-28
you have to transpose the variable "Temperature" not "A"...
Try to think on what you want to achieve... if "Temperature" is the wrong size then why would you transpose "A"?
Ellie Matlab
2022-6-28
Karim
2022-6-28
to be honest at this point i do not know if you are serious...
now you have overwritten "A" as the transpose of "Temperature"... why would you do this?
if your goal is to change the size of "Temperature" why to you write it to the variable "A"? Do you know the meaning/understand the = symbol?
offcourse it should be
Temperature = transpose(Temperature);
otherwise you haven't done anything... please try to think a bit.
Ellie Matlab
2022-6-28
Ellie Matlab
2022-6-28
类别
在 帮助中心 和 File Exchange 中查找有关 Mathematics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




