Finite Difference Method for second order ODE in MATLAB












0












$begingroup$


I have an ordinary differential equation,
$$frac{d^2Y}{dX^2} - frac{1}{R} frac{dY}{dX} = 0,$$
where $R$ is a constant. I have to numerically integrate this ODE for a range from $0$ to $1$ using the central difference method and the finite difference method. I have a couple of questions.



Our amount of steps are $30$ and I've rearranged the differential equation, substituting the difference equations for the differential equations in the formula. After I've done this, I create the central difference matrix using the three coefficients for $X_{i+1}$, $X_i$, and $X_{i-1}$ and multiply the matrix by a matrix with the $30$ steps. The output matrix in my code, $B$, doesn't seem to be correct.



Can anyone please tell me what is wrong with my algorithm? I'm having a hard time trying to find the problem. Thank you so much.



% initial conditions and constants  
yn = 1;
y0 = 0;
N = 30; #steps
R = .01;
h = (yn-y0)/(N-1);

alph = 2*R - h; %alpha coefficient
bet = -4*R; %Beta Coefficient
gam = 2*R + h; %Gamma Coefficient

% initialize
cent_diff = zeros(N,N); b = zeros(N,1); b(1) = 0;
y = zeros(N,1); y(1) = 0;


% creates the y vector
for i = 2:N
y(i) = y(i-1,1) + h;
%b(i) = cent_diff(1,:)*y;
end

% creates the cent_diff matrix
for i =1:N %rows
for j = 1:N %columns
if i == j
cent_diff(i,j) = bet;
elseif i == j+1
cent_diff(i,j) = alph;
elseif i == j-1
cent_diff(i,j) = gam;
end
end
end

for k = 2:N-1
c = 0;
% loop to add products of matrix and y vector
for i = 1:N
b_k = cent_diff(1,i)*y(i) + c;
c = b_k;
end
b(k) = c;
end % last value of vector b
b(N) = -gam;









share|cite|improve this question











$endgroup$












  • $begingroup$
    Do you have initial or boundary conditions?
    $endgroup$
    – rafa11111
    Dec 3 '18 at 22:21
















0












$begingroup$


I have an ordinary differential equation,
$$frac{d^2Y}{dX^2} - frac{1}{R} frac{dY}{dX} = 0,$$
where $R$ is a constant. I have to numerically integrate this ODE for a range from $0$ to $1$ using the central difference method and the finite difference method. I have a couple of questions.



Our amount of steps are $30$ and I've rearranged the differential equation, substituting the difference equations for the differential equations in the formula. After I've done this, I create the central difference matrix using the three coefficients for $X_{i+1}$, $X_i$, and $X_{i-1}$ and multiply the matrix by a matrix with the $30$ steps. The output matrix in my code, $B$, doesn't seem to be correct.



Can anyone please tell me what is wrong with my algorithm? I'm having a hard time trying to find the problem. Thank you so much.



% initial conditions and constants  
yn = 1;
y0 = 0;
N = 30; #steps
R = .01;
h = (yn-y0)/(N-1);

alph = 2*R - h; %alpha coefficient
bet = -4*R; %Beta Coefficient
gam = 2*R + h; %Gamma Coefficient

% initialize
cent_diff = zeros(N,N); b = zeros(N,1); b(1) = 0;
y = zeros(N,1); y(1) = 0;


% creates the y vector
for i = 2:N
y(i) = y(i-1,1) + h;
%b(i) = cent_diff(1,:)*y;
end

% creates the cent_diff matrix
for i =1:N %rows
for j = 1:N %columns
if i == j
cent_diff(i,j) = bet;
elseif i == j+1
cent_diff(i,j) = alph;
elseif i == j-1
cent_diff(i,j) = gam;
end
end
end

for k = 2:N-1
c = 0;
% loop to add products of matrix and y vector
for i = 1:N
b_k = cent_diff(1,i)*y(i) + c;
c = b_k;
end
b(k) = c;
end % last value of vector b
b(N) = -gam;









share|cite|improve this question











$endgroup$












  • $begingroup$
    Do you have initial or boundary conditions?
    $endgroup$
    – rafa11111
    Dec 3 '18 at 22:21














0












0








0





$begingroup$


I have an ordinary differential equation,
$$frac{d^2Y}{dX^2} - frac{1}{R} frac{dY}{dX} = 0,$$
where $R$ is a constant. I have to numerically integrate this ODE for a range from $0$ to $1$ using the central difference method and the finite difference method. I have a couple of questions.



Our amount of steps are $30$ and I've rearranged the differential equation, substituting the difference equations for the differential equations in the formula. After I've done this, I create the central difference matrix using the three coefficients for $X_{i+1}$, $X_i$, and $X_{i-1}$ and multiply the matrix by a matrix with the $30$ steps. The output matrix in my code, $B$, doesn't seem to be correct.



Can anyone please tell me what is wrong with my algorithm? I'm having a hard time trying to find the problem. Thank you so much.



% initial conditions and constants  
yn = 1;
y0 = 0;
N = 30; #steps
R = .01;
h = (yn-y0)/(N-1);

alph = 2*R - h; %alpha coefficient
bet = -4*R; %Beta Coefficient
gam = 2*R + h; %Gamma Coefficient

% initialize
cent_diff = zeros(N,N); b = zeros(N,1); b(1) = 0;
y = zeros(N,1); y(1) = 0;


% creates the y vector
for i = 2:N
y(i) = y(i-1,1) + h;
%b(i) = cent_diff(1,:)*y;
end

% creates the cent_diff matrix
for i =1:N %rows
for j = 1:N %columns
if i == j
cent_diff(i,j) = bet;
elseif i == j+1
cent_diff(i,j) = alph;
elseif i == j-1
cent_diff(i,j) = gam;
end
end
end

for k = 2:N-1
c = 0;
% loop to add products of matrix and y vector
for i = 1:N
b_k = cent_diff(1,i)*y(i) + c;
c = b_k;
end
b(k) = c;
end % last value of vector b
b(N) = -gam;









share|cite|improve this question











$endgroup$




I have an ordinary differential equation,
$$frac{d^2Y}{dX^2} - frac{1}{R} frac{dY}{dX} = 0,$$
where $R$ is a constant. I have to numerically integrate this ODE for a range from $0$ to $1$ using the central difference method and the finite difference method. I have a couple of questions.



Our amount of steps are $30$ and I've rearranged the differential equation, substituting the difference equations for the differential equations in the formula. After I've done this, I create the central difference matrix using the three coefficients for $X_{i+1}$, $X_i$, and $X_{i-1}$ and multiply the matrix by a matrix with the $30$ steps. The output matrix in my code, $B$, doesn't seem to be correct.



Can anyone please tell me what is wrong with my algorithm? I'm having a hard time trying to find the problem. Thank you so much.



% initial conditions and constants  
yn = 1;
y0 = 0;
N = 30; #steps
R = .01;
h = (yn-y0)/(N-1);

alph = 2*R - h; %alpha coefficient
bet = -4*R; %Beta Coefficient
gam = 2*R + h; %Gamma Coefficient

% initialize
cent_diff = zeros(N,N); b = zeros(N,1); b(1) = 0;
y = zeros(N,1); y(1) = 0;


% creates the y vector
for i = 2:N
y(i) = y(i-1,1) + h;
%b(i) = cent_diff(1,:)*y;
end

% creates the cent_diff matrix
for i =1:N %rows
for j = 1:N %columns
if i == j
cent_diff(i,j) = bet;
elseif i == j+1
cent_diff(i,j) = alph;
elseif i == j-1
cent_diff(i,j) = gam;
end
end
end

for k = 2:N-1
c = 0;
% loop to add products of matrix and y vector
for i = 1:N
b_k = cent_diff(1,i)*y(i) + c;
c = b_k;
end
b(k) = c;
end % last value of vector b
b(N) = -gam;






numerical-methods finite-differences finite-difference-methods






share|cite|improve this question















share|cite|improve this question













share|cite|improve this question




share|cite|improve this question








edited Dec 3 '18 at 23:28









rafa11111

1,1262417




1,1262417










asked Dec 3 '18 at 22:08









Sebastian SanchezSebastian Sanchez

1




1












  • $begingroup$
    Do you have initial or boundary conditions?
    $endgroup$
    – rafa11111
    Dec 3 '18 at 22:21


















  • $begingroup$
    Do you have initial or boundary conditions?
    $endgroup$
    – rafa11111
    Dec 3 '18 at 22:21
















$begingroup$
Do you have initial or boundary conditions?
$endgroup$
– rafa11111
Dec 3 '18 at 22:21




$begingroup$
Do you have initial or boundary conditions?
$endgroup$
– rafa11111
Dec 3 '18 at 22:21










0






active

oldest

votes











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "69"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
noCode: true, onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3024759%2ffinite-difference-method-for-second-order-ode-in-matlab%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Mathematics Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmath.stackexchange.com%2fquestions%2f3024759%2ffinite-difference-method-for-second-order-ode-in-matlab%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

ComboBox Display Member on multiple fields

Is it possible to collect Nectar points via Trainline?