plot a function that has a variable that changes with time
24 次查看(过去 30 天)
显示 更早的评论
The function has a variable ap that changes at t >= 100s. How do I plot that?
close all; clear; clc
tspan = [0 150];
x0 = [1];
t = linspace(0,150);
bp = 2; am = -1; bm = 1; r = 0.5; gamma = 2;
theta2 = func(t,am,bp)
plot(t,theta2)
function theta2 = func(t,am,bp)
if t >= 100
ap = 2;
else
ap = 1;
end
theta2 = (am-ap)/bp;
end
0 个评论
回答(1 个)
Chien-Han Su
2021-3-28
编辑:Chien-Han Su
2021-3-28
In your block of 'func', watch out that the 1st input argument 't' might be and indeed a vector in your case through
t = linspace(0,150);
This leads to an ambiguous result , in the 'func', for the if condition
if t >= 100
By typing in the command window
linspace(0,150) >= 100
you can see explicitly that the result is a vector not a single boolean value.
Hence, I suggest you modify the 'func' block by explicitly assign the size of output 'theta2' according to the input 't' and compute elements of the result using a loop. Namely, revise the code into
close all; clear; clc
tspan = [0 150];
x0 = [1];
t = linspace(0,150);
bp = 2; am = -1; bm = 1; r = 0.5; gamma = 2;
theta2 = func(t,am,bp);
plot(t,theta2,'LineWidth',1)
axis([-inf inf -2 -0.5]);
function theta2 = func(t,am,bp)
numInput = numel(t);
theta2 = zeros(size(t));
for i = 1:numInput
if t(i) >= 100
ap = 2;
else
ap = 1;
end
theta2(i) = (am-ap)/bp;
end
end
The result is shown as follows
Edit: fix typos
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!