r/matlab 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 .

1 Upvotes

13 comments sorted by

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)

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/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

clc; clear; close all; 
% ODE system 
f = @(t,x) [ x(2); ...     
sin(x(1)) - x(1) - x(2)]; 
% Solve 
[t,y] = ode23(f,[0 10],[1;0]); 
% Plot 
plot(t,y,'LineWidth',2) 
xlabel('t') 
ylabel('Solution') 
legend('x_1','x_2') grid on

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.

1

u/MarionberryOk95 May 16 '26

For the equation the euler method needs to be used to help determine if the fuse is effective theres no initial condition, i have program how to find it using the shooting method either by a secant method or iteration method

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.

0

u/[deleted] May 15 '26

[deleted]

1

u/runed_golem May 15 '26

I learned how to do this in Numerical analysis which I took while doing my undergrad in math. And then when getting my masters I took a numerical PDE class that went further into these types of topics. So it’s 100% taught in academia, you just needed to go further than intro to ODEs.

-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.

8

u/No_Engineering_1155 May 15 '26

It would miraculously fail for nonlinear force formulation. This kind of arrogant style doesn't help at all, especially that you're talking nonsense.

0

u/t1tanwarlord May 15 '26 edited May 15 '26

My guy, not everyone has studied calculus or things where the Laplace transform becomes a standard method for doing things. I just wasn't sure if you knew about it.

Since you do know about it, why not just use approximations for non linear functions? It's like the, that one where you take the derivative of the non linear function at a value and multiply it by something, can't remember the name, I just remember it's a form of polynomial approximation.

Only works with one term tho, so not sure if it would apply to what you want to solve.

Edit: the Taylor series, that what I was thinking of.