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!