How to solve a coupled nonlinear second order equations with a loop?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
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.
採用された回答
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 件のコメント
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?
Bjorn Gustavsson
2019 年 6 月 6 日
編集済み: Bjorn Gustavsson
2019 年 6 月 6 日
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
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- 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)
