problem in creating function file using ode45
Show older comments
I am trying to solve a second order coupled ode using ode45, however I am facing issues in making it function file and providing variables as input. How to convert symbolic input to numerical in main file using ode45.
Accepted Answer
More Answers (1)
Are you looking for something like this? (I might have misinterpreted your equations!)
O=1;
a=pi/2;
g=9.81;
L=100;
tspan = [0 15]; % Choose Appropriate Simulation Time
ic = [0 1 0 1]; % Choose Appropriate Initial Conditions
[t,y] = ode45(@(t,y) ftotal(t,y,O,a,g,L), tspan, ic);
figure
plot(t, y)
grid
legend('y1','dy1/dt','y2','dy2/dt')
function dydt=ftotal(~,y,O, a, g, L)
y1 = y(1); v1 = y(2); y2 = y(3); v2 = y(4);
dydt = [ v1;
2*O*sin(a)*y2 - g/L;
v2;
-2*O*sin(a) - g/L*y1];
end
6 Comments
Susmita Panda
on 11 Mar 2023
Edited: Susmita Panda
on 11 Mar 2023
Alan Stevens
on 11 Mar 2023
Hmm! You need an equation for yddot. What are your actual equations? Try uploading them exactly in mathematical notation (using an image if necessary).
Susmita Panda
on 11 Mar 2023
Call ode45 with the function
fun = @(t,y) [y(2); xddot; y(4); yddot]
where xddot and yddot are computed as
syms a b c d e f g h i t x y xdot ydot xddot yddot
eqn1 = (1-e)*xddot + e*yddot == -2*a*xdot - x - sin(b*t) + c*(x-y) + d*(xdot-ydot)
eqn2 = (g-h)*xddot - (i+g)*yddot == -f*(x-y)
solve([eqn1 eqn2],[xddot yddot])
Susmita Panda
on 11 Mar 2023
Walter Roberson
on 12 Mar 2023
After you get the xddot and yddot using solve(), call odeFunction to create something to pass to ode45() .
I recommend following the workflow shown in the first example to odeFunction
Categories
Find more on Numeric Solvers in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


