Second order diffrential equation solve using ode 45
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Abhik Saha
am 22 Aug. 2022
Kommentiert: Abhik Saha
am 22 Aug. 2022
I want to solve a second order differential equation of the form
d^2y/dt^2+dy/dt=sin(100t)
I want the value of dy/dt and y.
I am copied my code below where easily I can get the dy/dt as a function of t but I am not able find y as a function of t. I am new to Matlab. Please help me regarding this issue. I copied the code below
---------------------------------------------
main program
---------------------------------------------
t=linspace(0,500,1000); %time span%
y0=[0];
%%solving using ode45
[tsol, ysol]=ode45(@(t,y0) firstodefun2(t,y0), t, y0);
---------------------------------------------------
function definition
----------------------------------------------------
function dy=firstodefun2(t,y0)
dy=zeros(1,1);
dy(1)=sin(100*t)-y0(1);
end
0 Kommentare
Akzeptierte Antwort
Sam Chak
am 22 Aug. 2022
Hi @Abhik Saha
The code is fixed below. This is a 2nd-order system, and so there are two state variables and two initial values.
t = [0, 10];
y0 = [0 0];
% solving using ode45
[tsol, ysol] = ode45(@(t, y) firstodefun2(t, y), t, y0);
plot(tsol, ysol(:, 1)), grid on, xlabel('t, [sec]'), ylabel('y(t)')
function dy = firstodefun2(t, y)
dy = zeros(2, 1);
dy(1) = y(2);
dy(2) = sin(100*t) - y(2);
end
Weitere Antworten (1)
Torsten
am 22 Aug. 2022
Set x(1) = y and x(2) = dy/dt.
Then you get a system of differential equations
dx(1)/dt = x(2)
dx(2)/dt = -x(2) + sin(100*t)
Of course, you also will have to supply two initial conditions (one for y, the second for dy/dt at t=0).
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!