Ode45 initial conditions iteration
1 view (last 30 days)
Show older comments
Adedayo Odeleye
on 19 Nov 2020
Edited: Alan Stevens
on 19 Nov 2020
I am trying to solve the following 2nd order differential equation, which works fine but I need to iterate the initial conditions, so everytime the ode is solved, the last values for q(:,1) and q(:,2) should be the new value for X and Y respectivley.
%solve_2nd_order_diff
duf=@(t,q)[q(2);f*w^2*cos(w*t)-(Cmech/J)*q(2)-(K1/J)*q(1)-(K3/J)*q(1)^3];
for r=1:length(Rad_speed)%freq of harmonic fluctuations, rad/s
[t,q]=ode45(duf,Time,[X Y]);
Max_values=max(q);
plot(t,q(:,1),'k')
xlabel('TIme,s')
ylabel('Relative Displacement, Rad')
grid
end
0 Comments
Accepted Answer
Alan Stevens
on 19 Nov 2020
Like so
...
[X Y] = ...
for r=1:length(Rad_speed)%freq of harmonic fluctuations, rad/s
[t,q]=ode45(duf,Time,[X Y]);
[X Y] = [q(end,1) q(end,2)];
...
5 Comments
Alan Stevens
on 19 Nov 2020
Edited: Alan Stevens
on 19 Nov 2020
Excellent, but notice that your plot and your Max_values will only apply for the last iteration of the loop (they are overwritten every time through the loop). If that is what you want it is probably best to put that piece of coding outside the loop at the end. To store the Max_values (rather than just have them printed in the command window every iteration), you could use
Max_values(r) = max(q);
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!