Eulers method for system of linear equations

조회 수: 2 (최근 30일)
Oskar Mevik Päts
Oskar Mevik Päts 2019년 11월 27일
편집: Jan 2019년 11월 28일
function posi = position(m)
m = 10; % Car count
N = 400; % Steps
h = 0,1; % Stepsize
v_1 = 25; %Car 1's speed
% Assigns starting value
for i = 1:m
posi(1,m) = (m +1 - i) * 10;
end
% The first car's position is independent of other cars
for i = 1:N
posi(i+1,1) = posi(i,1) + h * v_1;
end
for j = 2:m
for i = 1:N
posi(i+1,j) = posi(i,j) + h * velo(i,j,posi(i,j-1),posi(i,j));
end
end
end
function veloc = velo(i,j,x,y)
m = 10;
N = 400;
v_1 = 25;
d = 10;
h = 0.1;
% All cars but the first one starts at zero speed.
for j = 1:m
veloc(1,j+1) = 0;
end
% If the distant difference between two cars is "d", the speed doesn't
% change.
if x - y == d
veloc(i,j) = veloc(i-1,j);
% Otherwise the speed changes
else
veloc(i,j) = veloc(i,j-1) + h * acc(i,j,x,y);
end
function acce = acc(i,j,x,y)
d = 10;
% If position difference is big the car will accelerate quick
if x - y > d * 2
acce(i,j) = 10;
% If position difference is big the car will accelerate quick
elseif x - y > d
acce(i,j) = 5;
% If too close, slow down.
elseif x - y < d
acce(i,j) = -2;
end
The problem's origin is a system of ODE's.
My idea is to create a N x m - matrix, for each car's position, velocity and acceleration for each euler step. For that I use three different functions. I don't see why this doesnt work.
Unable to perform assignment because the size of the left side is 1-by-1 and the size of the
right side is 1-by-10.
Error in velo (line 22)
veloc(i,j) = veloc(i,j-1) + h * acc(i,j,x,y);
Error in position (line 20)
posi(i+1,j) = posi(i,j) + h * velo(i,j,posi(i,j-1),posi(i,j));
It seems like it's the call to the acceleration function that is lacking something. I wanna keep the variables x,y from the original function. What am I doing wrong?
Any help is muy appriciated! <3

답변 (1개)

Jan
Jan 2019년 11월 28일
편집: Jan 2019년 11월 28일
h = 0,1;
% ^ you mean 0.1 with a dot
There is no need to create the accleration as array. Reply a scalar instead:
function acce = acc(i,j,x,y)
d = 10;
if x - y > d * 2
% If position difference is big the car will accelerate quick
acce = 10; % Without (i,j)!
elseif x - y > d % If position difference is big the car will accelerate quick
acce = 5;
elseif x - y < d % If too close, slow down
acce = -2;
end
By the way: this fails for x-y==d. Include this case in one of the other cases.
The same problem happens for the velocity also. You do not need to define veloc(i,j) , but the scalar veloc. Define the arrays of positions and velocities in the main function. Then the current acceleration is calculated and used to determine the new velocities, which again are use to update the positions.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by