ploting second derivative from second order differential equationn
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hi, Beloow is my code and i am solving second order differential equation numericaly and i am getting plot of first and zeroth derivation from ode45. is there any way to plot the second derivative as well by just calling ode45 ?
function fval = simulation( t,y )
x=y(1);
v=y(2);
c=6850;
m=450;
k=0;
f=2650;
e=535;
w=4410;
fval(1,1)=v;
%fval(2,1)= -(c*v+k*x)/m
fval(2,1)=(-c*v-f-e+w+k*x)/m;
end
%%%%main file%%%%%%
c=6850;
m=450;
k=0;
f=2650;
e=535;
w=4410;
Y0= [0;3.86];
tspan= [0 1];
options= odeset ('Events', @event);
[tsol,ysol] = ode45(@(t,y) simulation(t,y), tspan, Y0,options);
figure
plot(tsol,ysol(:,1), 'r-');
hold on;
figure
plot(tsol,ysol(:,2), 'g-');
hold on;
figure
採用された回答
Jan
2021 年 3 月 18 日
The easiest solution is to let the function to be integrated accept vectors as input and call it with the time steps and positions replied by ODE45:
[tsol, ysol] = ode45(@(t,y) simulation(t,y), tspan, Y0,options);
dy = simulation(tsol, ysol.').';
d2y = dy(:, 2);
function fval = simulation(t, y)
x = y(1, :);
v = y(2, :);
c = 6850;
m = 450;
k = 0;
f = 2650;
e = 535;
w = 4410;
fval(1, :) = v;
fval(2, :) = (-c * v - f - e + w + k * x) / m;
end
12 件のコメント
Jan there is some syntax error in your code line 2
The 2nd line of my code is empty. So please post the error message, if you get one. Letting me guess which error appears anywhere in the code, is not efficient.
By the way: My code contains only the snippet you have to change to get the 2nd derivative. You have to insert this in your code.
oh it was fine thank you for the help jan :)
Muhammad Sarmad Zahid
2021 年 3 月 19 日
編集済み: Jan
2021 年 3 月 19 日
i have one more question i have this code. I am following you guidance to plot g against time. g is a contant value. How can i plot g ?
Y0= [-0.75 ;0];
tspan= [0 2];
options= odeset ('Events', @freefall);
[tsol,ysol] = ode45(@(t,y) pra(t,y), tspan, Y0,options);
figure
plot(tsol,ysol(:,1), 'r-');
hold on;
figure
plot(tsol,ysol(:,2), 'g-');
hold on;
figure
%%%%function%%%
function fval = pra( t,y )
g=9.8;
y(1);
y(2);
% c=6850;
fval = [y(2); g)];
end
There is a not matching parenthesis in your code in:
fval = [y(2); g)];
You can plot the 2nd component of the output of pra() in exactly the same way, that I've showed you for simulation(): Convert pra() such that it accept vectors as input and use the output of ODE45 to get the wanted values. Afterwards use plot() as for the other components.
Muhammad Sarmad Zahid
2021 年 3 月 19 日
編集済み: Jan
2021 年 3 月 19 日
i am tring to do that but its not returning me a contant value of g instead it is returning me big values starting from 2.7 to 55 m/s^2. can you please guideme by writing a code? it would be really appreaciat if you check that code as well.
you can chekc the values of f as well
[t,y] = ode45(@(t,y) pra(t,y), [0 1], [-0.75 ; 0 ]);
f=simulation( t,y.').'
%%%%function%%%
function fval = pra( t,y )
g=9.8;
y(1);
y(2);
% c=6850;
fval = [y(2); g.*((t).^0)];
end
Please use the tools to format the code. This makes it easier to read you code. I've done this for you, but it is more efficient, if you do this by your own.
I've showed you how to convert the function simulation() such, that it accepts matrices as input. Then you can use the output of ODE45 to obtain the 2nd derivative also.
Now you have to modify your function pra() accordingly.
These lines are meaningless:
y(1);
y(2);
Instead of:
fval = [y(2); g.*((t).^0)];
you need:
fval = [y(2, :); repmat(g, size(t))];
Using g.*((t).^0) is smart, but the power operation is very expensive. repmat() is more efficient, or alternatively:
fval = [y(2, :); g * ones(size(t))];
Afterwards, do not call "f=simulation( t,y.').'", but:
df = pra(t, y.').';
plot(t, df(:, 2))
Error using vertcat
Dimensions of arrays being concatenated are not consistent.
Error in pra (line 10)
fval = [y(2, :); g * ones(size(t))];
Error in practicemailfile (line 20)
df = pra(tsol, ysol.').';
Then call it like this:
df = pra(tsol.', ysol.').';
% ^^
it worked. Thank you so much jan
Muhammad Sarmad Zahid
2021 年 3 月 20 日
編集済み: Jan
2021 年 3 月 20 日
jan kidly guide me on this question as well
Jan
2021 年 3 月 20 日
But this is the same question again?
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Solver Outputs and Iterative Display についてさらに検索
タグ
参考
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)
