problem with solving system of differential equations

8 views (last 30 days)
Hello everyone,
I've been trying to solve a system of second order differential equations for a few days, but I cant solve them using any functions in matlab. I tried using solve , dsolve , fsolve, vpasolve, bvp4c (this one gives a wrong answer) ...
clc
clear all
x1 = 2;
y1 = 1;
r1 = 1;
m1 = 0.5;
% equations
syms x(t) y(t) ;
p1 = 0.65;
eqn1 = diff(x,2) - p1*(sqrt(m1 + r1^2 + (y-y1)^2)) == 0;
eqn2 = diff(y,2) - p1*(sqrt(m1 + r1^2 + (x-x1)^2)) == 0;
conds = [y(0)==0, x(0)==0, y(2)==5, x(2)==5];
eqns = [eqn1, eqn2];
sol = dsolve(eqns, conds)
I'm new to matlab and I searched a lot to find an answer but none worked on my problem. Any help would be appreciated.
Thank you in advance.

Answers (3)

Friedel Hartmann
Friedel Hartmann on 11 Feb 2022
I think your problem are the second derivatives. Matlab can only solve differential equations where the leading term(s) are first order..
Check the literature or the documentation on how you can transform your two equations into a system of four differential equations which only contain first derivatives.

Friedel Hartmann
Friedel Hartmann on 11 Feb 2022
% Example: solve -u''(x) = sin(x)
syms up(x) u(x)
eqns = [diff(up,x) == -sin(x), diff(u,x) - up == 0]; % -up' = sin(x) u' = up
S = dsolve(eqns,up(1) == 0,u(0) == 0)
  1 Comment
ali hamidi
ali hamidi on 12 Feb 2022
Thank you for your answer, but unfortunately doing this did not change anything and i still can't get an answer. here's my code trying this :
clc
clear all
x1 = 2;
y1 = 1;
r1 = 1;
m1 = 0.5;
p1 = 0.65;
% equations
syms x(t) y(t) xt(t) yt(t);
% xt' = x" yt' = y"
eqn1 = diff(xt,t) - p1*(sqrt(m1 + r1^2 + (y-y1)^2)) == 0;
eqn2 = diff(x,t) == xt;
eqn3 = diff(yt,t) - p1*(sqrt(m1 + r1^2 + (x-x1)^2)) == 0;
eqn4 = diff(y,t) == yt;
conds = [y(0)==0, x(0)==0, y(2)==5, x(2)==5];
eqns = [eqn1, eqn2, eqn3, eqn4];
sol = dsolve(eqns, conds)
%V = odeToVectorField(eqns)

Sign in to comment.


Alan Weiss
Alan Weiss on 14 Feb 2022
If you are willing to have a numerical solution, rather than a symbolic solution, then using fsolve along with ode45 can work. The technique is to solve the ODE from an initial guess using ode45, then change the initial guess using fsolve to satisfy the boundary conditions. Here is some code I knocked together. I assume that the ODE is given in the form [x xdot y ydot]. The initial conditions are x(0) = 0, y(0) = 0. The final conditions are x(2) = 5, y(2) = 5.
myx = fsolve(@myval,[1;1]) % Obtain the solution
Equation solved. fsolve completed because the vector of function values is near zero as measured by the value of the function tolerance, and the problem appears regular as measured by the gradient.
myx = 2×1
1.4648 1.3795
% Examine the end point
y0 = [0;myx(1);0;myx(2)]; % initial poiint for equation
sol = ode45(@odefn,[0 2],y0);
yend = deval(sol,2)
yend = 4×1
5.0000 4.1178 5.0000 3.7295
function dydt = odefn(~,y)
% y(t) = [x dx y dy]
x1 = 2;
y1 = 1;
r1 = 1;
m1 = 0.5;
p1 = 0.65;
dydt = zeros(4,1); % allocate y
dydt(1) = y(2);
dydt(3) = y(4);
dydt(2) = p1*(sqrt(m1 + r1^2 + (y(3)-y1)^2));
dydt(4) = p1*(sqrt(m1 + r1^2 + (y(1)-x1)^2));
end
function mout = myval(x)
y0 = [0;x(1);0;x(2)]; % initial poiint for equation
sol = ode45(@odefn,[0 2],y0);
yend = deval(sol,2);
mout = [yend(1) - 5;yend(3) - 5];
end
Alan Weiss
MATLAB mathematical toolbox documentation

Community Treasure Hunt

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

Start Hunting!

Translated by