Problem with second order ODE solver

Hello everybody,
I'm trying to solve the following equation: y" = sin(y) - (1/t)*y'
I write down the following code for solving the equation but the output does not show anything, do i miss something here?
syms y(t)
[V] = odeToVectorField(diff(y, 2) == sin(y)-(1/t)*diff(y))
M = matlabFunction(V,'vars', {'t','Y'})
sol = ode45(M,[0 30],[5.71 0])
fplot(@(x)deval(sol,x,1), [0, 30])
I really appreciate your help!
Thanks.

回答 (1 件)

Star Strider
Star Strider 2017 年 5 月 9 日

1 投票

The problem is having ‘t’ to include 0 and having ‘t’ in the denominator. This creates a ‘0/0’ condition that equates to NaN, and tha then propagates throughout the integration of your ODE.
The easiest way to avoid that problem is to ‘cheat’, and use eps instead of 0.
This works:
syms y(t)
[V] = odeToVectorField(diff(y, 2) == sin(y)-(1/t)*diff(y))
M = matlabFunction(V,'vars', {'t','Y'})
[T,Y] = ode45(M,[eps 30],[5.71 0]);
figure(1)
plot(T, Y)
grid

2 件のコメント

Yas
Yas 2017 年 5 月 10 日
編集済み: Yas 2017 年 5 月 10 日
Thank you very much! Yes it worked. However I have one more question: 1. why the new code generates to separate plots (orange and blue)? I mean the solution to this eq should be one right?
Star Strider
Star Strider 2017 年 5 月 10 日
My pleasure!
There are two outputs (columns) in ‘Y’, ‘Y(:,1)’ (the derivative) and ‘Y(:,2)’, the solved equation. If you only want the solved equation, the plot changes to:
figure(1)
plot(T, Y(:,2))
grid

サインインしてコメントする。

タグ

質問済み:

Yas
2017 年 5 月 9 日

コメント済み:

2017 年 5 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by