Solving a second order Differential equasion using Eulers Method

6 次查看(过去 30 天)
I have to solve a second order differential equation using Euler's method (so not the ode45 command) and without using the Euler matlab shortcut. meaning i have write the loop myself.
i listed my parameter is a sturcture as follows:
%%This file defines the parameters of the parachutist
% use a structure to store the parameters
P.m = 80; % mass skydiver (kg)
P.h = 1000; % starting altitude (m)
P.g = 9.81; % gravity (m/s^2)
P.k = 1.4; % drag coefficient
and i defined the equations of motion for the skydiver as follows
function dx= Skydiver(t,w)
% Equations of motion for a skydiver
dx = zeros(2,1)
dx(1)=w(2);
dx(2)= -P.g+P.k/P.m*w(2)^2
In the following part i have to program the Euler's method to solve this problem, and eventually plot the altitude of the skydiver with respect to time and the speed of the skydiver with respect to time
clear all; % this helps to prevent unexpected problems
SkydiverParameters; % this runs the parameters script
%User-defined parameters
Tsim = 10; % simulate over Tsim seconds
h = 0.001; % step size
N= Tsim/h; % number of steps to simulate
x=zeros(2,1);
x(1)= 1000 % starthoogte
x(2)= 0 % startsnelheid
Simulation loop using Euler method
for k=1:N
end
so far i got this. but i cant figure out how to write the loop for the Euler's method Also, in the task description a pseude-code was given, but i cant figure it out.
Input: model function xdot = f(x) as defined above in step 3, simulation step size h, number of steps N to simulate, initial state x(1) Algorithm: for k from 1 to N x(k+1) = x(k) + h*f(x(k)) end Output: sequence of simulated states x(1), x(2), …, x(N+1)

采纳的回答

Mukul Rao
Mukul Rao 2015-6-22
I would recommend the following changes:
  • Redefine x to be a matrix of size 2 by N where each column corresponds to an instant in simulation time
  • Verify your algorithm against ode45 results to be sure of the implementation
With these changes and some subjective modifications, the code would look as shown below. Copy paste the code to a MATLAB function file and execute it.
function euler
Tsim = 10; % simulate over Tsim seconds
h = 0.01; % step size
N= Tsim/h; % number of steps to simulate
x=zeros(2,N);
t = zeros(1,N);
x(1,1)= 1000; % starthoogte
x(2,1)= 0; % startsnelheid
for k=1:N-1
t(k+1)= t(k) + h; %You can implement this with linspace as well
%Euler's formula , x(k+1)= x(k) + h * dx(k)
x(:,k+1) = x(:,k) + h* Skydiver(t(k),x(:,k)); %Euler algorithm
end
%Compare with ode45
x0 = [1000;0];
[T,Y] = ode45(@Skydiver,[0 Tsim],x0);
plot(t,x(1,:)) %Results from Euler algorithm
hold on
plot(T,Y(:,1),'^') %Results from ode45
legend('Euler','ode45')
end
function dx= Skydiver(t,w)
% use a structure to store the parameters
P.m = 80; % mass skydiver (kg)
P.h = 1000; % starting altitude (m)
P.g = 9.81; % gravity (m/s^2)
P.k = 1.4; % drag coefficient
% Equations of motion for a skydiver
dx = zeros(2,1);
dx(1)=w(2);
dx(2)= -P.g+P.k/P.m*w(2)^2;
end
  1 个评论
Stinelino
Stinelino 2015-6-22
Mukal, thanks so much already. I do have a follow up question. I want to use the same methode to plot a graph of the speed of the skydiver. Can you help me out with that as well?

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Numerical Integration and Differential Equations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by