# Plotting two functions with ranges in one plot

조회 수: 55(최근 30일)
Wouter Eppink 2019년 9월 28일
댓글: Matt J 2019년 9월 28일
Hello, I have trouble finding out how to put seperate ranges on my plot.
As of right now this is my code:
x=[0: 1: 6]
M1=9.*x-3.*x.^2
M2=-1.*(3.*(6-x).^2);
figure
plot(x, M1, x, M2)
grid
The first function's (M1) variable "x" should have ranges from 0 to 4 (x >= 0 and x < 4)
The second function's (M2) variable "x" should have ranges from 4 to 6 (x >= 4 and x < 6)
Anyways I need my plot to look like this: ##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Wouter Eppink 2019년 9월 28일
The eraser tool from an amazing piece of software called ms paint.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Matt J 2019년 9월 28일
편집: Matt J 2019년 9월 28일
x1=0:4;
x2=4:6;
M1=9.*x1-3.*x1.^2;
M2=-1.*(3.*(6-x2).^2);
figure
plot(x1, M1, x2, M2);
ylim([-120,20]);
grid

댓글을 달려면 로그인하십시오.

### 추가 답변(2개)

Catalytic 2019년 9월 28일
You could also edit away the data points you don't want
h=plot(x, M1, x, M2); ylim([-120,20]);
h(1).XData(6:7)=[]; h(1).YData(6:7)=[];
h(2).XData(1:4)=[]; h(2).YData(1:4)=[];
##### 댓글 수: 0표시숨기기 이전 댓글 수: -1

댓글을 달려면 로그인하십시오.

Wouter Eppink 2019년 9월 28일
However the awnser works fine for now. However when I'm putting more than two functions in one graph this way, I'd like to find a way to incorporate the range into the function in order to decrease the amount of extra variables
##### 댓글 수: 1표시숨기기 없음
Matt J 2019년 9월 28일
Which of the 2 answers are you referring to?

댓글을 달려면 로그인하십시오.

R2018b

### Community Treasure Hunt

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

Start Hunting!