You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Create a 1D row vector (5,1) with middle elements = T.
1 view (last 30 days)
Show older comments
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')
Answers (1)
Karim
on 28 Jun 2022
Edited: Karim
on 28 Jun 2022
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 Comments
Ellie Matlab
on 28 Jun 2022
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')
Ellie Matlab
on 28 Jun 2022
Edited: Ellie Matlab
on 28 Jun 2022
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).
Karim
on 28 Jun 2022
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
on 28 Jun 2022
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.
Ellie Matlab
on 28 Jun 2022
Edited: Ellie Matlab
on 28 Jun 2022
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')
Karim
on 28 Jun 2022
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
on 28 Jun 2022
how do i change the variable Temperaure to size [1 100] instead of the current [100 1]?
Karim
on 28 Jun 2022
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
Ellie Matlab
on 28 Jun 2022
Edited: Ellie Matlab
on 28 Jun 2022
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')
Karim
on 28 Jun 2022
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
on 28 Jun 2022
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);
Karim
on 28 Jun 2022
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
on 28 Jun 2022
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')
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)