現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
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')
回答 (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 件のコメント
Ellie Matlab
2022 年 6 月 28 日
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
2022 年 6 月 28 日
編集済み: Ellie Matlab
2022 年 6 月 28 日
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
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 日
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
2022 年 6 月 28 日
編集済み: Ellie Matlab
2022 年 6 月 28 日
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
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 日
how do i change the variable Temperaure to size [1 100] instead of the current [100 1]?
Karim
2022 年 6 月 28 日
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
2022 年 6 月 28 日
編集済み: Ellie Matlab
2022 年 6 月 28 日
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
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 日
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
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 日
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')
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
アジア太平洋地域
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)