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

2 Ansichten (letzte 30 Tage)
Eustace Tan
Eustace Tan am 2 Mai 2014
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?

Antworten (0)

Kategorien

Mehr zu Oceanography and Hydrology finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by