How do I automate the following code for a general case senario where the dimension N and the variables(p v a j or p1v1a1j1p2....) are user inputted

1 view (last 30 days)
% For 1D
if N == 1 % Constant Velocity
A = blkdiag(A);
if C == 1
v = input('Enter the constant velocity value : '); % Input for the constant velocity
p = v*tt; % Using formulae to find p
a = 0; % Since its constant velocity both acceleration
j = 0; % and jerk is zero
plot(tt,p); % Plot for position vs time graph
% To find the time-varying state vectors for inputted response
% times
for i = 1:length(idx)
Q = []; % Resets Q everytime loop runs
Q0 = [p(idx(i));v;a;j];
Q = A*Q0;
fprintf("For the time value %d the state vector is :\n", rt(i));
disp(Q);
end
elseif C == 2 % Constant Acceleration
a = input('Enter the constant acceleration value : '); % Input for the constant acceleration
v = a*tt;% Using formulae to find v
p = v.*tt; % and p
j = 0; % Since accleration is constant jerk is zero
plot(tt,p);% Plot for position vs time graph
% To find the time-varying state vectors for inputted response
% times
for i = 1:length(idx)
Q = []; % Resets Q everytime loop runs
Q0 = [p(idx(i));v(idx(i));a;j];
Q = A*Q0;
fprintf("For the time value %d the state vector is :\n", rt(i));
disp(Q);
end
elseif C == 3 % Constant Jerk
j = input('Enter the constant jerk value : '); % Input for the constant jerk
a = j*tt;% Using formulae to find a
v = a.*tt; % v and
p = v.*tt; % p
plot(tt,p);% Plot for position vs time graph
% To find the time-varying state vectors for inputted response
% times
for i = 1:length(idx)
Q = []; % Resets Q everytime loop runs
Q0 = [p(idx(i));v(idx(i));a(idx(i));j];
Q = A*Q0;
fprintf("For the time value %d the state vector is :\n", rt(i));
disp(Q);
end
% Providing a alternative to end the program when wrong inputs are
% given
else
disp("You have entered a invalid option for constant parameters, re-run the function again and input correct options !");
end
% For 2D
elseif N == 2
A = blkdiag(A,A); % A 2D State transition matrix
if C == 1 % Constant Velocity
v1 = input('Enter the constant velocity value [x coor] : '); % x-coordinate for velocity
v2 = input('Enter the constant velocity value [y coor] : '); % y-coordinate for velocity
p1 = v1*tt; % x-coordinate for position using formula
p2 = v2*tt; % y-coordinate for position using formula
a1 = 0; % Since velocity is constant acceleration and
a2 = 0; % jerk is zero
j1 = 0;
j2 = 0;
plot(p1,p2); % plot for x-position vs y-position
% To find the time-varying state vectors for inputted response
% times
for i = 1:length(idx)
Q = []; % Resets Q everytime loop runs
Q0 = [p1(idx(i)); v1; a1; j1; p2(idx(i)); v2; a2 ;j2];
Q = A*Q0;
fprintf("For the time value %d the state vector is :\n", rt(i));
disp(Q);
end
elseif C == 2 % Constant Acceleration
a1 = input('Enter the constant acceleration value [x coor] : '); % x-coordinate for acceleration
a2 = input('Enter the constant acceleration value [y coor] : '); % x-coordinate for acceleration
v1 = a1*tt; % x-coordinate for velocity using formula
v2 = a2*tt; % y-coordinate for velocity using formula
p1 = v1.*tt; % x-coordinate for position using formula
p2 = v2.*tt; % y-coordinate for position using formula
j1 = 0; % Since accleration is zero
j2 = 0; % jerk is also zero
plot(p1,p2); % plot for x-position vs y-position
% To find the time-varying state vectors for inputted response
% times
for i = 1:length(idx)
Q = []; % Resets Q everytime loop runs
Q0 = [p1(idx(i)); v1(idx(i)); a1; j1; p2(idx(i)); v2(idx(i)); a2; j2];
Q = A*Q0;
fprintf("For the time value %d the state vector is :\n", rt(i));
disp(Q);
end
elseif C == 3 % Constant Jerk
j1 = input('Enter the constant jerk value [x coor] : '); % x-coordinate for jerk
j2 = input('Enter the constant jerk value [y coor] : '); % y-coordinate for jerk
a1 = j1*tt; % x-coordinate for acceleration using formula
a2 = j2*tt; % y-coordinate for position using formula
v1 = a1.*tt; % x-coordinate for velocity using formula
v2 = a2.*tt; % y-coordinate for velocity using formula
p1 = v1.*tt; % x-coordinate for position using formula
p2 = v2.*tt; % y-coordinate for position using formula
plot(p1,p2) % plot for x-position vs y-position
% To find the time-varying state vectors for inputted response
% times
for i = 1:length(idx)
Q = []; % Resets Q everytime loop runs
Q0 = [p1(idx(i)); v1(idx(i)); a1(idx(i)); j1; p2(idx(i)); v2(idx(i)); a2(idx(i)); j2];
Q = A*Q0;
fprintf("For the time value %d the state vector is :\n", rt(i));
disp(Q);
end
% An alternative for when the user inputs the wrong parameters
else
disp("You have entered a invalid option for constant parameters, re-run the function again and input correct options !");
end
% For 3D
elseif N == 3
A = blkdiag(A,A,A); % A 3D State transition matrix
if C == 1 % Constant Velocity
v1 = input('Enter the constant velocity value [x coor] : '); % x-coordinate for velocity
v2 = input('Enter the constant velocity value [y coor] : '); % y-coordinate for velocity
v3 = input('Enter the constant velocity value [z coor] : '); % z-coordinate for velocity
p1 = v1*tt; % x-coordinate for position using formula
p2 = v2*tt; % y-coordinate for position using formula
p3 = v3*tt; % z-coordinate for position using formula
a1 = 0; % With constant velocity
a2 = 0; % both Accleration and
a3 = 0; % Jerk is zero
j1 = 0;
j2 = 0;
j3 = 0;
plot3(p1,p2,p3); % plot for x-position vs y-position v z-position
% To find the time-varying state vectors for inputted response
% times
for i = 1:length(idx)
Q = [];
Q0 = [p1(idx(i)); v1; a1; j1; p2(idx(i)); v2; a2 ;j2 ; p3(idx(i)); v3; a3 ;j3];
Q = A*Q0;
fprintf("For the time value %d the state vector is :\n", rt(i));
disp(Q);
end
elseif C == 2 % Constant Accleration
a1 = input('Enter the constant acceleration value [x coor] : '); % x-coordinate for acceleration
a2 = input('Enter the constant acceleration value [y coor] : '); % y-coordinate for acceleration
a3 = input('Enter the constant acceleration value [z coor] : '); % z-coordinate for acceleration
v1 = a1*tt; % x-coordinate for velocity using formula
v2 = a2*tt; % y-coordinate for velocity using formula
v3 = a3*tt; % z-coordinate for velocity using formula
p1 = v1.*tt; % x-coordinate for position using formula
p2 = v2.*tt; % y-coordinate for position using formula
p3 = v3.*tt; % z-coordinate for position using formula
j1 = 0; % Since accleration is zero
j2 = 0; % jerk is zero
j3 = 0;
plot3(p1,p2,p3); % plot for x-position vs y-position v z-position
% To find the time-varying state vectors for inputted response
% times
for i = 1:length(idx)
Q = [];
Q0 = [p1(idx(i)); v1(idx(i)); a1; j1; p2(idx(i)); v2(idx(i)); a2; j2; p3(idx(i)); v3(idx(i)); a3; j3];
Q = A*Q0;
fprintf("For the time value %d the state vector is :\n", rt(i));
disp(Q);
end
elseif C == 3 % Constant Jerk
j1 = input('Enter the constant jerk value [x coor] : '); % x-coordinate for jerk
j2 = input('Enter the constant jerk value [y coor] : '); % y-coordinate for jerk
j3 = input('Enter the constant jerk value [z coor] : '); % z-coordinate for jerk
a1 = j1*tt; % x-coordinate for acceleration using formula
a2 = j2*tt; % y-coordinate for acceleration using formula
a3 = j3*tt; % z-coordinate for acceleration using formula
v1 = a1.*tt; % x-coordinate for velocity using formula
v2 = a2.*tt; % y-coordinate for velocity using formula
v3 = a3.*tt; % z-coordinate for velocity using formula
p1 = v1.*tt; % x-coordinate for jerk using formula
p2 = v2.*tt; % y-coordinate for jerk using formula
p3 = v3.*tt; % z-coordinate for jerk using formula
plot3(p1,p2,p3) % plot for x-position vs y-position v z-position
% To find the time-varying state vectors for inputted response
% times
for i = 1:length(idx)
Q = [];
Q0 = [p1(idx(i)); v1(idx(i)); a1(idx(i)); j1; p2(idx(i)); v2(idx(i)); a2(idx(i)); j2; p3(idx(i)); v3(idx(i)); a3(idx(i)); j3];
Q = A*Q0;
sprintf("For the time value %d the state vector is :\n", rt(i));
disp(Q);
end
% An alternative when anything other than 1, 2 or 3 is entered for
% dimension
else
disp("You have entered a invalid option for constant parameters, re-run the function again and input correct options !");
end
end
  1 Comment
Bob Thompson
Bob Thompson on 5 Aug 2019
You have a lot of 'p = ...' and 'a = ...' statements in your code. Are you saying that all of those will be removed?
In general, the easiest way to do what you're asking is to have the input command at the beginning of your code, or a matrix where the user can manually adjust the values. I would expect it to look something like this:
N = input('Please enter the value of N: ');
values = input('Please enter the values of p, v, a, and j for position velocity and acceleration: ');
I would expect to see a single value for N, and a matrix for pvaj that looks something like the following:
values = [p v a j ;
p1 v1 a1 j1;
p2 v2 a2 j2;
p3 v3 a3 j3];
The transpose would also be fine, or even a single row or column.
Then you would just replace the pvaj values within your code with the index of the appropriate input. (You don't even need to have a value called 'p' a this point, just call values(1,1) instead.)

Sign in to comment.

Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!