problem in creating function file using ode45
1 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
Torsten
2023-3-11
编辑:Torsten
2023-3-11
Be careful with the order of the solution vector Y. It is (y, Dy, x, Dx). You have to supply the vector of initial values "ic" in the same order !
rng("default")
clc
clear all
close all
O=rand;
a=rand;
g=9.81;
L=rand;
[y]=coupled_ode(O, a, g, L);
Disp=y;
function [y] = coupled_ode(Onum, anum, gnum, Lnum)
syms O a g L x(t) y(t) t Y
dx = diff(x);
d2x = diff(x,2);
dy = diff(y);
d2y = diff(y,2);
Eq1 = d2x == 2*O*sin(a)*dy - (g/L)*x(t);
Eq2 = d2y == -2*O*sin(a)*dx - (g/L)*y(t);
[VF,Subs] = odeToVectorField(Eq1, Eq2)
VF = subs(VF,[O a g L],[Onum anum gnum Lnum]);
ftotal = matlabFunction(VF,'Vars',{t,Y})
tspan = [0 25]; % Choose Appropriate Simulation Time
ic = [0 1 0 1]; % Choose Appropriate Initial Conditions
[t,y] = ode45(ftotal, tspan, ic);
figure
plot(t, y)
grid
legend(string(Subs))
end
2 个评论
Torsten
2023-3-12
编辑:Torsten
2023-3-12
It's always a good idea if programming parts that serve different tasks have their own subfunctions. But in your case, I don't understand why and how you divided your code in the two different .m files.
E.g. to have control over what is done in the function coupled_ode, you will have to pass tspan, the initial conditions for the variables and the parameter values for O, a, g and L. The values returned should be t and y. The plotting part also should be done in the main program because it makes no sense to do the plotting in coupled_ode while passing the results as output parameters back to the calling program.
I'd arrange the code as
rng("default")
clc
clear all
close all
O = rand;
a = rand;
g = 9.81;
L = rand;
f = function_to_integrate(O, a, g, L)
tstart = 0.0;
tend = 25.0;
tspan = linspace(tstart,tend,100); % Choose Appropriate Simulation Time
ic = [0 1 0 1]; % Choose Appropriate Initial Conditions
[t,y] = ode45(f, tspan, ic);
figure
plot(t, y)
grid
legend(string(Subs))
function f = function_to_integrate(Onum, anum, gnum, Lnum)
syms O a g L x(t) y(t) t Y
dx = diff(x);
d2x = diff(x,2);
dy = diff(y);
d2y = diff(y,2);
Eq1 = d2x == 2*O*sin(a)*dy - (g/L)*x(t);
Eq2 = d2y == -2*O*sin(a)*dx - (g/L)*y(t);
[VF,Subs] = odeToVectorField(Eq1, Eq2);
VF = subs(VF,[O a g L],[Onum anum gnum Lnum]);
f = matlabFunction(VF,'Vars',{t,Y});
end
更多回答(1 个)
Alan Stevens
2023-3-10
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 个评论
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!