1

我试图通过使用RK2和RK4的方法在matlab中解决洛伦兹系统。我对这两种方法都有一个脚本,现在的问题是如何收敛以下

y(1) = @(t,y) 10*(y(2)-y(1));
y(2) = @(t,y) y(1)*(28-y(3))-y(2);
y(3) = @(t,y) y(1)*y(2)-8*y(3)/3;

简单地转换为 y 的列向量。

这是我所希望的,但它从未奏效:

y = zeros(3,1);
y(1) = @(t,y) 10*(y(2)-y(1));
y(2) = @(t,y) y(1)*(28-y(3))-y(2);
y(3) = @(t,y) y(1)*y(2)-8*y(3)/3;

以下是我的 RK2 函数。我的 RK4 和这个 RK2 很相似,这可以帮助你理解为什么我真的需要一个函数向量。

function y = RK2(fcn,lrange,urange,step,init)
%fcn = vector of functions
%lrange = lower bound
%urange = upper bound
%step = number of steps
%init = initial value

row = size(fcn,1);
stepsize = (urange-lrange)/step;
y = zeros(row,step);
%initializing vector of y
y(:,1) = init;
%initial condition
t = zeros(1,step+1);
%initializing vector of t

if row ~= size(init,1)
    disp('number of functions and number of initial values do not match');
end

for n = 1:step

    t(n) = (n-1)*stepsize;
    t(step+1) = urange;
    y1 = stepsize.*fcn(t(n),y(:,n));
    y2 = stepsize.*fcn(t(n) + stepsize/2, y(:,n) + y1./2);
    y(:,n+1) = y(:,n) + y2;

end
4

2 回答 2

3

或者,

f = cell(3,1); % create a cell array

% initialize

    f{1} = @(t) t^2;
    f{2} = @(t) cos(2*t);
    f{3} = @(t) 4*(t^3);

% access properties

    size(f)(1); % access the number of functions
    f{1} % access the first function
    f{2}(17) % evaluate the second function at x = 17

元胞数组是一种具有索引数据容器的数据类型,称为元胞,其中每个元胞可以包含任何类型的数据。

文档:https ://www.mathworks.com/help/matlab/ref/cell.html

或者,在 Octave 中:

f = cell(3,1); # create a cell array

# initialize

    f(1) = @(t) t^2;
    f(2) = @(t) cos(2*t);
    f(3) = @(t) 4*(t^3);

# access properties

    size(f)(1); # access the number of functions
    f{1} # access the first function
    f{2}(17) # evaluate the second function at x = 17
于 2019-04-01T16:57:57.803 回答
2

这应该可以,只需使函数输出一个向量:

y = @(t,y) [10*(y(2)-y(1)), y(1)*(28-y(3))-y(2), y(1)*y(2)-8*y(3)/3];
y(1,[1;2;3])
size(y(1,[1;2;3]))
于 2014-12-08T00:42:18.623 回答