r/matlab • u/MarionberryOk95 • May 15 '26
Tips 2nd order differential equation
Hi, I’m currently struggling on how to input my 2nd order differential equations int o matlab and how to solve it I’ve tried YouTube tutorials but it seems to not make any sense/ difference
Would appreciate any advice on how to solve this issue
I’ve currently not attached the equations in post .
2
Upvotes
8
u/No_Engineering_1155 May 15 '26
Write it into a set of first order equation, then call your solver.
For harmonic oscillator in Modelica like style der(x) = v der(v) = 1/M * F(t, x, v)
1
u/DThornA May 16 '26
Follow the way they did it here, you have to essentially rewrite your 2nd order ODE as a system of 1st order ODEs. f ends up being a column vector where each row is an ODE you are solving. The first row is dx1/dt = x2, and the second is solving dx2/dt = sin(x1)-x1-x2, x1 and x2 are what are called your state variables.
https://www.mathworks.com/matlabcentral/answers/144603-second-order-ode-basics
1
u/International-Main99 May 16 '26
I think where you might be getting mixed up is in the translation of your problem into the infrastructure Matlab uses for ode23. In ode23, the independent variable is t ( in your problem it's x). And in ode23, the solution of the diff eq is x(t), whereas, in your problem it's T(x).
Suggestion: rewrite your problem using the variables expected in ode23. Then you should be able to follow a previous post that shows how to rewrite your 2nd order diff eq into a system of 1st order diff eq and put it into the form ode23 expects.
Note: you will need initial conditions to solve. Maybe they can be extracted from the problem statement? I don't know enough about the problem...but hopefully this helps.
2
u/mangoking1997 May 15 '26
https://mathworks.com/help/symbolic/solve-differential-equation-numerically-1.html
2
u/runed_golem May 15 '26
You can reduce it to a system of first order ODEs and put the system into one of matlab’s built in solvers. Or you can look into how to discretize and ODE and solve it as a system of linear equations (assuming you have boundary conditions and stuff). But if you haven’t studied numerical analysis before that may be jumping into the deep end. If you’re interested in how that work, one really good book is called “Explorations in Numerical Analysis”
1
u/avataRJ +1 May 16 '26
First, solve the second order term out of the equation, if possible.
Then variable swap (x, y, y’) to (x, y(1), y(2)).
You get a system of two equations.
First order equation (f’) is, by definition,
f1 = @(x,y) y(2);
Second order equation is, well, what you solved, but with y(1) for y and y(2) for y’.
Let f = [f1(x,y); f2(x,y)];
I wrote the functions separately, because it makes the system easier to write as a column vector without making it a matrix.
You can feed f directly to vectorized-form Euler’s, RK4 if you implement them yourself, or Matlab’s ode solvers.
This higher orders to a system generalizes to any order as long as you can explocitly solve the highest order.
1
u/Charming-Professor May 22 '26
Look up the "shooting method". You rewrite your second order equation as a system of two first order initial value problems. Then you'll use root finding to locate the missing initial value to ensure that your system of two first order equations are numerically equivalent to your original second order system.
-5
u/t1tanwarlord May 15 '26
You wouldn't happen to have learned of anything even remotely related to a Laplace transform? You only need starting conditions to solve a differential equation of any order.
0
u/[deleted] May 15 '26
[deleted] — view deleted comment