MATLAB: Solving an ODE in matlab

MATLABode

Can you guys help me?
?̈(?) = -(0.1/50) ?̇(?)-2?(?)
How to numerically integrate to find a solution for ??
Speed of the mass in time-step ? + Δ? is:
?̇(? + Δ?) ≈ ?̇(?) + ?̈(?) Δ?
and the position in time-step ? + Δ? is:
?(? + Δ?) ≈ ?(?) + ?̇(?) Δ?
I want to put the previous two equations in a loop and loop from ? = 0 s to ? = 10 s(step Δ? = 0.001 s). And then plot those two graphs into in graph.
Assuming the mass is let go from initial displacement ?(0) = 0.5 m.
But I can’t get it to work in matlab.

Best Answer

You can use ode45 to solve such ODEs. See this example: https://www.mathworks.com/help/matlab/ref/ode45.html#bu3uj8b to see how your differential equation is converted into two first order ODEs.
fun = @(t, x) [x(2); -0.1/50*x(2) - 2*x(1)];
time = 0:0.001:10;
initial_condition = [0.5 0];
[t, y] = ode45(fun, time, initial_condition);
plot(t,y)
legend({'position', 'velocity'});
Related Question