Plotting a 2D "Time Snapshot"

조회 수: 4 (최근 30일)
Matthew Osler
Matthew Osler 2019년 4월 29일
댓글: Matthew Osler 2019년 4월 29일
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
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일
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개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by