1D Finite Difference Method
Show older comments
Hello guys, I'm very new at Matlab. Currently i'm working the following task:
given ODE:
with the following conditions:
Analytic solution:
Task: Implement an iterative Finite Difference scheme based on backward, forward and central differencing to solve this ODE. Use an array to store the N unknowns (DOFs)
. Implement the scheme in a function of the time step width which returns the DOF array as result. Plot the solution vector for a time step width
of ∆? = 0.1 ? together with the analytic solution in the same graph. Select N large enough to ensure that the simulation will at least simulate the solution for the first
second.
I'm trying to get the backward differencing first:
%Main Program
close all;
clear all;
dt=0.1;
N=round(1/dt+0.5);
backwardDiff(N,dt);
analyticSolution(N,dt);
function backwardDiff(N,dt)
dof=zeros(1,N);
g = -9.8;
%to get rid of negative degrees of freedom
dof(1) = 4;
dof(2) = g*dt+dof(1);
for i=3:N
dof(i) = g*dt*dt+2*dof(i-1)-dof(i-2)
end
%fill time vector with spacing 0.1 for the plot
startValue = 0.0;
endValue = 1.0;
Elements = 11;
stepSize=(endValue-startValue)/(Elements-1);
timeVector = startValue:stepSize:endValue
plot(timeVector,dof)
end
%analytical Solution
function analyticSolution(N,dt)
%fill time vector with spacing 0.1 for the plot
startValue = 0.0;
endValue = 1.0;
Elements = 11;
stepSize=(endValue-startValue)/(Elements-1);
timeVector = startValue:stepSize:endValue;
sVector = zeros(1,N);
for i=1:N
sVector(i) = 4+0.5*(-9.8)*timeVector(i)^2
end
plot(timeVector, sVector)
ylabel("S/t")
xlabel("t")
I have no clue how to proceed with the central and forward method. Moreover how do I place all the results on the same graph?
Thank you.
Answers (0)
Categories
Find more on Ordinary Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!