how derive a solution of ode45

1 view (last 30 days)
Luca Tricarico
Luca Tricarico on 7 Nov 2022
Edited: Torsten on 8 Nov 2022
Hi i have a ode45 solution with this script
syms y(t)
[V] = odeToVectorField(diff(y, 2) + 2*diff(y) - sin(y) == 0)
M = matlabFunction(V,'vars', {'t','Y'})
sol = ode45(M,[0 20],[1 0])
fplot(@(x)deval(sol,x,1),[0, 20])
dsol = diff(sol)
Now i want to derive the solution to obtain the velocity.
How i can do ?

Accepted Answer

Torsten
Torsten on 7 Nov 2022
The red curve is the derivative of the blue curve.
syms y(t)
[V] = odeToVectorField(diff(y, 2) + 2*diff(y) - sin(y) == 0)
V = 
M = matlabFunction(V,'vars', {'t','Y'})
M = function_handle with value:
@(t,Y)[Y(2);sin(Y(1))-Y(2).*2.0]
sol = ode45(M,[0 20],[1 0])
sol = struct with fields:
solver: 'ode45' extdata: [1×1 struct] x: [0 2.3881e-04 0.0014 0.0074 0.0373 0.1865 0.4925 0.8627 1.3143 1.8619 2.5131 3.2315 3.9762 4.7234 5.4770 6.2552 7.0796 7.9773 8.9985 10.3680 11.5660 12.5652 13.5105 14.5711 15.7928 17.2330 18.9829 20] y: [2×28 double] stats: [1×1 struct] idata: [1×1 struct]
hold on
fplot(@(x)deval(sol,x,1),[0, 20])
fplot(@(x)deval(sol,x,2),[0, 20])
hold off
  3 Comments
Luca Tricarico
Luca Tricarico on 7 Nov 2022
So the structure sol has two solutions, the first is the position and the second the velocity?
Torsten
Torsten on 8 Nov 2022
Edited: Torsten on 8 Nov 2022
If you look at the function handle M, your second-order equation is solved as a system of two first-order equations:
y1' = y2
y2' = sin(y1) -2*y2
Here, y1 is your y und y2 is dy/dt.
So if y1 is position, y2 is velocity, and these are the two components of the sol structure.
This is the disadvantage of OdeToVectorField: the beginner quickly looses control over the solution process.

Sign in to comment.

More Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!