微分方程MATLAB2

时间:2016-04-28 22:48:45

标签: matlab

我是MATLAB的新手,我正在努力寻找解决微分方程的方法。我的等式是:d ^ 2x / dt ^ 2 - sin(t)*(dx / dt)= x。我试图解决t = 10并假设t = 0指定初始值。我不知道从哪里开始这个任何帮助都会很棒。

1 个答案:

答案 0 :(得分:0)

我建议使用状态空间建模语法,我们将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

Resulting plot