Solve 2nd order ODE using Euler Method
Ältere Kommentare anzeigen
VERY new to Matlab...
Trying to implement code to use Euler method for solving second order ODE.
Equation:
x'' + 2*z*w*x' + w*x = 2*sin(2*pi*2*t)
z and w are constants. "t" is time.
Any help would be great.
Thanks!
5 Kommentare
John D'Errico
am 27 Sep. 2022
Bearbeitet: John D'Errico
am 27 Sep. 2022
If you need to solve that ODE, then why in the name of god are you writing an Euler's method to solve the ODE. Use ODE45. Do not write your own code. Since the only reason you need to use Euler's method is to do this as a homework assignment, then you need to write your own code. But Answers is not a service where we do your homework with no effort shown by you.
Matt
am 27 Sep. 2022
Matt
am 4 Okt. 2022
Verschoben: James Tursa
am 4 Okt. 2022
James Tursa
am 4 Okt. 2022
@Matt - FYI, when you get errors, it is best to post the entire error message along with your code. Regardless, see my answer below ...
Matt
am 4 Okt. 2022
Akzeptierte Antwort
Weitere Antworten (1)
Davide Masiello
am 27 Sep. 2022
Bearbeitet: Davide Masiello
am 27 Sep. 2022
Hi Matt - a second order ODE can be decomposed into two first order ODEs.
The secret is to set 2 variables y as

The you have

An example code is
clear,clc
tspan = [0,1]; % integrates between times 0 and 1
x0 = [1 0]; % initial conditions for x and dx/dt
[t,X] = ode15s(@odeFun,tspan,x0); % passes functions to ODE solver
x = X(:,1);
dxdt = X(:,2);
plot(t,x)
function dydt = odeFun(t,y)
z = 1;
w = 1;
dydt(1,1) = y(2);
dydt(2,1) = 2*z*w*y(2)-w*y(1)+2*sin(2*pi*2*t);
end
1 Kommentar
Davide Masiello
am 27 Sep. 2022
For more info, I suggest reading the documentation at the following link.
Kategorien
Mehr zu Programming finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
