How to solve a coupled nonlinear second order equations with a loop?
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
The equations are as following images, i=1,2,.......20,alpha and lambda are known.I have code some,but there some mistakes I don't know how to solve.I need some help. Best wishes to you.
Accepted Answer
Bjorn Gustavsson
on 4 Jun 2019
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 Comments
luck
on 5 Jun 2019
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.
Bjorn Gustavsson
on 5 Jun 2019
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
luck
on 6 Jun 2019
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?
Bjorn Gustavsson
on 6 Jun 2019
Edited: Bjorn Gustavsson
on 6 Jun 2019
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
luck
on 7 Jun 2019
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
Bjorn Gustavsson
on 7 Jun 2019
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
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)