FDM by centred difference gives a gradient of -0.5 instead of -2
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (0)
Siehe auch
Kategorien
Mehr zu Calculus 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!