1

所以我有一个学校任务,我需要计算一堆汽车在道路上相互跟随的位置(AKA 排成一列,所以如果 10 号车 [排在第一个的车] 刹车,那么 9 号车刹车当 9 号车刹车时,8 号车必须刹车等)。

每辆车都遵循“三秒规则”(除了排在最前面的那辆车,他可以随心所欲地行驶,其他所有排队的车都相应地调整速度)。每辆汽车的速度由以下表达式表示:

在此处输入图像描述

其中“i”是汽车的索引,“t”是时间点(索引最高的汽车是排在第一位的汽车),函数“f”由以下代码表示:

% Input x: Distance to the car in front (of you. NOT the car that is at the
% very front of the line).
% Output: Car velocity
function output = carFunc(x)
    if (x >= 75)
        output = 25;
    else
        output = x/3;
    end
end

最前面的汽车只有一个恒定的速度“g”:

在此处输入图像描述

无论如何,既然您知道了任务的上下文,那么让我们真正进入任务本身。这个学校任务包括多个步骤,第一步是使用前向/显式欧拉计算每辆车的位置,我已经完成了。这是代码:

% Euler's Method
% Initial conditions and setup
g = 25; % Velocity of car M (the car that is first in line, ahead of all others) [m/s]
h = 0.01;  % step size
M = 10; % Number of cars
x = 0:h:40;  % the range of x (time)
n = numel(x);  % the number of timesteps
posMat = zeros(M,n); % Matrix that holds positions of each car (car = row, time = column)
for i=1:M
    posMat(i,1) = 10*i;  % the initial positions for the cars
end
for t=1:(n-1)
    % Calculate position of all cars EXCEPT car M (using the first
    % equation)
    for c=1:(M-1)
    f = carFunc(posMat(c+1,t) - posMat(c,t)); % Velocity of car 'c'
    posMat(c,t+1) = posMat(c,t) + h * f; % Explicit Euler
    end
    % Calculate positon of last car M (first car in line) here:
    % x_M^(n+1) = x_M^n + h*g
    posMat(M,t+1) = posMat(M,t) + h*g;
end

然而问题(我被卡住了)是第二步,我现在需要在第一步中修改我的代码以通过定点迭代使用向后/隐式欧拉。我写了一个函数来进行定点迭代,但除此之外我真的不知道该怎么做。这是我的定点迭代代码:

%Fixed-Point Iteration
%Computes approximate solution of g(x)=x
%Input: function handle g, starting guess x0,
% number of iteration steps k
%Output: Approximate solution

function out=fpi(g, x0, k)
    x = zeros(1, k+1);
    x(1)=x0;
        for i=1:k
            x(i+1)=g(x(i));
        end
    out=x(k+1);
end

任何帮助表示赞赏。对不起,很长的文字。我帖子的上半部分主要是对任务背后背景的“简短”总结。这不是绝对必要的(也不是这里的重点),但我还是添加了它,所以你们知道我的代码中发生了什么。

谢谢!

4

1 回答 1

2

这里的问题是您为标量编写了定点迭代,但您有一个微分方程组。如果我们以矢量形式重写系统,它会变得更加清晰。我添加了一条评论,说明显式和隐式方程的外观如何,以使它们真正具有您可以在教科书中找到的相同标准形式y(t+1) = y(t) + h * f(y(t),t)(resp。 )。y(t+1) = y(t) + h * f(y(t+1),t+1)然后您可以轻松写下更新:

% Euler's Method
% Initial conditions and setup
g = 25; % Velocity of car M (the car that is first in line, ahead of all others) [m/s]
h = 0.01;  % step size
M = 10; % Number of cars
x = 0:h:40;  % the range of x (time)
n = numel(x);  % the number of timesteps
posMat = zeros(M,n); % Matrix that holds positions of each car (car = row, time = column)
k = 20; % number of fixed point iterations
explicit = true;
for i=1:M
    posMat(i,1) = 10*i;  % the initial positions for the cars
end

for t=1:n-1 
    % Calculate position of all cars EXCEPT car M (using the first
    % equation)
    c=1:M-1;
    if explicit
        f = [carFunc(posMat(c+1,t) - posMat(c,t)); g]; % Velocity of car 'c'
        posMat(:,t+1) = posMat(:,t) + h * f; % Explicit Euler
    else 
        %explicit euler:
        %posMat(:,t+1) = posMat(:,t) + h * [carFunc(posMat(c+1,t) - posMat(c,t)); g];
        %implicit euler: (needs to be solved)
        %posMat(:,t+1) = posMat(:,t) + h * [carFunc(posMat(c+1,t+1) - posMat(c,t+1)); g];

        %fixed point iteration:
        posMat(:,t+1) = posMat(:,t); % initialization
        for m=1:k
            posMat(:,t+1) = posMat(:,t) + h * [carFunc(posMat(c+1,t+1) - posMat(c,t+1)); g]; 
        end
    end
end
plot(posMat');


% Input x: Distance to the car in front (of you. NOT the car that is at the
% very front of the line).
% Output: Car velocity
function output = carFunc(x)
    mask = x >= 75;
    output = zeros(size(x));
    output(mask) = 25;
    output(~mask) = x(~mask)/3;
end
于 2019-02-08T21:14:16.907 回答