Piecewise function of y

8 vues (au cours des 30 derniers jours)
Lennard Pol
Lennard Pol le 7 Jan 2020
Commenté : Ridwan Alam le 7 Jan 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!

Réponse acceptée

Ridwan Alam
Ridwan Alam le 7 Jan 2020
Modifié(e) : Ridwan Alam le 7 Jan 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 commentaires
Lennard Pol
Lennard Pol le 7 Jan 2020
That's what I was looking for, thank you!
Ridwan Alam
Ridwan Alam le 7 Jan 2020
Sure. Glad to help.

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 7 Jan 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)

Community Treasure Hunt

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

Start Hunting!

Translated by