如何在 matlab 中对 symfun 类型的函数使用 interp1 函数?

How to use interp1 function in matlab on a function of type symfun?

我的 matlab 脚本中有微分方程组的解。我定义我的函数如下:

syms V_x(t) V_y(t) X(t) Y(t);
ode_V_x = diff(V_x,t) == -(B/m)*V_x;
ode_V_y = diff(V_y,t) == -g - (B/m)*V_y;
ode_X = diff(X,t) == V_x;
ode_Y = diff(Y,t) == V_y;

然后我用适当的初始条件解决它们如下:

[V_xSol(t), V_ySol(t), XSol(t), YSol(t)] = dsolve(odes,conds);

例如 Y(t) 的解是:

YSol(t) = exp(-t/5)*((981*exp(t/5))/4 - 12891678040772023/35184372088832) - (981*t)/20 + 4262710785985975/35184372088832

现在我需要找到 Y(t) = 0 的时间值。我考虑过通过执行以下命令在 matlab 中使用 interp1 函数: t_f = interp1([0,5],YSol,0); 但它不起作用。错误显示:Values V must be of type double or single。 在 matlab 中对 symfun 类型的函数使用 interp1 的正确方法是什么? P.S。自变量 't' 之前没有被定义为向量(我知道 matlab 喜欢向量)。

我不确定 interpl 功能是否适合这项工作。您可以使用 fsolve 来达到您想要的结果。

fsolve的用法:

fsolve(function handle, initial guess)

这是在下面的 matlab 中针对您的问题实现的:

t = fsolve(@(t)exp(-t/5)*((981*exp(t/5))/4 - 12891678040772023/35184372088832) - (981*t)/20 + 4262710785985975/35184372088832, 5)

在这种情况下,答案是 4.3243。请注意,答案取决于您提供给求解器的初始猜测。在你的情况下,我注意到任何高于 2 的初始值都应该有效。任何其他方式都会收敛到 different/wrong 答案。

您可以在此处阅读有关 fsolve 的更多信息: https://www.mathworks.com/help/optim/ug/fsolve.html