Error in computing numerical solution?

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 Kommentar

Nagasai Bharat
Nagasai Bharat am 26 Aug. 2020
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.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu App Building finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 16 Dez. 2019

Kommentiert:

am 26 Aug. 2020

Community Treasure Hunt

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

Start Hunting!

Translated by