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

2 次查看(过去 30 天)
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