# I am trying to find the displacement of this below 2nd order differential equation.

4 ビュー (過去 30 日間)
Lokesh Katari 2021 年 8 月 19 日
コメント済み: Wan Ji 2021 年 8 月 19 日 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

サインインしてコメントする。

### 回答 (2 件)

Wan Ji 2021 年 8 月 19 日

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 件のコメント表示非表示 なし
Wan Ji 2021 年 8 月 19 日
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 2021 年 8 月 19 日
Please refer to dsolve documentation where you understand how to solve differential equations in MATLAB. Few examples are also provided in the documentation.

サインインしてコメントする。

R2019b

### Community Treasure Hunt

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

Start Hunting!