How can I plot a point which is product of two functions in time domain with changing parameter?
1 view (last 30 days)
Show older comments
Mikolaj Krakowski
on 19 Jan 2019
Commented: Star Strider
on 19 Jan 2019
Hello, I'm making one assignment to my class and i hit the wall and can't proceed.
We're supposed to program movement of a car. ( look at the attached picture )
and the equations are: x' = cos( θ) *cos( ϕ)*u1 | y' = cos (θ) sin( ϕ)*u1 | ϕ' = (sin( θ)/L)*u1 | θ' = u2
where x,y are the position of the rear axle of the car and u1 is longitudinal speed and u2 is wheels turning speed, θ is the wheel steering angle, ϕ is the orientation of the car ( look at the attached picture ).
Written in Matlab function as :
function carDerivatives = car2(t, car, l, u1, u2)
% car is in form:
%[x, y, phi, theta]
% carDerivatives = zeros(1, 4);
dxdt = cos(car(4)) * cos(car(3)) * u1;
dydt = cos(car(4)) * sin(car(3)) * u1;
dphidt = sin(car(4)) * u1 / l;
dthetadt = u2;
carDerivatives = [dxdt; dydt; dphidt; dthetadt];
end
with defined variables and initial conditions and the ode45:
l = 1; %length
u1 = 1; %speed
u2 = 0; %turning speed(movement=[u1;u2]=[1;0]
%inits :
x0 = 0; %rear axle x position
y0 = 0; %rear axle y position
phi0 = 0; %car orientation angle
theta0 = 0; %steering wheel angle
car0 = [x0; y0; phi0; theta0];
tspan = [0 40];
[t, car] = ode45(@(t, car)(car2(t, car, l, u1, u2)), tspan, car0);
so as an output we have x and y values which are car(:,1) and car(:,2), when plotted they correctly represent the rear axle movement, with relation to the distance not time.
i want to plot the position of the car but in time domain, so i made
rear=[car(:,1), car(:,2)];
figure(3)
plot(t,rear)
and the output is 2 graph in 1 figure, what can i do to express in the time domain?
And the second question is:
i would like to program the cars movement with changing the values of u1 and u2, using a piecewise function, for example:
for t<1 u1=1, u2=0;
for 1<t<2 u1=0, u2=1;
for 2<t<3 u1=1, u2=0;
for 3<t<4 u1=0,u2=-1;
how can i express this in the time domain ? How can i write piecewise function for these controls ?
Thank You for your time!
0 Comments
Accepted Answer
Star Strider
on 19 Jan 2019
Try this:
l = 1; %length
u1 = 1; %speed
u2 = 0; %turning speed(movement=[u1;u2]=[1;0]
%inits :
x0 = 0; %rear axle x position
y0 = 0; %rear axle y position
phi0 = 0; %car orientation angle
theta0 = 0; %steering wheel angle
car0 = [x0; y0; phi0; theta0];
tspan = [0 40];
u1v = [1; 0; 1; 0];
u2v = [0; 1; 0; 1];
tm = [0 1; 1 2; 2 3; 3 4];
for k1 = 1:size(tm,1)
k1
tspan = tm(k1,:);
u1 = u1v(k1);
u2 = u2v(k1);
[t, car] = ode45(@(t, car)(car2(t, car, l, u1, u2)), tspan, car0);
car0 = car(end,:);
tv{k1,:} = t;
carc{k1,:} = car;
end
t_all = cell2mat(tv);
car_all = cell2mat(carc);
figure
plot(t_all, car_all)
grid
figure
plot3(car_all(:,1), car_all(:,2), t_all)
grid on
xlabel('car(:,1)')
ylabel('car(:,2)')
zlabel('t')
This code illustrates the general idea.
Experiment to get the result you want.
Your ‘car2’ function remains unchanged, so there is no reason to post it again.
4 Comments
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!