- 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
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)
댓글 수: 0
채택된 답변
Mukul Rao
2015년 6월 22일
I would recommend the following changes:
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
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File 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!