How to solve a coupled nonlinear second order equations with a loop?

 Respuesta aceptada

In your ode_coupled_chain you will overwrite dz(i+3) and the other higher dz components since you increment i with 2 in the for-loop. That can't possibly be what you want. If you have 4 coupled differential equations for each index, you should get 8 first-order ODEs, so the assignments might be changed to something like:
dz(1 + 8*(i-1)) = ...
dz(2 + 8*(i-1)) = ...
Seems like a "fun" punishment to get this coded right without misstyping - good luck.
Last advice: print it out on paper single-sided and cross off terms with different-coloured pens.
HTH

6 comentarios

Thank you very much!
I changed the code in the attachments,but there are some problems in code.Warning me insufficient number of input parameters,I have no idea about that.
Ah, I think you have to merge your 4 variable z,w,xi,eta into one column vector, to match your differential equations, such that you get something like this:
y = [z;dzdt;w;dwdt;xi;dxidt;eta;detadt];
% Then modify the definition of ode_coupled_chain to accept the vector y
% instead of z, w, xi, eta:
dy = ode_coupled_chain(t,y,alpha,lambda)
In ode_coupled_chain.m you can extract the variable you need:
function dy = ode_coupled_chain(t,y,alpha,lambda)
z = y(1:8:end);
dzdt = y(2:8:end);
w = y(3:8:end);
dwdt = y(4:8:end);
% etc...
% and then your ode-definitions - you still need the loops I think to
% get the derivatives for all your chain-links? you should have something like
% (4*2)*N components in dy.
end
After that you have to set the initial conditions component by component, whatever they are,
select values for alpha and lambda, and solve away:
N = 20;
y0 = randn(8*N,1);
alpha = 1;
lambda = 0.3;
t_span = [0,10];
[T,Y] = ode45(@(t,y) dy = ode_coupled_chain(t,y,alpha,lambda),t_span,y0);
HTH
Thank you very much!
If I do this,z(),w(),xi(),eta() varibles in ode_coupled_chain of all equations should be modified to y() form,right?
Well you can do that, but in my personal experience, that is just too large risk for typos and misspellings in the the ode-expressions. Yours would be rather challenging for me to type into code correctly. So my advice, just for simplicity of writing the code and read it (manually for checking, verification and debugging purposes) is just to extract the z, w eta and xi components from the y-vector, as I outlined above:
function dy = ode_coupled_chain(t,y,alpha,lambda)
z = y(1:8:end);
dzdt = y(2:8:end);
w = y(3:8:end);
dwdt = y(4:8:end);
xi = y(5:8:end);
dxidt = y(6:8:end);
eta = y(7:8:end);
detadt = y(8:8:end);
By defining those variables with names that way, you get to write the long expressions for the second derivatives with the same variable-names as you have in the images you attached. If that doesn't kill the performance of the ODE-integration it is, at least to me, a price well worth paying - you at least have a fighting chance to read the code correctly, with plain indexing of y, that seems impossible.
HTH
Thank you very much!
Sorry to disturb you again,I have modified code in your way,but there are some problems.Warning the array index must be a positive integer or a logical value.I think it's right in my code,the index is positive.My code have upload in the attachment.
Looking forward to your suggestions
It seems to me that you would still have to loop inside the ode_coupled_chain-function. The way I interpret your code is that you have 4 2nd-order ODEs for each of your 20 nodes. That would give you ~8*20 ODEs to integrate, and you only set i to 1. The thing is that the first (and last?) chain-link will be different since it(they) most likely will be fixed in some way. So my guess is that the equations for chain-link-position for the first (and last) will be fixed (or moving in some prescribed way). To model that you have to treat those equations differently and then loop over i from 2 to 19.
In situations like these when things don't go as you plan you have to learn to use matlab's debug feature. At the matlab-prompt type:
dbstop if error
Then rerun your code, when it fails it will stop excecusion at the point of error and give you a prompt at that place in the call-stack so that you can inspect variables and jump up and down the call-stack and check where things go kaboom.
HTH

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

el 4 de Jun. de 2019

Comentada:

el 7 de Jun. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by