How to use max function for second order ODE
古いコメントを表示
I have a second order ODE y'' + 0.5y + 2y = cos(5t^2 ) , y(0) = y'(0) = 0 that I have graphed and computed using ODE45.
tspan= [0 20];
xinit=[0 0];
[t,x] = ode45(@f,tspan,xinit);
max_val= max(t,x);
plot(t,x);
savefig('oscillator.fig')
My function for f is
function dxdt=f(t,x)
dxdt=[x(2);cos(5*t^(2))-0.5*x(1)+2*x(2)];
end
However, the max_val return 0,0 which is not mathematically reasonable The code complies and the graph looks exponential in nature. What am I doing wrong?
回答 (1 件)
Walter Roberson
2019 年 11 月 10 日
0 投票
Your t will be a column vector. Your x will be a 2D array with the same number of rows that t has, and with 2 columns.
max(column vector, 2d array) is (R2016b onwards) treated the same as max(repmat(column vector, 1, number of columns in 2d array), 2d array) -- that is, the column vector would be replicated until it was a 2D array the same size as the second input.
Once that was done, the meaning of max(2d array, another 2d array) is that the maximum of each corresponding position is taken.
So what you are doing is comparing each element of the x array to the time that the entry was generated at, and taking the maximum between those (time vs ode value)
If you want to know the maximum of each x entry, then max(x) not max(t,x)
カテゴリ
ヘルプ センター および File Exchange で Ordinary Differential Equations についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!