Using function handles in a ODE function

4 次查看(过去 30 天)
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.

采纳的回答

Torsten
Torsten 2022-8-12
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

更多回答(1 个)

Walter Roberson
Walter Roberson 2022-8-11
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 个评论
Pavitra Jain
Pavitra Jain 2022-8-11
编辑:Pavitra Jain 2022-8-11
I tried that earlier but it gave a different error that it cannot identify operator “*” for function handle.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Ordinary Differential Equations 的更多信息

产品


版本

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by