how can execute the following program/
Show older comments
%Road profile
t=1:0.1:100;
dt=0.002*sin(2*pi*t)+0.002*sin(7.5*pi*t);
t1=t-3.5;
t2=t-6.5;
t3=t-8.5;
t4=t-11.5;
if((t>=3.5) && (t<5))
zr= -0.0592*t1^3+0.1332*t1^2+dt;
elseif((t>=5) && (t<6.5))
zr=0.0592*t2^3+0.1332*t2^2+dt;
elseif((t>=8.5) && (t<10))
zr=0.0592*t3^3-0.1332*t3^2+dt;
elseif((t>=10) && (t<11.5))
zr=-0.0592-0.1332*t4^2+dt;
else
dt;
end
plot(zr)
when i run it, i have the following error
Operands to the and && operators must be convertible to logical scalar values.
Answers (2)
Walter Roberson
on 31 Aug 2012
0 votes
This is not surprising in your code. You create t as a vector, so (t>=3.5) will be a vector of logical results, and (t<5) will be a vector of logical results, and then you attempt to use the scalar operator && against those two vectors.
I would suggest that you need to research "logical indexing"
Image Analyst
on 31 Aug 2012
I think you're looking to do this:
% Road profile
t = 1:0.1:100;
dt = 0.002*sin(2*pi*t)+0.002*sin(7.5*pi*t);
t1=t-3.5;
t2=t-6.5;
t3=t-8.5;
t4=t-11.5;
% Initialize
zr = dt;
% Assign the various elements
indexes = (t>=3.5) & (t<5)
zr(indexes) = -0.0592*t1(indexes).^3+0.1332*t1(indexes).^2+dt(indexes);
indexes = (t>=5) & (t<6.5)
zr(indexes) = 0.0592*t2(indexes).^3+0.1332*t2(indexes).^2+dt(indexes);
indexes = (t>=8.5) & (t<10)
zr(indexes) = 0.0592*t3(indexes).^3-0.1332*t3(indexes).^2+dt(indexes);
indexes = (t>=10) & (t<11.5)
zr(indexes) = -0.0592-0.1332*t4(indexes).^2+dt(indexes);
% Plot it.
plot(zr);
grid on;
fontSize = 24;
title('Road Profile', 'FontSize', fontSize);
xlabel('t', 'FontSize', fontSize);
ylabel('zr', 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
% Give a name to the title bar.
set(gcf,'name','Demo by ImageAnalyst','numbertitle','off')
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!