How to use max function for second order ODE
Mostra commenti meno recenti
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?
Risposte (1)
Walter Roberson
il 10 Nov 2019
0 voti
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)
Categorie
Scopri di più su Ordinary Differential Equations in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!