Surf Question/ Data dimensions must agree
조회 수: 20 (최근 30일)
이전 댓글 표시
I am trying to create a dufort frankel scheme. This is a textbook code. I am having trouble repeatedly with line 71 error using surf, data dimensions must agree, then error in line 74 surf(x, tt, u'). I am unsure of what this means. Could someone please inform me of the error and how to correct in detail?
clear all; % clear all variables in memory
xl=0; xr=1; % x domain [xl,xr]
J = 10; % J: number of division for x
dx = (xr-xl) / J; % dx: mesh size
tf = 0.1; % final simulation time
Nt = 50; % Nt: number of time steps
dt = tf/Nt;
k = 0;
t = 0;
mu = dt/(dx)^2;
if mu > 0.5 % make sure dt satisy stability condition
error('mu should < 0.5!')
end
% Evaluate the initial conditions
x = xl : dx : xr; % generate the grid point
% f(1:J+1) since array index starts from 1
f = sin(pi*x) + sin(2*pi*x);
% store the solution at all grid points for all time steps
u = zeros(J+1,Nt+1);
% Find the approximate solution at each time step
for n = 1:Nt
t = n*dt; % current time
% boundary condition at left side
gl = sin(pi*xl)*exp(-pi*pi*t)+sin(2*pi*xl)*exp(-4*pi*pi*t);
% boundary condition at right side
gr = sin(pi*xr)*exp(-pi*pi*t)+sin(2*pi*xr)*exp(-4*pi*pi*t);
if n==1 % first time step
for j=2:J % interior nodes
u(j,n) = f(j) + mu*2*(f(j+1)-(f(j)+f(j))+f(j-1));
end
u(1,n) = gl; % the left-end point
u(J+1,n) = gr; % the right-end point
else
for j=2:J % interior nodes
u(j+1,n)=u(j,n-1)+mu*2*(u(j,n+1)-(u(j-1,n)+u(j+1,n))+u(j,n-1));
end
u(1,n) = gl; % the left-end point
u(J+1,n) = gr; % the right-end point
end
% calculate the analytic solution
for j=1:J+1
xj = xl + (j-1)*dx;
u_ex(j,n)=sin(pi*xj)*exp(-pi*pi*t) ...
+sin(2*pi*xj)*exp(-4*pi*pi*t);
end
end
% Plot the results
tt = dt : dt : Nt*dt;
figure(1)
colormap(gray); % draw gray figure
surf(x,tt, u'); % 3-D surface plot
xlabel('x')
ylabel('t')
zlabel('u')
title('Numerical solution of 1-D parabolic equation')
figure(2)
surf(x,tt, u_ex'); % 3-D surface plot
xlabel('x')
ylabel('t')
zlabel('u')
title('Analytic solution of 1-D parabolic equation')
maxerr=max(max(abs(u-u_ex)));
댓글 수: 1
per isakson
2019년 3월 14일
You might want to try to fix your code with the help of: Debug a MATLAB Program and Examine Values While Debugging
답변 (1개)
KSSV
2019년 3월 14일
YOu have asked this question already here: https://in.mathworks.com/matlabcentral/answers/449546-problem-with-index-dufort-frankel-scheme?s_tid=prof_contriblnk . I have corrected your code..you have not acknowledged it and you didn't discuss it. You should close your question by accepting the answer if it worked for you and then ask the second next question.
xl=0; xr=1; % x domain [xl,xr]
J = 10; % J: number of division for x
dx = (xr-xl) / J; % dx: mesh size
tf = 0.1; % final simulation time
Nt = 50; % Nt: number of time steps
dt = tf/Nt;
k = 0;
t = 0;
mu = dt/(dx)^2;
if mu > 0.5 % make sure dt satisy stability condition
error('mu should < 0.5!')
end
% Evaluate the initial conditions
x = xl : dx : xr; % generate the grid point
% f(1:J+1) since array index starts from 1
f = sin(pi*x) + sin(2*pi*x);
% store the solution at all grid points for all time steps
u = zeros(J+1,Nt+1);
u_ex = u ;
% Find the approximate solution at each time step
for n = 1:Nt
t = n*dt; % current time
% boundary condition at left side
gl = sin(pi*xl)*exp(-pi*pi*t)+sin(2*pi*xl)*exp(-4*pi*pi*t);
% boundary condition at right side
gr = sin(pi*xr)*exp(-pi*pi*t)+sin(2*pi*xr)*exp(-4*pi*pi*t);
if n==1 % first time step
for j=2:J % interior nodes
u(j,n) = f(j) + mu*2*(f(j+1)-(f(j)+f(j))+f(j-1));
end
u(1,n) = gl; % the left-end point
u(J+1,n) = gr; % the right-end point
else
for j=2:J % interior nodes
u(j+1,n)=u(j,n-1)+mu*2*(u(j,n+1)-(u(j-1,n)+u(j+1,n))+u(j,n-1));
end
u(1,n) = gl; % the left-end point
u(J+1,n) = gr; % the right-end point
end
% calculate the analytic solution
for j=1:J+1
xj = xl + (j-1)*dx;
u_ex(j,n)=sin(pi*xj)*exp(-pi*pi*t) ...
+sin(2*pi*xj)*exp(-4*pi*pi*t);
end
end
% Plot the results
tt = dt : dt : (Nt+1)*dt;
figure(1)
colormap(gray); % draw gray figure
surf(x,tt, u'); % 3-D surface plot
xlabel('x')
ylabel('t')
zlabel('u')
title('Numerical solution of 1-D parabolic equation')
figure(2)
surf(x,tt, u_ex'); % 3-D surface plot
xlabel('x')
ylabel('t')
zlabel('u')
title('Analytic solution of 1-D parabolic equation')
maxerr=max(max(abs(u-u_ex)));
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Surfaces, Volumes, and Polygons에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!