在Matlab中求解不同输入的微分方程

时间:2015-03-30 01:54:43

标签: matlab

如何仅及时查看端点(在本例中为12)并查看在该点更改参数(如其中一个微分方程中的标量)如何更改数据?

function test

options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-5]);
[T,Y] = ode45(@rigid,[0 12],[0 1 1],options);

plot(T,Y(:,1),'-',T,Y(:,2),'-.',T,Y(:,3),'.')

    function dy = rigid(t,y)
        dy = zeros(3,1);    % a column vector
        dy(1) = y(2) * y(3);
        dy(2) = -y(1) * y(3);
        dy(3) = -0.51 * y(1) * y(2);

    end

end

1 个答案:

答案 0 :(得分:0)

您只需在ode中添加一个参数,然后将结果累积到单元格数组中。

function test
close all

options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-5]);

%// List of parameter values.
kList = 0:0.5:2;
%// Cells were save the single time histories.
tList = cell(length(kList),1);
yList = cell(length(kList),1);
%// Last values of each integration.
yEnd = zeros(length(kList), 3);

%// Loop over the parameter values.
for i = 1:length(kList)
    %// Select the parameter value.
    k = kList(i);
    %// Call ode45 with k at the end.
    [tList{i},yList{i}] = ode45(@rigid,[0 12],[0 1 1],options,k);
    figure();
    plot(tList{i},yList{i}(:,1),'-', ...
        tList{i},yList{i}(:,2),'-.', ...
        tList{i},yList{i}(:,3),'.');
    yEnd(i,:) = yList{i}(end,:);
end

disp(yEnd);

    %// Make the ode dependent from k.
    function dy = rigid(t,y,k)
        dy = zeros(3,1);    % a column vector
        dy(1) = y(2) * y(3);
        dy(2) = -y(1) * y(3);
        dy(3) = k * y(1) * y(2);

    end

end