FDM by centred difference gives a gradient of -0.5 instead of -2

조회 수: 2 (최근 30일)
Eustace Tan
Eustace Tan 2014년 5월 2일
Okay, maybe I'm just stupid, but the title pretty much says it all. The code I made is as follows,
n=[10:10:90 100:100:900 1000:1000:9000 10000:10000:90000];
for i=1:length(n),
h(i)=1/(n(i));
E=sparse(1:n(i)+1,1:n(i)+1,2*ones(1,n(i)+1),n(i)+1,n(i)+1);
F=sparse(2:n(i)+1,1:n(i),-1*ones(1,n(i)),n(i)+1,n(i)+1);
A=F+E+F';
A(1,1)=1;
A(1,2)=0;
A(n(i)+1,n(i))=-1;
A(n(i)+1,n(i)+1)=1;
x=0:h(i):1;
B(1,1:1+n(i))=((h(i))^2)*10;
B(1,1)=0;
B(1,1+n(i))=0;
u=A\B';
y=-5*x.^2+10*x; %Analytical solution
e(i)=norm(u-y');
end
figure
loglog(h.^-2,e,'r-+')
grid on
title('Graph of partition width to the power of -2, h^-^2 versus error, e')
xlabel('partition width to the power of -2, h^-^2')
ylabel('error, e')
I have been hammering away at this all night but can't seem to figure out why the gradient is -0.5. Any help?

답변 (0개)

카테고리

Help CenterFile Exchange에서 Oceanography and Hydrology에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by