Error in computing numerical solution?

조회 수: 1 (최근 30일)
Muhammad Usman
Muhammad Usman 2019년 12월 16일
댓글: Nagasai Bharat 2020년 8월 26일
I am enable to compute numerical solution, as it generates weird Initial conditions (may be) compared to the inintial value of U_exact
t0 = 0;
tn = 1;
x0 = 0;
xn = 1;
N = 16;
dx = 1/N;
dt = 0.004;
x = (x0:dx:xn)';
t = (t0:dt:tn)';
beta = 1;
L = 1;
nx = (xn-x0)/dx;
nt = (tn-t0)/dt;
u = @(x,t) (0.2*pi/3)*(((sin(pi*x))*exp(-(pi^2)*t)));
%% construction of diagonal matrix
v =((1:nx)*pi/L).^2;
A = full(diag(v,0));
%%
U=zeros(nx+1,nt+1);
U_exact=zeros(nx+1,nt+1);
for i=1:nx+1
fun = @(x) 2*x.*(1-x).*sin(i*(pi).*x);
q(i) = integral(fun,0,L,'ArrayValued',true);
end
U(:,1) = q';
U(1,:)=0;
U(nx+1,:)=0;
%% numerical solution construction
for k=1:nt
U(2:nx+1,k+1) = (eye(N)+dt*A)\U(2:nx+1,k);
end
%% exact solution construction
for k=1:nt+1
U_exact(1:nx+1,k) =u(x,t(k));
end
Numerical_sol = U;
Analytical_sol = U_exact;
%Plots
figure(1)
subplot(2,2,1)
mesh(t,x,U)
xlabel('Temporal dim')
ylabel('Spatial dim')
zlabel('Numerical sol')
subplot(2,2,2)
mesh(t,x,U_exact)
xlabel('Temporal dim')
ylabel('Spatial dim')
zlabel('Analytical sol')
subplot(2,2,3)
mesh(t,x,U_exact-U)
xlabel('Temporal dim')
ylabel('Spatial dim')
zlabel('Error in sol')
Help me to resolve the error. Thanks
  댓글 수: 1
Nagasai Bharat
Nagasai Bharat 2020년 8월 26일
Hi Usman,
After looking into your code, I think there might be an incorrect numerical approximation of the analytical expression. Please do check on that.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by