I am trying to recreat this plot
and I've got this
They are very close to each other, the only reason I can't get it is that I have a time step of 1 in my code.
When I try to change my time step from 1 to 0.1 in t = 0:1:tmax; my graph completely goes wild.
I would appreciate if someone could help me to solve this problem,
My code below.
clc;clear all;close all
tmax=25
t = 0:1:tmax;
speed0 = zeros(1,length(t));
speed1= @(t) 0.1553567*(t.^6) - 2.0416*(t.^5) + 9.1837*(t.^4) - 14.829*(t.^3) - 1.3703*(t.^2) + 32.821*(t) - 1.3155
speed2= @(t) 0.003980879*(t.^5) - 0.2247*(t.^4) + 4.8682*(t.^3) - 50.442*(t.^2) + 254.67*(t) - 430.66
speed3= @(t) -0.073*(t.^2) + 6.1802*(t) + 40.423
for n = 2:length(t);
if n<5
speed0(n) = speed1(n)
elseif n>5 && n < 15.4
speed0(n) = speed2(n)
else
speed0(n) = speed3(n)
end
end
plot(t,speed0);

 采纳的回答

Your function handles are functions of t, so you need to be passing in t, not n. E.g.,
if t(n)<5
speed0(n) = speed1(t(n))
elseif t(n)>5 && t(n) < 15.4
speed0(n) = speed2(t(n))
else
speed0(n) = speed3(t(n))
end

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 MATLAB 的更多信息

产品

版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by