MATLAB Answers

0

1D Finite Difference Method

Asked by Vitaly Andreychenko on 23 Apr 2019 at 11:32
Latest activity Edited by Vitaly Andreychenko on 23 Apr 2019 at 15:42
Hello guys, I'm very new at Matlab. Currently i'm working the following task:
given ODE:
with the following conditions:
and ,
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.

  0 Comments

Sign in to comment.

Products


Release

R2019a

0 Answers