problem in creating function file using ode45
3 views (last 30 days)
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.
0 Comments
Accepted Answer
Torsten
on 11 Mar 2023
Edited: Torsten
on 11 Mar 2023
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 Comments
Torsten
on 12 Mar 2023
Edited: Torsten
on 12 Mar 2023
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
More Answers (1)
Alan Stevens
on 10 Mar 2023
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
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
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!