Solving second order differential equation that contains a matrix
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello I am trying to solve this following ODE:
d^2y/dt^2+epsilon*dy/dt= -0.2*(exp(-y*y')+0.2).*A*y
epsilon=1/100000
such that A=[1 0 -1;0 1.5 0;-1 0 2];
I tried the code written on the attached file(Untitled222.m) above, once I delete the matrix A it works, and once i write it i get an error message.You can find attached my code file. I hope someone can give me useful answer to help me and help other students that they may face the same problem.
regards;
AMINA.
syms t y(t) Y
D1y = diff(y,t);
D2y = diff(y,t,2);
A=[1 0 -1; 0 1.5 0;-1 0 2];
Eqn = D2y +(1/100000)*D1y+(0.2*(exp(-y*y')+0.2).*A*y) == 0;
yode = odeToVectorField(Eqn);
Yodefcn = matlabFunction(yode, 'Vars',[t Y]);
Yodefcn = @(t,Y) [Y(2);-(1/100000)*Y(2)-(0.2*(exp(-Y(1)*Y(1)')+0.2).*A*y)];
tspan = [0 15];
Y0 = [0 0];
[T,Y] = ode45(Yodefcn, tspan, Y0);
figure(1)
plot(T, Y)
grid
2 Kommentare
Jan
am 20 Okt. 2021
Bearbeitet: Jan
am 20 Okt. 2021
The matrix A appears at two locations in the code. Please post exactly the code, you are running. Append the error message also: It is very useful to see the error, if it should be solved.
I've included the code directly and ran it to show the error message.
Everything coming after the message is confusing only. Does the not affected code matter here?
Jan
am 21 Okt. 2021
Dear Jan
thank you for your answer
1/ The matrix A appears at two locations because when we want to solve a seconde order ODE by using ode45 we need first to rewrite the second order ODE as a system of First-order for that I wrote down my equation (eqn) with the first derivative as D1Y=diff(y,t) and the second order derivative as D2Y=diff(y,t,2). Then i tried to write it down as a system using odeToVectorField(Eqn). After that we need to convert that system to a matlab function because matlab ODE solvers doesn't accept symbolic expressions as input, that's why i wrote down the function Yodefcn=@(t,y)=[Y(2); (-1/100000)*Y(2)-(0.2*(exp(-Y(1)*Y(1)')+0.2).*A*Y)]. Finally we solve the system of First-Order using ODE45.
2/ The algorithms stops at the step of rewriting the second order ode as a system using odeTovecTorField(Eqn) and we get the following error message
Error in odeToVectorField>mupadOdeToVectorField (line 189)
T = feval(symengine,'symobj::odeToVectorField',sys,x,stringInput);
Error in odeToVectorField (line 138)
sol = mupadOdeToVectorField(varargin);
Error in Untitled222 (line 7)
[yode] = odeToVectorField(Eqn);
syms t y(t) Y
D1y = diff(y,t);
D2y = diff(y,t,2);
A=[1 0 -1; 0 1.5 0;-1 0 2];
Eqn = D2y +(1/100000)*D1y+(0.2*(exp(-y*y')+0.2).*A*y) == 0;
[yode] = odeToVectorField(Eqn);
%[yode]= odeToVectorField(D2y ==-(1/100000)*D1y-(0.2*(exp(-y*y')+0.2).*A*y));
%Yodefcn = matlabFunction(yode, 'Vars',[t Y]);
Yodefcn = @(t,Y) [Y(2);-(1/100000)*Y(2)-(0.2*(exp(-Y(1)*Y(1)')+0.2).*A*Y)];
tspan = [0 15];
Y0 = [0 0];
[T,Y] = ode45(Yodefcn, tspan, Y0);
figure(1)
plot(T, Y)
grid
Antworten (0)
Siehe auch
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!