Graphs Not Showing Up

조회 수: 2 (최근 30일)
Kevin Lee
Kevin Lee 2016년 10월 17일
편집: Massimo Zanetti 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에서 Legend에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by