Plotting a 2D "Time Snapshot"

4 vues (au cours des 30 derniers jours)
Matthew Osler
Matthew Osler le 29 Avr 2019
Commenté : Matthew Osler le 29 Avr 2019
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 commentaires
Matthew Osler
Matthew Osler le 29 Avr 2019
% 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 le 29 Avr 2019
Sorry about that... getting used to using this forum.

Connectez-vous pour commenter.

Réponse acceptée

KSSV
KSSV le 29 Avr 2019
Modifié(e) : KSSV le 29 Avr 2019
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 commentaire
Matthew Osler
Matthew Osler le 29 Avr 2019
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Tags

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by