简体   繁体   English

创建要在ode45中使用的函数

[英]Create function to use in ode45

这是一个较大项目的一部分

So this is part of a larger project but I am stuck on number two of this section. 因此,这是一个较大的项目的一部分,但我仍然停留在本节的第二部分。 I rewrote the system to get it in the required form: 我重写了系统,以所需的形式获取它:

dx(1)/dt = x(2) dx(1)/ dt = x(2)

dx(2)/dt = (-(M+m)/mL))x(4) + 1/(mL)u dx(2)/ dt =(-(M + m)/ mL))x(4)+ 1 /(mL)u

dx(3)/dt = x(4) dx(3)/ dt = x(4)

dx(4)/dt = -(mg/M)x(1) + (1/M)u dx(4)/ dt =-(mg / M)x(1)+(1 / M)u

After substituting the variables given in the problem I wrote the funcion: 替换问题中给定的变量后,我编写了函数:

function dx = fun(t,x)
dx = zeros(4,1);
dx(1) = x(2);
dx(2) = -((2+.1)/(.1*.5)).*x(4);
dx(3) = x(4);
dx(4) = -((.1*9.81)/2).*x(1);
end

I am confused on how to implement u(t) = 0 and how to create the theta function. 我对如何实现u(t)= 0以及如何创建theta函数感到困惑。

Any help even if its just pointing me in the right direction would be amazing. 即使只是将我指向正确的方向,任何帮助都将是惊人的。 Thank you in advance :) 先感谢您 :)

It's easy. 这很容易。 You implement theta as another state. 您将theta实现为另一种状态。 This is possible since you know the derivative, which does not even depend on the other states. 这是可能的,因为您知道不依赖于其他状态的导数。 To be more precise, you should add two more states here. 更精确地说,您应该在此处添加另外两个状态。 One for theta and one for theta_dot . 一个用于theta ,一个用于theta_dot

dx(5) = x(6)  % Theta'
dx(6) = -0.1  % Theta''

And by the way, you can also pass additional variables to your differential equation. 而且,您还可以将其他变量传递给微分方程。 You just add more arguments to it 您只需添加更多参数

function dx = diffeq(t,x,parameters)
  ...
end

and create a new function handle where you execute the ODE solver 并在执行ODE求解器的地方创建一个新的函数句柄

[T,X] = ode45(@(t,x)diffeq(t,x,parameters),t_span,X0, ode_options);

This is just a hint since you're using magic numbers in your differential equation function. 这只是一个提示,因为您在微分方程函数中使用了幻数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM