Using function handles in a ODE function
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Pavitra Jain
le 11 Août 2022
Réponse apportée : Torsten
le 12 Août 2022
I am working on an optimisation problem for which i created a fitness function. From the input of the fitness we get some symbolic expression which i kept as a function handle in this case. And those expression will be used in the ODE function which will then later be solved in main fitness function using ode45 solver. In trying to do so, I am getting following error:

Next is my ODE function 

then comes the declaration of wdwdX wdx and w2. Names of the function handle looks a bit wierd but they were kept for the reason of their origin which is actually a little complex and hence the names are like this.
Here is the declaration of function handles in the main fitness function:
function y = acceleromet(x)
global wdwdX wdx w2
wdwdX = @(t) -6*(x(1)*t.^3 + x(2)*t.^2 + x(3)*t + x(4))*(3*x(1)*t.^2 + 2*x(2)*t + x(3));
wdx = @(t) -1*(6*(x(1)*t.^3 + x(2)*t.^2 + x(3)*t + x(4))*(3*x(1)*t.^2 + 2*x(2)*t + x(3))^2 + 3*(x(1)*t.^3 + x(2)*t.^2 + x(3)*t + x(4))^2*(6*x(1)*t + 2*x(2)));
w2 = @(t) (x(1)*t.^3 + x(2)*t.^2 + x(3)*t + x(4))^2;
xRange = [0,L];
yinit = [0 0 M V];
sol = ode45(@deflection,xRange,yinit);
xinit = linspace(xlow,xhigh,300);
Sxinit = deval(sol,xinit);
end
using Sxinit i further solve it.
So, I request anyone who can help me with this problem.
0 commentaires
Réponse acceptée
Torsten
le 12 Août 2022
x, L, M, V, xlow, xhigh are not given.
function y = acceleromet(x)
wdwdx = @(t) -6*(x(1)*t.^3 + x(2)*t.^2 + x(3)*t + x(4))*(3*x(1)*t.^2 + 2*x(2)*t + x(3));
wdx = @(t) -1*(6*(x(1)*t.^3 + x(2)*t.^2 + x(3)*t + x(4))*(3*x(1)*t.^2 + 2*x(2)*t + x(3))^2 + 3*(x(1)*t.^3 + x(2)*t.^2 + x(3)*t + x(4))^2*(6*x(1)*t + 2*x(2)));
w2 = @(t) (x(1)*t.^3 + x(2)*t.^2 + x(3)*t + x(4))^2;
xRange = [0,L];
yinit = [0 0 M V];
sol = ode45(@(t,y)deflection(t,y,wdwdx,wdx,w2),xRange,yinit);
xinit = linspace(xlow,xhigh,300);
Sxinit = deval(sol,xinit);
end
function dydt = deflection(t,y,wdwdx,wdx,w2)
y0 = y(1);
y1 = y(2);
y2 = y(3);
y3 = y(4);
dy0dt = y1;
dy1dt = y2;
dy2dt = y3;
dy3dt = (wdwdx(t)*y3 + wdx(t)*y2 + 12*2300*9.81/169e9)/w2(t)
dydt = [dy0dt;dy1dt;dy2dt;dy3dt];
end
0 commentaires
Plus de réponses (1)
Walter Roberson
le 11 Août 2022
Your Y1, Y2, Y3 are numeric. Your dY3dt is a function handle. You try to put those all together in one array.
Your function needs to return a pure numeric array, not a function handle.
In other words, you should remove the @(t,y) from that line of code.
2 commentaires
Voir également
Catégories
En savoir plus sur Ordinary Differential Equations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
