Using function handles in a ODE function
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (1)
Walter Roberson
am 11 Aug. 2022
0 Stimmen
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 Kommentare
Pavitra Jain
am 11 Aug. 2022
Bearbeitet: Pavitra Jain
am 11 Aug. 2022
Pavitra Jain
am 12 Aug. 2022
Kategorien
Mehr zu Ordinary Differential Equations finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
