Program using both conditional and loop

조회 수: 4 (최근 30일)
Felvie Valerie Leong
Felvie Valerie Leong 2021년 2월 7일
댓글: Walter Roberson 2021년 2월 7일
I don't know how to make continuos plot with different condition, i attach the formula and expected output. Thank you so much!

답변 (1개)

Walter Roberson
Walter Roberson 2021년 2월 7일
For example
for t = 0:.01:3
if t < 0.2
Y = t.^2 + 1;
elseif t <= 1
Y = (t - 1/4).^3 - t.^2 + sin(pi*t);
else
Y = cot(t .* pi);
end
Now record t
Now record Y
end
plot(recorded_t, recorded_Y)
  댓글 수: 2
Felvie Valerie Leong
Felvie Valerie Leong 2021년 2월 7일
I can't run the 'Now record t' and 'Now record Y' code
Walter Roberson
Walter Roberson 2021년 2월 7일
function Now(flaff, varname)
assert(strcmp(flaff, 'record'), 'Now what??')
persistent recorded_t recorded_Y
switch varname
case 't'
current_t = evalin('caller', 't');
recorded_t(end+1) = current_t;
case 'Y'
current_Y = evalin('caller', 'Y');
recorded_Y(end+1) = current_Y;
otherwise
error('Now can only record t or Y');
end
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by