Solving differential equations with inital conditions
1 回表示 (過去 30 日間)
古いコメントを表示
Attempting to solve a differential with inital conditions and a time range and then plot the function and it's derivative on a graph and to determine the time when the function first crosses zero.
x0 = [20;0]; %Inital conditions
tr = [0; 300]; %Time range for our function
[t,y]=ode45(@fun, tr, x0);
Pi=find(t<0); %Find elements of Z less than 0
Pi1=Pi(1); % the array index for first Z element<0
zz=Z(Pi1-4:Pi1+3); %Pick 8 elements from Z
tt=t(Pi1-4:Pi1+3); %Pick 8 elements from t
% (tt, zz) will be used for interpretation
plot(t,y)
plot(tt,zz)
function dydx = fun(t,x)
%dx(1)/dt = dy/dt = x(2)
%dx(2)/dt = d^2y/dt^2
dydx = [x(2);
0.375*sign(x(2))*(x(2))^2+0.00074*x(1)];
end
The error I get is shown below. If anyone can help that'd be great
Warning: Failure at t=2.082134e+01. Unable to meet
integration tolerances without reducing the step size below
the smallest value allowed (5.684342e-14) at time t.
> In ode45 (line 360)
In ME2602Ahmed_P4_1 (line 4)
Index exceeds the number of array elements (0).
Error in ME2602Ahmed_P4_1 (line 7)
Pi1=Pi(1); % the array index for first Z element<0
1 件のコメント
Star Strider
2020 年 3 月 8 日
The Warning:
Warning: Failure at t=2.082134e+01. Unable to meet
integration tolerances without reducing the step size below
the smallest value allowed (5.684342e-14) at time t.
is due to ‘fun’ becoming infinite at about that point.
It appears to simply be the nature of your differential equations. I see no specific problem (such as divide-by-zero) that could otherwise cause that.
回答 (1 件)
Guru Mohanty
2020 年 3 月 11 日
Hi, I understand you are getting warning and error in solving the system of differential equation. The warning is due to the ode45 solver is getting a singularity or discontinuity at t=20.82 in your time range [0:300]. So, the solver could not find solution further. You can also visualize it by plotting ‘y’ vs ‘t’.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/276510/image.png)
After changing the range to [0:20] The solver can solve the differential equation.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/276511/image.png)
The error is due to the find function tries to find the index of negative values of t. But as there are no negative value in t, the command
Pi=find(t<0);
returns an empty array. So
Pi1=Pi(1); % the array index for first Z element<0
Will pass an error.
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Ordinary Differential Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!