Piecewise function for structural analysis
Mostrar comentarios más antiguos
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 comentario
Andrew Newell
el 17 de Abr. de 2017
Respuestas (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
el 17 de Abr. de 2017
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
Categorías
Más información sobre Language Fundamentals en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!