I am trying to find the displacement of this below 2nd order differential equation.
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
where x is the displacement and g is the acceleration due to gravity.
how to solve this equation in matlab. can anyone please help as it is new to me.
Thanks
0 Kommentare
Antworten (2)
Wan Ji
am 19 Aug. 2021
Bearbeitet: Wan Ji
am 19 Aug. 2021
Hi, friend! The ode you provided is a 2nd order ode. Follow the code you will know how to solve this ode. But at first, since both the mass m and the stiffness K is positive, the equation should be modified as:
Then the code is
% Firstly, define the ode function
% Here we set x(1)=x and x(2)=x';
% Then odefun = [x'; x''] = [x(2); -K/m*x*(1)+g]
odefun = @(t,x, K, m, g)[x(2); -K/m*x(1)+g];
K = 1; % set stiffness
m = 1; % set mass
g = 10; % set gravity
x0 = [0;0]; % set the initial conditions [initial position and initial velocity]
tspan = 0:0.1:20; % set t span
[t, x] = ode45(@(t,x)odefun(t, x, K, m, g), tspan, x0); % solve with ode45
plot(t,x(:,1),'r-') % plot results
hold on
plot(t,x(:,2),'b-')
xlabel('t'); ylabel('x or dx/dt')
legend('x','dx/dt')
1 Kommentar
Wan Ji
am 19 Aug. 2021
Since this ode can also be solved by dsolve (Notice that only a few odes can be solved analytically), here I post how to use this symbolic tool.
First we define
and set ω a positive real number.
The initial condition: the initial position x and initial velocity are denoted by x01 and x02 respectively in this demo.
syms x(t) x01 x02 g
syms omega real positive
eq = diff(x,2) + omega^2*x - g ==0; % pde
Dx = diff(x,1); % x'
conds = [ x(0) == x01, Dx(0) == x02]; % initial conditions
x = dsolve(eq, conds)
The result is (calculated in the *mlx file )
Krishna Sutar
am 19 Aug. 2021
Please refer to dsolve documentation where you understand how to solve differential equations in MATLAB. Few examples are also provided in the documentation.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!