How to solve a system of nonlinear 2nd order differential equations?

Hi there,
I have a challenge solving a system of differential equations 2nd order. I do not receive an error message but have rather strange results... Could you please have a look on what I did and comment on it. If you think the way it was done is fine, I may have the bug some where else...
function dHbar = diffH(tspan,H)
h1 = H(1);
dh1 = H(2);
ddh1 = H(3);
h2 = H(4);
dh2 = H(5);
ddh2 = H(6);
h3 = H(7);
dh3 = H(8);
ddh3 = H(9);
% etc
% the equations are very long. In short the look similar to the following where f(h1,dh1) means some expression as a function of ...
% A1 to A3 are known
ddh1 = ( A1 * f(h1,dh1) + A2 * f(h2,dh2,ddh2) + A3 * f(h3,dh3,ddh3));
ddh2 = ( A2 * f(h2,dh2) + A1 * f(h1,dh1,ddh1) + A3 * f(h3,dh3,ddh3));
ddh3 = ( A3 * f(h3,dh3) + A1 * f(h1,dh1,ddh1) + A2 * f(h2,dh2,ddh2));
dHbar = [h1; dh1; ddh1; h2; dh2; ddh2; h3; dh3; ddh3];
Then I call it with
[T,Hbar] = ode45('diffH',tspan,H); % with tspan - time and H - starting values
As mentioned above, I receive some solution, but it seems strange.
Thanks a lot for your help in advance. Cheers, Franziska

7 Comments

you want to integrate a set of nonlinear equations? or solve a set of nonlinear equations?
I want to solve it. h1,h2,h3 are functions of time; dh1,dh2,dh3 are the related velocites and ddh1,ddh2,ddh3 are the related accelerations
out of these parameters that you are listing above, which ones are known and which ones are unknown and need to be solved for?
I know A1,A2,A3 and the initial values for all positions, velocities, accelerations -> h1,dh1,ddh1, h2,dh2,ddh2, h3,dh3,ddh3
I am concerned whether it is even possible to solve such a system using Matlab.
Franziska, now, I'm understanding yoru problem better.
I think you have not formulated your problem in the proper way that ODE45 requires you to.
In the function dHbar = diffH(tspan,H) the return value of this function, should only include the velocity and acceleration. If you see
doc ode45
you will notice that the return of the function that needs to be integrated with ODE45 is the derivative of the input. So you need to have position and velocity as input and velocity and acceleration as output.
You need to write H as (h1, dh1,h2,dh2,h3,dh3) and then write your fucntion like this:
function dHbar = diffH(tspan,H)
h1 = H(1);
dh1 = H(2);
h2 = H(3);
dh2 = H(4);
h3 = H(5);
dh3 = H(6);
% dHbar(1) = dh1;
% dHbar(2) = ddh1;
% dHbar(3) = dh2;
% dHbar(4) = ddh2;
% dHbar(5) = dh3;
% dHbar(6) = ddh3;
% already known, need not be computed:
dHbar(1) = dh1;
dHbar(3) = dh2;
dHbar(5) = dh3;
ddh1 = ( A1 * f(h1,dh1) + A2 * f(h2,dh2,ddh2) + A3 * f(h3,dh3,ddh3));
ddh2 = ( A2 * f(h2,dh2) + A1 * f(h1,dh1,ddh1) + A3 * f(h3,dh3,ddh3));
ddh3 = ( A3 * f(h3,dh3) + A1 * f(h1,dh1,ddh1) + A2 * f(h2,dh2,ddh2));
dHbar = [dh1; ddh1; dh2; ddh2; dh3; ddh3];
end
Thanks Babak for your help and patience. I have now rewritten the entire system to M(y,t)*y' = f(t,y) and work with ode23t due to singular matrix. Thanks again for your help! Franziska
I think after your write your equations in the form I'm mentioning above, you can solve it with any ODE solver like ODE45. It doesn't seem to be a complicated case.

Sign in to comment.

Answers (2)

You need to use the optimization toolbox and it's fsolve(.) routine. You can embed all your nonlinear functions (can be an integrated ODE or whatever)as the arguments of fsolve, assigning thos functions to be the ones that are needed to be optimized.

1 Comment

Thanks for your answer Babak, according to the matlab-help fsolve is not applicable for systems of differential equations. I need the time dependency.
Cheers, Franziska

Sign in to comment.

My code is very sensitive to the choice of initial velocity values. I have some questions here:
1) Is there any possibility to search for appropriate initial velocity values similar to decic for ode15i? 2) Does it make sense if I try with ode15i and decic? Are there any limitations due to singular and time dependent mass matrix? I couldn't find a 2nd order problem similar to my challenge but coded in ode15i which might help me.
Thanks.
Franziska

1 Comment

I need to solve a system of 3 equations in the variable x1,x2,x3, I do not know how write the ode function that takes into account a term of a second order derivative of x2 in equation 1. I have a system like that:
ddx1=F1(t)-B1*dx1-M3*ddx3-B3*dx3-M2*ddx2-B2*dx2
ddx2=F2(t)-B2*dx2-K2*dx-M1*ddx1-B1*dx1-M2*ddx2-B2*dx2
ddx2=F3(t)-B3*dx3-K3*dx3-M1*ddx1-B1*dx1-K1*dx1-M2*ddx2-B2*dx2
I do not know how write in the ode function for this system. Can you please explain o write an example of the ode function required to solve a non linear system like that? I would be greateful
Best regards Alessandro Antonini

Sign in to comment.

Asked:

on 21 Feb 2013

Community Treasure Hunt

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

Start Hunting!