【发布时间】:2016-05-05 21:13:01
【问题描述】:
我是 MATLAB 新手,我正在尝试解决一个微分方程。我的等式是:d^2x/dt^2 - sin(t)*(dx/dt) = x。我正在尝试解决 t=10 并假设为 t = 0 指定初始值。我不知道从哪里开始,任何帮助都会很好。
【问题讨论】:
-
您可能会从 The MathWorks 中找到 this blog post。
标签: matlab
我是 MATLAB 新手,我正在尝试解决一个微分方程。我的等式是:d^2x/dt^2 - sin(t)*(dx/dt) = x。我正在尝试解决 t=10 并假设为 t = 0 指定初始值。我不知道从哪里开始,任何帮助都会很好。
【问题讨论】:
标签: matlab
我建议使用状态空间建模语法,我们将 x 视为状态变量 (x) 及其后续导数的向量。
以下是解决初始值问题的示例代码:
(我用的是FreeMat,不过MATLAB应该也一样)
function [] = ode()
% Time
t_start = 0.0;
t_final = 10.0;
% Lets treat x as a vector
% x(1) = x
% x(2) = dx/dt (first derivative of x)
x0 = [0.1; 0]; % Initial conditions
options = odeset('AbsTol',1e-12,'RelTol',1e-6,'InitialStep',1e-6,'MaxStep',1e-2); % stepping tolerances
[t,x] = ode45(@system, [t_start t_final], x0, options); % Run the differential equation solver
hold on
plot(t,x(:,1),'r-')
plot(t,x(:,2),'b-')
hold off
end
% Define the differential equation
function dxdt = system(t,x)
dxdt = [x(2); ... % derivative of x(1) is x(2)
x(1) + sin(t)*x(2)]; % derivative of x(2) is x + sin(t)*dx/dt
end
【讨论】: