Need help with and if else statement to be entered into a state space set up

2 views (last 30 days)
clc
clear
Mb = 1200; %kg
Mt = 15; %kg
ks = 30000; %N/m
kt = 100000; %N/m
Cs = 3000; %N*s/m
b = 2; %m
c = 0.3; %m
d = 1.5; %m
e = 1; %m
% Speed bump is half a sine wave
v1 = 5; %m/s
v2 = 10; %m/s
v3 = 20; %m/s
v4 = 30; %m/s
t = [0:0.1:4];
if t>=0 && t < (e/v1)
y = 0;
elseif t>=(e/v1) && t < (e+d)/v1
y = (c*sin(((pi*v1)/d)*(t-(e/v1))));
elseif t>=((e+d)/v1) && t < (e+d+b)/v1
y = 0;
elseif t>=((e+d)/v1) && t < (e+d+b)/v1
y = (c*sin(((pi*v1)/d)*(t-((e+d+b)/v1))));
end
A = [0 0 1 0; 0 0 0 1; -((ks+kt)/Mt) ks/Mt -Cs/Mt Cs/Mt; ks/Mb -ks/Mb Cs/Mb -Cs/Mb]
B = [0; 0; (kt/Mt).*y; 0]
C = [1 0 0 0; 0 1 0 0]
D = zeros(1)
sys = ss(A,B,C,D)
Above is my code im working on for a state space problem. in matrice B im multyplying by variable y which is in described in my if statement. Y changes with time and the if statement on wants scalar. Here is the error code:
Operands to the || and && operators must be convertible to logical scalar values
Any help with this would be grateful. also can I do an array for v values or would 4 if statements be easier to do. I need 4 different plots for the 4 different v values.

Answers (1)

Walter Roberson
Walter Roberson on 17 Apr 2019
You assign a vector to t. You then try to test
if t>=0 && t < (e/v1)
With t being a vector, this is
if logical_vector && logical_vector
however, the && operator can only be used when both sides are scalars.
You could try changing the && to & so that you had
if logical_vector & logical_vector
which would then calculate down to
if logical_vector
but remember that is only considered to be true if all of the elements of the vector are non-zero, same as if you had written
if all(t>=0 & t < (e/v1))
This will be false because some of your t are not < e/v1
It looks to me as if you have a state space model that you want to have act differently depending on the time. That is not possible with typical transfer functions or typical ss . I am not sure if it is possible if you use genmat() inputs to ss() or if you use genss() . genmat() and genss() permit you to use tunable parameters, but at any one point the tunable parameters always have a specific value, not a formula, so I do not think you can make time a component of the tunable matrices.

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!