code is right but can not the a plot

1 visualización (últimos 30 días)
Cesar Cardenas
Cesar Cardenas el 5 de Mzo. de 2023
Comentada: Torsten el 5 de Mzo. de 2023
Hello, I do not what the mistake is? I'm getting the first plot. However, I have not been able to get the second plot. Not sure how to fix it. As alwyas any help will be greatly appreciated. Thanks
clear
clc
L = 1-0;
T = 0.06-0;
alpha = 1;
dx = 0.05;
dt = 0.001;
%Q = (alpha.*dt)/(dx.^2);
Q = 0.25;
N = L/dx +1;
M = T/dt + 1;
x = zeros(N,1);
t = zeros(M,1);
for i=1:N
x(i) = 0+(i-1).*dx;
end
for n=1:M
t(n) = 0+(n-1).*dt;
end
u = zeros(M,N);
u(:,1) = 2;
u(:,N) = 1;
u(1,2:N-1) = sin(pi.*x(2:N-1));
for n=1:M
for i=2:N-1
u(n+1,i) = Q.*u(n,i+1) + (1-2*Q).*u(n,i) + Q.*u(n,i-1)
end
end
figure(1)
plot (x,u(M,:)) %plot x vs u
title ('x vs u at t = 0.06')
xlabel ('x')
ylabel ('T')
grid on
figure(2)
plot(t, u(:,N-1)) %plot t vs u
title ('t vs u at x = 0.5')
xlabel ('time')
ylabel ('T')
  1 comentario
Cesar Cardenas
Cesar Cardenas el 5 de Mzo. de 2023
thank you. could you show me the lines where the problem is? I did not write the whole code, I just added a couple of lines to plot. Thank you.

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 5 de Mzo. de 2023
Editada: Star Strider el 5 de Mzo. de 2023
You need to redefine ‘t’ so that it matches the row size of ‘u’:
t = linspace(0, 2.5, size(u,1));
Try this —
% clear
% clc
L = 1-0;
T = 0.06-0;
alpha = 1;
dx = 0.05;
dt = 0.001;
%Q = (alpha.*dt)/(dx.^2);
Q = 0.25;
N = L/dx +1;
M = T/dt + 1;
%x = zeros(N,1);
x = linspace(0, 2.5, 5);
% t = linspace(0, 2.5, 5);
%t = zeros(M,1);
for i=1:N
x(i) = 0+(i-1).*dx;
end
for n=1:M
t(n) = 0+(n-1).*dt;
end
u = zeros(M,N);
u(:,1) = 2;
u(:,N) = 1;
u(1,2:N-1) = sin(pi.*x(2:N-1));
for n=1:M
for i=2:N-1
u(n+1,i) = Q.*u(n,i+1) + (1-2*Q).*u(n,i) + Q.*u(n,i-1);
end
end
figure(1)
plot (x,u(M,:)) %plot x vs u
title ('x vs u at t = 0.06')
xlabel ('x')
ylabel ('T')
grid on
t = linspace(0, 2.5, size(u,1)); % Redefine 't' To Match 'u'
figure(2)
plot(t, u(:,N-1)) %plot t vs u
title ('t vs u at x = 0.5')
xlabel ('time')
ylabel ('T')
EDIT — Forgot to re-run code before posting. Now run.
.
  1 comentario
Torsten
Torsten el 5 de Mzo. de 2023
By changing t before plotting, u(:,N-1) is plotted at the wrong times.

Iniciar sesión para comentar.

Más respuestas (1)

Torsten
Torsten el 5 de Mzo. de 2023
Editada: Torsten el 5 de Mzo. de 2023
If you compare size(t) and size(u,1), you will find that the number of elements of both vectors are not the same (they differ by one element). Thus the second plot command errors.
Replace
for n=1:M
for i=2:N-1
u(n+1,i) = Q.*u(n,i+1) + (1-2*Q).*u(n,i) + Q.*u(n,i-1)
end
end
by
for n=1:M-1
for i=2:N-1
u(n+1,i) = Q.*u(n,i+1) + (1-2*Q).*u(n,i) + Q.*u(n,i-1)
end
end

Categorías

Más información sobre Geographic Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by