I am trying to find the displacement of this below 2nd order differential equation.
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!