Graphs Not Showing Up

1 次查看(过去 30 天)
Kevin Lee
Kevin Lee 2016-10-17
clear all;
close all;
clc;
x = linspace(0,2);
for t = [0 1 2 3 4]
n = 2;
Cn = 32;
Dn = 0;
u2 = (sin((n*pi*x)/2))*(Cn*cos(5*n*pi*t)+Dn*sin(5*n*pi*t));
n = 4;
Cn = 32;
Dn = 0;
u4 = (sin((n*pi*x)/2))*(Cn*cos(5*n*pi*t)+Dn*sin(5*n*pi*t));
n = 5;
Cn = 32;
Dn = 0;
u5 = (sin((n*pi*x)/2))*(Cn*cos(5*n*pi*t)+Dn*sin(5*n*pi*t));
n = 6;
Cn = 32;
Dn = 0;
u6 = (sin((n*pi*x)/2))*(Cn*cos(5*n*pi*t)+Dn*sin(5*n*pi*t));
n = 12;
Cn = 32;
Dn = 0;
u12 = (sin((n*pi*x)/2))*(Cn*cos(5*n*pi*t)+Dn*sin(5*n*pi*t));
u = u2 + u4 + u5 + u6 + u12
plot(x,u)
hold on
grid on
legend('0', '1', '2', '3', '4')
title('u(x,t)')
xlabel('x')
ylabel('u(x,t)')
end
This is my code to plot multiple graphs for a PDE, however no graphs actually show up for t = 0, 1 and 2. What's weird though is when I just have
t = 0
the plot comes up. Would anyone be able to help me as to why it is not working when trying to plot for multiple t?

回答(2 个)

KSSV
KSSV 2016-10-17
It is showing up for all the time steps....But it is taking same values. so you are able to see only few curves. Check the below code:
clear all;
close all;
clc;
x = linspace(0,2);
mark = {'*r','.k','Og','dc','sm'} ;
T = [0 1 2 3 4] ;
for i = 1:length(T)
t = T(i) ;
n = 2;
Cn = 32;
Dn = 0;
u2 = (sin((n*pi*x)/2))*(Cn*cos(5*n*pi*t)+Dn*sin(5*n*pi*t));
n = 4;
Cn = 32;
Dn = 0;
u4 = (sin((n*pi*x)/2))*(Cn*cos(5*n*pi*t)+Dn*sin(5*n*pi*t));
n = 5;
Cn = 32;
Dn = 0;
u5 = (sin((n*pi*x)/2))*(Cn*cos(5*n*pi*t)+Dn*sin(5*n*pi*t));
n = 6;
Cn = 32;
Dn = 0;
u6 = (sin((n*pi*x)/2))*(Cn*cos(5*n*pi*t)+Dn*sin(5*n*pi*t));
n = 12;
Cn = 32;
Dn = 0;
u12 = (sin((n*pi*x)/2))*(Cn*cos(5*n*pi*t)+Dn*sin(5*n*pi*t));
u = u2 + u4 + u5 + u6 + u12 ;
plot(x,u,mark{i})
hold on
grid on
legend('0', '1', '2', '3', '4')
title('u(x,t)')
xlabel('x')
ylabel('u(x,t)')
end

Massimo Zanetti
Massimo Zanetti 2016-10-17
编辑:Massimo Zanetti 2016-10-17
You just see the last two ('3' and '4') because the values are the same, so curve are superimposed and you just see the last one which is printed. A comment about your code, put legend and labels outside the for loop:
for t = [0 1 2 3 4]
%...... your code
plot(x,u)
hold on
end
grid on
legend('0', '1', '2', '3', '4')
title('u(x,t)')
xlabel('x')
ylabel('u(x,t)')
hold off
and add hold off

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by