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.
댓글 수: 0
채택된 답변
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
댓글 수: 0
추가 답변 (1개)
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
