Piecewise function for structural analysis
이전 댓글 표시
Hi, I'm trying to graph several different piece wise functions. I'm having a lot of trouble trying to figure out how to use matlab. Basically i have four different functions, each function within a certain value.
F(x)= 176x for when 0<x ≤5
F(x)= 1320-88x for when 5<x ≤10
F(x)= 188x-1220 for when 10<x ≤15
F(x)= 5840-276x for when 15<x ≤20
i have to do this for 5 separate examples, and some of the equations contain various x's at different powers.
any help would be greatly appreciated thank you
댓글 수: 1
Andrew Newell
2017년 4월 17일
답변 (2개)
MATLAB is most efficient when working with complete arrays at once, so you can simply use logical indexing and create the piece-wise function:
function y = myfun(x)
%
y = nan(size(x));
%
idx = 0<x & x<=5;
y(idx) = 176*x(idx);
%
idx = 5<x & x<=10;
y(idx) = 1320-88*x(idx);
%
idx = 10<x & x<=15;
y(idx) = 188*x(idx)-1220;
%
idx = 15<x & x<=20;
y(idx) = 5840-276*x(idx);
%
end
Star Strider
2017년 4월 17일
Similarly, using an anonymous function implementation:
F = @(x) ((0 < x) & (x <= 5)).*(176*x) + ((5 < x) & (x <= 10)).*(320 - 88*x) + ((10 < x) & (x <= 15)).*(188*x - 1220) + ((15 < x) & (x <= 20)).*(5840 - 276*x);
x = linspace(0, 20);
figure (1)
plot(x, F(x))
grid
카테고리
도움말 센터 및 File Exchange에서 Language Fundamentals에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!