이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
Create a 1D row vector (5,1) with middle elements = T.
조회 수: 2 (최근 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')
참고 항목
카테고리
Help Center 및 File Exchange에서 Signal Operations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 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 (한국어)