Piecewise function of y
조회 수: 1 (최근 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!
댓글 수: 0
채택된 답변
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');
추가 답변 (1개)
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)
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!