Filter löschen
Filter löschen

How to use if else for terms

2 Ansichten (letzte 30 Tage)
Ivan Dwi Putra
Ivan Dwi Putra am 14 Jan. 2021
Bearbeitet: Mario Malic am 14 Jan. 2021
I have code to plot Position, Velocity and Torque
clear all; close all; clc;
%% Initialization
S0= 0;
T0 = 0;
%% Parameters
% Davis Equation Parameters
C0 = [7.6658e-3;7.6658e-3;7.6658e-3;7.6658e-3;7.6658e-3;7.6658e-3;7.6658e-3;];
C1 = [1.08e-4;1.08e-4;1.08e-4;1.08e-4;1.08e-4;1.08e-4;1.08e-4;];
C2 = [2.06e-5;2.06e-5;2.06e-5;2.06e-5;2.06e-5;2.06e-5;2.06e-5;];
% Mass Parameters
Mass = [20546.8815192050;20957.5068354343;20964.8885351993;20157.6130816775;20970.5927817606;20957.1669482429;20485.3756487228];
% Another Parameters
Tao = [5;5;5;5;5;5;5;]; % Inertial lag of longitudinal dynamics
R = [1.22734407596025;1.24787534177172;1.24824442675996;1.20788065408388;1.24852963908803;1.24785834741215;1.22426878243614]; % Mechanical efficiency of the driveline
u = [205000;220000;235000;250000;265000;280000;302000];
Torque = [2600;2600;2600;2600;2600;2600;2600;];
V0 = 30; % Desired Speed
f = 0.01; % rolling friction
Eta = 0.96; % Efficency
g = 9.8; % gravitity
%% Output Vectors (Void)
S_1 = [];
V_1 = [];
T_1 = [];
S_2=[];
V_2=[];
T_2=[];
S_3=[];
V_3=[];
T_3=[];
S_4=[];
V_4=[];
T_4=[];
S_5=[];
V_5=[];
T_5=[];
S_6=[];
V_6=[];
T_6=[];
S_7=[];
V_7=[];
T_7=[];
%% Discrete Time Model
for k = 0:100 %%%%%%step definition : 0 to step size %%%%%
dt = 100 - k;
S1 = S0 + V0*dt;
V1 = V0 + (dt/Mass(1))*(Eta*Torque(1)/R(1) - (C0(1) + C1(1)*V0 + C2(1)*V0^2 + Mass(1)*g*f));
T1 = T0 - (1/Tao(1))*Torque(1)*dt + (1/Tao(1))*u(1)*dt;
S2 = S1 + V1*dt;
V2 = V1 + (dt/Mass(2))*(Eta*Torque(2)/R(2) - (C0(2) + C1(2)*V1 + C2(2)*V1^2 + Mass(2)*g*f));
T2 = T1 - (1/Tao(2))*Torque(2)*dt + (1/Tao(2))*u(2)*dt;
S3 = S2 + V2*dt;
V3 = V2 + (dt/Mass(3))*(Eta*Torque(3)/R(3) - (C0(3) + C1(3)*V2 + C2(3)*V2^2 + Mass(3)*g*f));
T3 = T2 - (1/Tao(3))*Torque(3)*dt + (1/Tao(3))*u(3)*dt;
S4 = S3 + V3*dt;
V4 = V3 + (dt/Mass(4))*(Eta*Torque(4)/R(4) - (C0(4) + C1(4)*V3 + C2(4)*V3^2 + Mass(4)*g*f));
T4 = T3 - (1/Tao(4))*Torque(4)*dt + (1/Tao(4))*u(4)*dt;
S5 = S4 + V4*dt;
V5 = V4 + (dt/Mass(5))*(Eta*Torque(5)/R(5) - (C0(5) + C1(5)*V4 + C2(5)*V4^2 + Mass(5)*g*f));
T5 = T4 - (1/Tao(5))*Torque(5)*dt + (1/Tao(5))*u(5)*dt;
S6 = S5 + V5*dt;
V6 = V5 + (dt/Mass(6))*(Eta*Torque(6)/R(6) - (C0(6) + C1(6)*V5 + C2(6)*V5^2 + Mass(6)*g*f));
T6 = T5 - (1/Tao(6))*Torque(6)*dt + (1/Tao(6))*u(6)*dt;
S7 = S6 + V6*dt;
V7 = V6 + (dt/Mass(7))*(Eta*Torque(7)/R(7) - (C0(7) + C1(7)*V6 + C2(7)*V6^2 + Mass(7)*g*f));
T7 = T6 - (1/Tao(7))*Torque(7)*dt + (1/Tao(7))*u(7)*dt;
S0 = S1;
T0 = T1;
V0 = V1;
% Fi_i =[s1;u1;Ti1];
%% Output Vectors
S_1 = [S_1,S1];
V_1 = [V_1,V7];
T_1 = [T_1,T7];
S_2 = [S_2,S2];
V_2 = [V_2,V2];
T_2 = [T_2,T2];
S_3 = [S_3,S3];
V_3 = [V_3,V3];
T_3 = [T_3,T3];
S_4 = [S_4,S4];
V_4 = [V_4,V4];
T_4 = [T_4,T4];
S_5 = [S_5,S5];
V_5 = [V_5,V5];
T_5 = [T_5,T5];
S_6 = [S_6,S6];
V_6 = [V_6,V6];
T_6 = [T_6,T6];
S_7 = [S_7,S7];
V_7 = [V_7,V7];
T_7 = [T_7,T7];
end
%% Plots
figure(1)
plot(S0,'c-','linewidth',1.5),grid;
hold on;
plot(S_1,'m-','linewidth',1.5),grid;
hold on;
plot(S_2,'b-','linewidth',1.5),grid;
hold on;
plot(S_3,'g-','linewidth',1.5),grid;
hold on;
plot(S_4,'m*','linewidth',1.5),grid;
hold on;
plot(S_5,'g*','linewidth',1.5),grid;
hold on;
plot(S_6,'b*','linewidth',1.5),grid;
hold on;
plot(S_7,'m--','linewidth',1.5),grid;
hold on;
h = legend('0','1','2','3','4','5','6','7','Location','SouthEast','interpreter','latex');
set(h,'box','off'); box off;
xlabel('Step Size','interpreter','latex');ylabel('Position (m)','interpreter','latex');
title('Train Position','interpreter','latex')
set(gca,'FontSize',20)
set(gcf,'Position',[0 350 700 1050 1400 1750 2100 ]);
figure(2)
plot(V0,'c-','linewidth',1.5),grid;
hold on;
plot(V_1,'m-','linewidth',1.5),grid;
hold on;
plot(V_2,'b-','linewidth',1.5),grid;
hold on;
plot(V_3,'g-','linewidth',1.5),grid;
hold on;
plot(V_4,'m*','linewidth',1.5),grid;
hold on;
plot(V_5,'g*','linewidth',1.5),grid;
hold on;
plot(V_6,'b*','linewidth',1.5),grid;
hold on;
plot(V_7,'m--','linewidth',1.5),grid;
hold on;
h = legend('0','1','2','3','4','5','6','7','Location','SouthEast','interpreter','latex');
set(h,'box','off'); box off;
xlabel('Step Size','interpreter','latex');ylabel('Speed (m/s)','interpreter','latex');
title('Train Velocity','interpreter','latex')
set(gca,'FontSize',20)
figure(3)
plot(T0,'c-','linewidth',1.5),grid;
hold on;
plot(T_1,'m-','linewidth',1.5),grid;
hold on;
plot(T_2,'b-','linewidth',1.5),grid;
hold on;
plot(T_3,'g-','linewidth',1.5),grid;
hold on;
plot(T_4,'m*','linewidth',1.5),grid;
hold on;
plot(T_5,'g*','linewidth',1.5),grid;
hold on;
plot(T_6,'b*','linewidth',1.5),grid;
hold on;
plot(T_7,'m--','linewidth',1.5),grid;
hold on;
h = legend('0','1','2','3','4','5','6','7','Location','SouthEast','interpreter','latex');
set(h,'box','off'); box off;
xlabel('Step Size ','interpreter','latex');ylabel('Torque (N)','interpreter','latex');
title('Train Torque','interpreter','latex')
set(gca,'FontSize',20)
I want to add if else for terms, like this
if S_1 > 350
V_2
else V_2 = 0
end
if S_2 > 700
V_3
else V_3 = 0
end
if S_3 > 1050
V_4
else V_4 = 0
end
if S_4 > 1400
V_5
else V_5 = 0
end
if S_5 > 1750
V_6
else V_6 = 0
end
if S_6 > 2100
V_7
else V_7 = 0
end
is it true?

Antworten (1)

Mario Malic
Mario Malic am 14 Jan. 2021
Bearbeitet: Mario Malic am 14 Jan. 2021
Hello,
Apart from code which is not friendly to read, here's explanation:
if S_1 > 350 % if S_1 is bigger than 350
V_2 % show V_2
else V_2 = 0 % set V_2 equal to 0
end
  1 Kommentar
Ivan Dwi Putra
Ivan Dwi Putra am 14 Jan. 2021
S is position
V is velocity
I have 7 Vehicle which is
  • S_i is position vehicle - i
  • V_i is velocity vehicle - i
  • T_i is torque vehicle - i
I want the terms is
  • When position vehicle 1 in 350, the vehicle 2 can move but when position vehicle 1 less than 350, the vehicle 2 cannot moving
  • When position vehicle 2 in 700, the vehicle 3 can move but when position vehicle 2 less than 700, the vehicle 3 cannot moving
  • When position vehicle 3 in 1050, the vehicle 4 can move but when position vehicle 3 less than 1050, the vehicle 4 cannot moving
  • When position vehicle 4 in 1400, the vehicle 5 can move but when position vehicle 4 less than 1400, the vehicle 5 cannot moving
  • When position vehicle 6 in 1750, the vehicle 7 can move but when position vehicle 6 less than 1750, the vehicle 7 cannot moving

Melden Sie sich an, um zu kommentieren.

Produkte


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by