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

조회 수: 1(최근 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일
편집: 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!