Skip to content

Instantly share code, notes, and snippets.

@pureexe
Created January 9, 2018 03:53
Show Gist options
  • Select an option

  • Save pureexe/7fea7fcd3395df1bb0de14b99abff808 to your computer and use it in GitHub Desktop.

Select an option

Save pureexe/7fea7fcd3395df1bb0de14b99abff808 to your computer and use it in GitHub Desktop.
function [result,t] = fdm(a,b,h,alpha,beta,p,q,r)
t = a+h:h:b-h;
n = length(t);
forward = @(t) 1 + (p(t).*(h./2));
center = @(t) q(t).*(h.^2) - 2;
backward = @(t) 1 - (p(t).*(h./2));
right = @(t) r(t).*(h.^2);
A = spdiags([forward(t).',center(t).',backward(t).'],[-1,0,1],n,n);
F = right(t).';
F(1) = F(1) - alpha;
F(n) = F(n) - beta;
result = A \ F;
result = [alpha;result;beta];
t = [a,t,b];
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment