Error in computing numerical solution?
1 回表示 (過去 30 日間)
古いコメントを表示
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
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 Center および File Exchange で Surface and Mesh Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!