find matching values plus minus percent
14 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
yousef Yousef
am 1 Nov. 2015
Beantwortet: kumlachew
am 22 Feb. 2024
Hi Is there a way to find the matching values with plus minus 5 percent?
0 Kommentare
Akzeptierte Antwort
Image Analyst
am 1 Nov. 2015
Try this:
m = rand(1, 100);
targetValue = 0.5; % Whatever you want
tolerance = 0.05 * targetValue; % 5% tolerance
differences = abs(m-targetValue) % Diff of each element from targetValue. Make sure you use abs()!!!
% Find indexes with tolerance of this
inToleranceIndexes = differences < tolerance
% Report the values that we found to the command window
withinTolerance = m(inToleranceIndexes)
8 Kommentare
Image Analyst
am 1 Nov. 2015
Yes. That's useless code. It just says that each angle will interfere with itself - pretty useless information.
Weitere Antworten (2)
Geoff Hayes
am 1 Nov. 2015
yousef - if you have an array of elements that you wish to determine are within 5 percent of another number, you may be able to do something like
% create an array of numbers within some interval
b = 122;
a = 100;
randValues = (b-a).*rand(100,1) + a;
% find those within 5% of 114
withinRange = abs(randValues - 114) < 0.05*114;
randValues(withinRange)
If you step through the above code, withinRange is a logical array of zeros and ones where a one indicates that the element at that index satisfies the condition abs(randValues - 114) < 0.05*114; and a zero indicates otherwise.
3 Kommentare
Stephen23
am 2 Nov. 2015
@yousef Yousef: you can format your code properly using the {} Code button.
kumlachew
am 22 Feb. 2024
%% Lane Keeping Assist System Using Model Predictive Control
% Assist System> block in Simulink(R) and demonstrates the control
% objectives and constraints of this block.
%% Lane Keeping Assist System
% A vehicle (tesla car) equipped with a lane-keeping assist (LKA) system has
% a sensor, such as camera, that measures the lateral deviation and
% relative yaw angle between the centerline of a lane and the tesela car. The
% sensor also measures the current lane curvature and curvature derivative.
% Depending on the curve length that the sensor can view, the curvature in
% front of the ego car can be calculated from the current curvature and
% curvature derivative.
%%
% The LKA system keeps the ego car traveling along the centerline of the
% lanes on the road by adjusting the front steering angle of the ego car.
% The goal for lane keeping control is to drive both lateral deviation and
% relative yaw angle close to zero.
%
% <<../LKAfig.png>>
%
%% Simulink Model for Ego Car
% The dynamics for ego car are modeled in Simulink. Open the Simulink model.
mdl = 'mpcLKAsystem';
open_system(mdl)
%%
% Define the sample time, |Ts|, and simulation duration, |T|, in seconds.
Ts = 0.1;
T = 15;
%%
% To describe the lateral vehicle dynamics, this example uses a _bicycle
% model_ with the following parameters.
%
% * |m| is the total vehicle mass (kg).
% * |Iz| is the yaw moment of inertia of the vehicle (Kg*m^2).
% * |lf| is the longitudinal distance from the center of gravity to the
% front tires (m).
% * |lr| is the longitudinal distance from center of gravity to the rear
% tires (m).
% * |Cf| is the cornering stiffness of the front tires (N/rad).
% * |Cr| is the cornering stiffness of the rear tires (N/rad).
%
m = 1625;
Iz = 2875;
lf = 1.48;
lr = 1.12;
Cf = 170390;
Cr = 195940;
m_original = 1625; % kg
% Adjust parameters by +/- 20%
m_plus_20_percent = m_original * 1.2;
m_minus_20_percent = m_original * 0.8;
%%
% You can represent the lateral vehicle dynamics using a linear
% time-invariant (LTI) system with the following state, input, and output
% variables. The initial conditions for the state variables are assumed to
% be zero.
%
% * State variables: Lateral velocity $V_y$ and yaw angle rate $r$
% * Input variable: Front steering angle $\delta$
% * Output variables: Same as state variables
%%
% In this example, the longitudinal vehicle dynamics are separated from the
% lateral vehicle dynamics. Therefore, the longitudinal velocity is assumed
% to be constant. In practice, the longitudinal velocity can vary. The Lane
% Keeping Assist System block uses adaptive MPC to adjust the model of the
% lateral dynamics accordingly.
% Specify the longitudinal velocity in m/s.
Vx = 30;
%%
% Specify a state-space model, |G(s)|, of the lateral vehicle dynamics.
A_plus_20_percent = [-(2*Cf+2*Cr)/m_plus_20_percent/Vx, -Vx-(2*Cf*lf-2*Cr*lr)/m_plus_20_percent/Vx;...
-(2*Cf*lf-2*Cr*lr)/Iz/Vx, -(2*Cf*lf^2+2*Cr*lr^2)/Iz/Vx];
B_plus_20_percent = [2*Cf/m_plus_20_percent, 2*Cf*lf/Iz]';
G_plus_20_percent = ss(A_plus_20_percent, B_plus_20_percent, C, 0);
A_minus_20_percent = [-(2*Cf+2*Cr)/m_minus_20_percent/Vx, -Vx-(2*Cf*lf-2*Cr*lr)/m_minus_20_percent/Vx;...
-(2*Cf*lf-2*Cr*lr)/Iz/Vx, -(2*Cf*lf^2+2*Cr*lr^2)/Iz/Vx];
B_minus_20_percent = [2*Cf/m_minus_20_percent, 2*Cf*lf/Iz]';
G_minus_20_percent = ss(A_minus_20_percent, B_minus_20_percent, C, 0);
%% Sensor Dynamics and Curvature Previewer
% In this example, the Sensor Dynamics block outputs the lateral deviation
% and relative yaw angle. The dynamics for relative yaw angle are
% $$\dot{e}_2 = r-V_x\rho$, where $\rho$ denotes the curvature. The
% dynamics for lateral deviation are $\dot{e}_1 = V_x e_2+V_y$.
%%
% The Curvature Previewer block outputs the previewed curvature with a
% look-ahead time of one second. Therefore, given a sample time $Ts = 0.1$,
% the prediction horizon |10| steps. The curvature used in this example is
% calculated based on trajectories for a double lane change maneuver.
%%
% Specify the prediction horizon and obtain the previewed curvature.
PredictionHorizon = 15;
time = 0:0.1:15;
md = getCurvature(Vx,time);
%% Configuration of the Lane Keeping Assist System Block
% The LKA system is modeled in Simulink using the Lane Keeping Assist
% System block. The inputs to the LKA system block are:
%
% * Previewed curvature (from lane detections)
% * Ego longitudinal velocity
% * Lateral deviation (from lane detections)
% * Relative yaw angle (from lane detections)
%
% The output of the LKA system is the front steering angle of the ego car.
% Considering the physical limitations of the ego car, the steering angle
% is constrained to the range [-0.5,0.5] rad/s.
u_min = -0.5;
u_max = 0.5;
%%
% For this example, the default parameters of the Lane Keeping Assist
% System block match the simulation parameters. If your simulation
% parameters differ from the default values, update the block
% parameters accordingly.
%% Simulation Analysis
% Run the model.
sim(mdl)
%%
% Plot the simulation results.
figure;
hold on;
plot(logsout.get('Time').Values.Time, logsout.get('Output').Values.Data(:,1), 'b-', 'LineWidth', 2); % Original
plot(logsout_plus_20_percent.get('Time').Values.Time, logsout_plus_20_percent.get('Output').Values.Data(:,1), 'r--', 'LineWidth', 2); % Plus 20%
plot(logsout_minus_20_percent.get('Time').Values.Time, logsout_minus_20_percent.get('Output').Values.Data(:,1), 'g-.', 'LineWidth', 2); % Minus 20%
xlabel('Time (s)');
ylabel('Lateral Deviation');
title('Comparison of Lateral Deviation with Mass Adjusted by +/- 20%');
legend('Original (6000 kg)', 'Plus 20%', 'Minus 20%');
grid on;
hold off;
mpcLKAplot(logsout)
%%
% The lateral deviation and the relative yaw angle both converge to zero.
% That is, the ego car follows the road closely based on the previewed
% curvature.
% Close Simulink model.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Automated Driving Applications finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!