MATLAB for 3个条件具有相同变量“速度”的循环

时间:2018-11-05 13:22:13

标签: matlab

我已经花费了数小时试图弄清楚如何执行此代码,但是似乎没有任何效果。有人可以显示如何为该方程式编写代码

enter image description here

2 个答案:

答案 0 :(得分:1)

index=0;
time_axis=0:0.1:25; %i chose time step as 0.1 seconds
speed=zeros(1, numel(time_axis)); %this line is for memory allocation, its optional
for t=0:0.1:25 %we could also use a while loop on time_axis elements
index=index+1; 
if t<5
speed(index)= 0.1553567*t^6-2.0416*t^5 ... %complete the formula
elseif t<15.4
speed(index)=0.0039.... %complete the formula
else
speed(index)=.... %complete the formula
end

end
plot(time_axis,speed); %you can use xlabel, ylabel and title functions to write labels

答案 1 :(得分:0)

您可以使用heaviside函数,并通过组合指定域中的各个方程式来创建函数句柄。

eq1 = @(t) t.^6 + t.^5;     % etc.
eq2 = @(t) t.^5 + t.^4;     % etc.
eq3 = @(t) t.^2 + t;        % etc.

f = @(t) eq1(t) + heaviside(t-5).*(eq2(t)-eq1(t)) + heaviside(t-15.4).*(eq3(t)-eq2(t));

t = 0:25;

y = f(t);