problem in creating function file using ode45
显示 更早的评论
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.
采纳的回答
更多回答(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 个评论
Susmita Panda
2023-3-11
编辑:Susmita Panda
2023-3-11
Alan Stevens
2023-3-11
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
2023-3-11
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
2023-3-11
Walter Roberson
2023-3-12
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
类别
在 帮助中心 和 File Exchange 中查找有关 Numeric Solvers 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



