[英]Returning original mathematical function values from ode45 in Matlab?
My intention is to plot the original mathematical function values from the differential equation of the second order below: 我的意图是从下面的二阶微分方程绘制原始数学函数值:
I(thetadbldot)+md(go^2asin(ot))sin(theta)=0
where thetadbldot
is the second derivative of theta
with respect to t
and m,d,I,g,a,o
are given constants. 其中
thetadbldot
是theta
关于t
的二阶导数,并且m,d,I,g,a,o
给出常数。 Initial conditions are theta(0)=pi/2
and thetadot(0)=0
. 初始条件为
theta(0)=pi/2
和thetadot(0)=0
。
My issue is that my knowledge and tutoring is limited to storing the values of the derivatives and returning them, not values from the original mathematical function in the equation. 我的问题是,我的知识和辅导仅限于存储导数的值并返回它们,而不是方程中原始数学函数的值。 Below you can see a code that calculates the differential in Cauchy-form and gives me the derivatives.
在下面,您可以看到一个代码,该代码以柯西形式计算微分并为我提供了导数。 Does anyone have suggestions what to do?
有人建议做什么吗? Thanks!
谢谢!
function xdot = penduluma(t,x)
% The function penduluma(t,x) calculates the differential
% I(thetadbldot)+md(g-o^2asin(ot))sin(theta)=0 where thetadbldot is the second
% derivative of theta with respect to t and m,d,I,g,a,o are given constants.
% For the state-variable form, x1=theta and x2=thetadot. x is a 2x1 vector on the form
% [theta,thetadot].
m=1;d=0.2;I=0.1;g=9.81;a=0.1;o=4;
xdot = [x(2);m*d*(o^2*a*sin(o*t)-g)*sin(x(1))/I];
end
options=odeset('RelTol', 1e-6);
[t,xa]=ode45(@penduluma,[0,20],[pi/2,0],options);
% Then the desired vector from xa is plotted to t. As it looks now the desired
% values are not found in xa however.
Once you have the angle, you can calculate the angular velocity and acceleration using diff
: 一旦有了角度,就可以使用
diff
计算角速度和加速度:
options=odeset('RelTol', 1e-6);
[t,xa]=ode45(@penduluma,[0,20],[pi/2,0],options);
x_ddot = zeros(size(t));
x_ddot(2:end) = diff(xa(:,2))./diff(t);
plot(t,xa,t,x_ddot)
legend('angle','angular velocity','angular acceleration')
which gives the following plot in Octave (should be the same in MATLAB): 在Octave中给出以下图表(在MATLAB中应该相同):
Alternatively, you can work it out using your original differential equation: 或者,您可以使用原始的微分方程来计算:
x_ddot = -m*d*(o^2*a*sin(o*t)-g).*sin(xa(:,1))/I;
which gives a similar result: 得到类似的结果:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.