Graphs Not Showing Up

1 回表示 (過去 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 Center および File Exchange2-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