Hello. I am having trouble plotting a 2D "time snapshot" of a solution to a parabolic heat equation. The mesh plot seen near the bottom of my code works like a charm. However, I am supposed to try and plot a 2D "time snapshot" at time t = 20, so essentially just taking a slice of my 3D mesh. I am unsure how to do this. I have tried numerous things and nothing seems to be working. One of my classmates was able to use plot(:,401); to get his to work, where 401 is the final n iterate. This does not seem to work for me though. I get an error about an undefined function or variable with plot. From what I can tell, it does not like the semi-colon. I am not sure why this is a problem for me and not him, as our code is essentially the same. If anyone is able to show me how to make this 2D plot I would greatly appreciate it. A photo of my code is below:
Screen Shot 2019-04-28 at 10.45.05 PM.png

3 件のコメント

KSSV
KSSV 2019 年 4 月 29 日
Copy your code here....not a image.
Matthew Osler
Matthew Osler 2019 年 4 月 29 日
% This code solves the Parabolic Heat Equation
L = 2*pi; % length of metal rod
T = 20; % length of time interval
c = 1; % diffusion coefficient
m = 20; % number of steps in spatial grid
n = 400; % number of steps in temporal grid
h = L/m; % spatial step size
k = T/n; % temporal step size
x = 0:h:L; % initialize spatial vector
t = 0:k:T; % initialize temporal vector
f = -sin(x/4); % temperature distribution at time t=0
g1 = sin(t); % left endpoint boundary condition
g2 = 0; % right endpoint boundary condition
% initialize solution matrix u(x,t)
u = zeros(m+1,n+1);
u(:,1) = f;
u(1,:) = g1;
u(m+1,:) = g2;
r = (c*k)/(h^2); % constant coefficient
[X,T] = meshgrid(x,t); % define function q(x,t)
q = 10*X.^(-2)+T.^(-2); % function q(x,t)
Q = q.'; % orient matrix Q same as matrix u
% solve the parabolic heat equation
for jj = 1:n
for ii = 2:m
u(ii,jj+1)=r*u(ii-1,jj)+(1-2*r)*u(ii,jj)+r*u(ii+1,jj)+k*Q(ii,jj+1);
end
end
figure(1)
mesh(x,t,u');
title('Metal Rod with Applied Heat Source')
xlabel('Length of Metal Rod')
ylabel('Time')
Matthew Osler
Matthew Osler 2019 年 4 月 29 日
Sorry about that... getting used to using this forum.

サインインしてコメントする。

 採用された回答

KSSV
KSSV 2019 年 4 月 29 日
編集済み: KSSV 2019 年 4 月 29 日

0 投票

plot(t,u(20,:))
The one you are trying:
plot(:,401); % this is wrong
YOu have not specified the variable there. It should be
plot(u(:,401))

1 件のコメント

Matthew Osler
Matthew Osler 2019 年 4 月 29 日
Thank you so much KSSV!
plot(u(:,401))
This was exactly what I needed. So Matlab essentially couldn't tell what variable to plot there with the way I was doing it, in a sense?
Also, I believe you answered another question I had on here a couple of months ago. I really appreciate your help! I just started learning Matlab this semester.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

製品

リリース

R2019a

タグ

質問済み:

2019 年 4 月 29 日

コメント済み:

2019 年 4 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by