Piecewise function of y

1 visualización (últimos 30 días)
Lennard Pol
Lennard Pol el 7 de En. de 2020
Comentada: Ridwan Alam el 7 de En. de 2020
I want to create a very simple graph consisting of two vertical lines interconnected by an oblique line. I have three expressions for a certain correction factor C, of which the middle one is a function of height. All three are valid for certain ranges of height. Here is my code:
height = [0:180];
C1 = 1.037; % between 10 and 20 m
C2 = (-0.0017 * height) + 1.071; % between 20 and 90 m
C3 = 0.918; % between 90 and 180 m
plot([C1 C1], [height(10) height(20)], 'r');
hold on
plot(C2, height, 'r');
hold on
plot([C3 C3], [height(90) height(180)], 'r');
axis([0.5 1.2 0 180]);
grid on
This results in a graph that doesn't satisfy my needs; I want to get rid of the 'tails' caused by the C2 expression, but everything I try results in a vector that is too short..
I hope you can help me out!

Respuesta aceptada

Ridwan Alam
Ridwan Alam el 7 de En. de 2020
Editada: Ridwan Alam el 7 de En. de 2020
It's a bit hard to understand the issue here. Please let me know if this is not what you are looking for:
plot(C2(20:90), height(20:90), 'r');
  2 comentarios
Lennard Pol
Lennard Pol el 7 de En. de 2020
That's what I was looking for, thank you!
Ridwan Alam
Ridwan Alam el 7 de En. de 2020
Sure. Glad to help.

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 7 de En. de 2020
Start off with a C vector that is the same size as height.
C = zeros(size(height));
Now use the technique shown in the "Replace Values That Meet a Condition" section on this documentation page to fill in the appropriate sections of C.
C(height < 20) = -1;
case2 = 20 <= height & height < 90;
C(case2) = 2*height(case2);
Note that where C depends on height in this second case, I'm filling in a section of C using the same section of height. If I tried to fill in that section of C using all of height it would work as well as fitting a dozen eggs into one cup of an egg carton. MATLAB doesn't like making scrambled elements like that.
You should be able to use this technique to fill in C. Then plot height and C.
plot(height, C)

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by