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 个评论

yes! OMG thanks for finding my mistake!
If i want to change the gridpoints to 100 instead. When i modify the codes like this there is an error..
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,100);
Temperature = ones(1,101);
Temperature(1,1)=100;
Temperature(1,100)=0;
Temperature(2:end-2)=T;
plot(x,Temperature)
title('Temperature distribution between x=0 and x=0.8 ')
xlabel('x')
ylabel('Temperature')
i updated the answer so that it works with as many grid points as you want
I dont understand on changing the N to the number of gridpoints. I need N to be constant =3. but my linspace have to be exact (0,0.8,100).
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')
not really interpoling, but more of altering the previous codes of 4 gridpoints to 100 gridpoints instead. I tried 3 different sets of codes with one provide from you, but the results is still incorrect.
This is the current code that i have that fulfils most of the requirements. But there is still error on the variable T.
n=100;
%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)=100; b(n-2)=0;
T=zeros(n,1); % Pre-defining the temperature vector
T=A\b; % Use backslash operator
x=linspace(0,0.8,100); % Domain vector x
plot(x,T)
title('Temperature distribution between x=0 and x=0.8 ')
xlabel('x')
ylabel('Temperature')
Altering the previous code you provided,
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,100);
Temperature = ones(1,100);
Temperature(1,1)=100;
Temperature(1,100)=0;
Temperature(2:end-1)=T; %%Unable to perform assignment because the left and right sides have a different number of elements.
plot(x,Temperature)
title('Temperature distribution between x=0 and x=0.8 ')
xlabel('x')
ylabel('Temperature')
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.
how do i change the variable Temperaure to size [1 100] instead of the current [100 1]?
by transposing the data:
A = rand(100,1)
A = 100×1
0.6662 0.2808 0.9608 0.3905 0.3165 0.4323 0.5959 0.3109 0.4829 0.2172
A = transpose(A)
A = 1×100
0.6662 0.2808 0.9608 0.3905 0.3165 0.4323 0.5959 0.3109 0.4829 0.2172 0.8374 0.3507 0.0973 0.5586 0.7019 0.5457 0.2889 0.8119 0.2632 0.9979 0.1576 0.2243 0.4143 0.7989 0.7886 0.2565 0.3138 0.1508 0.3934 0.2843
This is after i transpose, but the size is still wrong and if i put it anywhere above the A\b, there will be an error.
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;
A = rand(100,1);
A = transpose(A);
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')
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"?
if i change to the below codes, its still wrong. i tried changing to (1,100) but to no avail.
A = rand(100,1);
A = transpose(Temperature);
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.
ah! i got it! I need to change the name...
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')

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Mathematics 的更多信息

产品

版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by