converting a second degree differential equation into a first order

I am trying to reduce the order of a function but I always get an error message :
syms y(t);
eqn = diff(y,2) + c*y' + k*y == F0sin(w*t) ;
cond1 = y(0) == 4 ;
cond2 diff(y0) == 0 ;
V = odeToVectorField(eqn);

 Réponse acceptée

Declare all the variables, and for good measure, return the second output from odeToVectorField because it tells you wnat the function is doing internally, and the substitutions it made.
syms c k F0 w y(t)
D1y = diff(y);
D2y = diff(D1y);
eqn = D2y + c*D1y + k*y == F0*sin(w*t) ;
% cond1 = y(0) == 4 ;
% cond2 = diff(y0) == 0 ;
[V,Subs] = odeToVectorField(eqn)
V = 
Subs = 
Now, convert this into an anonymous function that the nmumerical ordinary differntial equation solvers can work with, and integrate it to get a numerical result.
.
.

6 commentaires

Anas Gharsa
Anas Gharsa le 28 Jan 2022
Modifié(e) : Anas Gharsa le 28 Jan 2022
I really appreciate it!! thank you !!! But it stiil gives me error!!!
'syms' requires Symbolic Math Toolbox.
Error in finite_differences (line 3)
syms c k F0 w y(t)
As always, my pleasure!
Yes, it does.
The original posted code uses Symbolic Math Toolbox syntax and function calls, so I assumed it was available.
In any event, it is still possible to use it here:
syms c k F0 w y(t) t Y
D1y = diff(y);
D2y = diff(D1y);
eqn = D2y + c*D1y + k*y == F0*sin(w*t) ;
% cond1 = y(0) == 4 ;
% cond2 = diff(y0) == 0 ;
[V,Subs] = odeToVectorField(eqn)
V = 
Subs = 
Vfcn = matlabFunction(V, 'Vars',{t,Y,c,F0,k,w})
Vfcn = function_handle with value:
@(t,Y,c,F0,k,w)[Y(2);F0.*sin(t.*w)-c.*Y(2)-k.*Y(1)]
Use that with ode45 (or the function of your choice) to numerically integrate it.
Download and install the Symbolic Math Toolbox as soon as possible to have access to all its functions.
.
Thank you so much!!! that was so helpful!! but i have 2 more questions !! i did what u said and still gives me error !!!
Unrecognized function or variable 'y0'.
Error in finite_differences (line 8)
cond2 = diff(y0) == 0 ;
and the last question is : can i use the finite_differences or heun's method to solve it ??
As always, my pleasure!
Note — I commented-out the ‘cond1’ and ‘cond2’ assignments because they are not used as symbolic variables in this instance. Instead, assign the values to an initial conditions vector as:
ic = [4; 0];
(this is one instance where the ‘Subs’ result becomes useful) in order to use them with the numeric ODE integration functions.
So long as the method you use integrate it accepts functions such as ‘Vfcn’ in that form, it can be used with them. (I designed it to work with the MATLAB numeric ODE integrators, since I thought that was the eventual objective.)
.
Thank you again for your help!!! I really appreciate it
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Produits

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by