- #1
abooaboo110
- 1
- 0
Homework Statement
y'' + 3y' + 2y = 0, y(0) = 1, y'(0) = 0
Homework Equations
Finite Difference Approximations:
y'' = (y(ii+1) - 2y(ii) + y(ii-1))/h^2
y' = (y(ii+1) - y(ii-1))/(2h)
where h is the finite difference.
The Attempt at a Solution
I wrote the MATLAB code (just to try out finite differences):
x = linspace(0,1,20);
h = x(2) - x(1);
N = length(x);
y0 = 1;
yp0 = 0;
ynext = y0 + h*yp0;
a = ones(size(x))*(1/h^2 - 3/(2*h))
b = ones(size(x))*(-2/h^2 + 2);
c = ones(size(x))*(1/h^2 + 3/(2*h))
r = zeros(size(x));
a(1) = 0; c(1) = 0; b(1) = 1; r(1) = y0;
a(2) = 0; c(2) = 0; b(2) = 1; r(2) = ynext;
%a(2) = -1/(2*h); b(2) = 0; c(2) = 1/(2*h); r(2) = yp0;
A = diag(a(2:N),-1) + diag(b,0) + diag(c(1:(N-1)),1)
y = inv(A)*r';
yt = 2*exp(-x) - exp(-2*x);
plot(x,y,'o',x,yt);
Basically setting up a tridiagonal matrix involving terms y(ii).
I can solve the problem by doing a two step forward iteration but do not see why solving the system does not give me the same(correct plot). yt gives the exact solution.