Program using both conditional and loop
1 次查看(过去 30 天)
显示 更早的评论
I don't know how to make continuos plot with different condition, i attach the formula and expected output. Thank you so much!
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!