Piecewise function of y

10 次查看(过去 30 天)
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!

采纳的回答

Ridwan Alam
Ridwan Alam 2020-1-7
编辑:Ridwan Alam 2020-1-7
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 个评论
Lennard Pol
Lennard Pol 2020-1-7
That's what I was looking for, thank you!
Ridwan Alam
Ridwan Alam 2020-1-7
Sure. Glad to help.

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2020-1-7
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)

类别

Help CenterFile Exchange 中查找有关 Directed Graphs 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by