Graphs Not Showing Up

1 vue (au cours des 30 derniers jours)
Kevin Lee
Kevin Lee le 17 Oct 2016
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?

Réponses (2)

KSSV
KSSV le 17 Oct 2016
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 le 17 Oct 2016
Modifié(e) : Massimo Zanetti le 17 Oct 2016
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

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by