Piecewise function of y

조회 수: 1 (최근 30일)
Lennard Pol
Lennard Pol 2020년 1월 7일
댓글: Ridwan Alam 2020년 1월 7일
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에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by