problem with solving system of differential equations
7 views (last 30 days)
Show older comments
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.
0 Comments
Answers (3)
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
% 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)
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
2 Comments
Anil
on 6 Mar 2024
does that ode45 inside the myval, helps to find the sationary solution of nonlinear equations through fsolve? Could you please comment on that. Thanks.
Alan Weiss
on 6 Mar 2024
The code works by taking a numerical solution of the ODE starting from [0,0] and evaluating it at time 2. It tries to make the values at time 2 equal to [5,5]; that is why in myval(x) I subtract [5,5] from the final point, so that fsolve can try to make the function equal to [0,0].
Alan Weiss
MATLAB mathematical toolbox documentation
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.
0 Comments
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)
See Also
Categories
Find more on Surrogate Optimization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!