因此,我有一项学校任务,需要计算在道路上相互跟随的一堆汽车的位置(也就是在一条直线上行驶,因此,如果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来计算每辆车随时间的位置,我已经完成了。这是该代码:
% 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
感谢您的帮助。抱歉,长文本。我的帖子的顶部主要只是任务后台的“简短”摘要。并不是绝对必要的(这里也不是重点),但是我还是添加了它,以便你们知道我的代码中发生了什么。
谢谢!
答案 0 :(得分:2)
这里的问题是您为标量写了定点迭代,但是您有一个系统微分方程。如果我们以矢量形式重写系统,它将变得更加清晰。我添加了一条注释,说明显式和隐式方程的外观如何,以使它们真正具有您可以在texbook中找到的相同标准格式y(t+1) = y(t) + h * f(y(t),t)
(分别为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