OdeToVectorField usage with initial conditions

Trying to understand:
this is documentation for odeToVectorField:
so, for exmaple i have two second order DE
eq1 and eq2 with variables for example x(t) and y(t)
And my initial conds:
cond1 = x(0) == 2
cond2 = y(0) == 4
cond3 = x'(0) == 0
cond4 = y'(0) == 0
So I want to solve it exactly this way (using odeToVectorField with initial conds)
this is my usage of function:
V = odeToVectorField([eq1, eq2],[cond1, cond2, cond3, cond4])
M = matlabFunction(V,'vars', {'t','Y'});
So question is:
How I can pass this to ODE solver (or solve without ODE solver) exactly this way (with initial conds passed to odeToVectorField). Is this possible?

Answers (2)

syms y(x) a
cond1 = y(0) == a
Sol = dsolve(diff(y,2)==x,cond1) % symbolic solver since initial condition is symbolic
And if you really want to know how to use the initial condtions for the derivative then it's:
% an example
dy = diff(y);
cond = dy(0) == a;
You can easily create matlabFunction from V vector. THe main problem (i think) is correct order of initial conditions
You can see Y variable and see how your f,g,Df functions are ordered. Or you can manipulate with sort
here is an example (not tested)
syms f(t) g(t) Df
v = [f g Df];
y0 = [2 0 1]; % f(0) = 2, g(0) = 0, Df(0) = 1
[V,Y] = odeToVectorField(diff(f, 2) == f + g, diff(g) == -f + g);
M = matlabFunction(V,'vars', {'t','Y'});
[~,ix1] = sort(v);
y0 = y0(ix1);
[~,ix2] = sort(Y);
y0 = y0(ix2);
[t,y] = ode45(M,tspan,y0);

Categories

Asked:

on 12 Jun 2020

Answered:

on 13 Jun 2020

Community Treasure Hunt

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

Start Hunting!