将显式欧拉转换为隐式欧拉(通过 fixed-point 迭代)
Converting explicit euler to implicit euler (via fixed-point iteration)
所以我有一个学校任务,我需要计算道路上一堆汽车的位置(也就是排队行驶,所以如果 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':
无论如何,现在您知道了任务的上下文,让我们真正地转到任务本身。这个学校任务包含多个步骤,第一步是使用 forward/explicit 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 迭代使用 backwards/implicit 欧拉. 我已经编写了一个函数来执行 fixed-point 迭代,但除此之外我真的不知道该做什么。这是我的 fixed-point 迭代代码:
%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
感谢任何帮助。抱歉,文字很长。我的 post 的顶部主要只是任务背后上下文的“简短”摘要。这不是绝对必要的(而且这不是这里的重点)但是我还是添加了它所以你们知道我的代码中发生了什么。
谢谢!
这里的问题是您为 标量 编写了不动点迭代,但是您有一个 系统 微分方程。如果我们以矢量形式重写系统,它会变得更加清晰。我添加了一条评论,说明显式和隐式方程式看起来如何,这样它们实际上具有与您可以在 texbook 中找到的相同的标准形式 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
所以我有一个学校任务,我需要计算道路上一堆汽车的位置(也就是排队行驶,所以如果 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':
无论如何,现在您知道了任务的上下文,让我们真正地转到任务本身。这个学校任务包含多个步骤,第一步是使用 forward/explicit 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 迭代使用 backwards/implicit 欧拉. 我已经编写了一个函数来执行 fixed-point 迭代,但除此之外我真的不知道该做什么。这是我的 fixed-point 迭代代码:
%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
感谢任何帮助。抱歉,文字很长。我的 post 的顶部主要只是任务背后上下文的“简短”摘要。这不是绝对必要的(而且这不是这里的重点)但是我还是添加了它所以你们知道我的代码中发生了什么。
谢谢!
这里的问题是您为 标量 编写了不动点迭代,但是您有一个 系统 微分方程。如果我们以矢量形式重写系统,它会变得更加清晰。我添加了一条评论,说明显式和隐式方程式看起来如何,这样它们实际上具有与您可以在 texbook 中找到的相同的标准形式 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