how to plot y=ax+b for different equations

How to plot a line for different equations for the x and y, for example: y=1 when 0<x<=1; y=2x+1 when 1<x<=3; y=-2x-1 when 3<x<=5; y=0 when x>5

답변 (2개)

Titus Edelhofer
Titus Edelhofer 2013년 6월 25일

0 개 추천

Hi Vivi,
  • set up a vector x between 0 and 1
  • compute y
  • plot y vs. x
  • hold on
  • do the same for the interval [1 3]
Titus

댓글 수: 2

Vivi
Vivi 2013년 6월 25일
is that use'if' fuction to set up X?
Not really. You can either compute one vector for plotting like Iain did, or plot the different pieces separately using "hold on" in between.

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

Iain
Iain 2013년 6월 25일

0 개 추천

x = min_desired_x:step_size:max_desired_x;
y = (x>0 & x<=1) * 1 + ...
(x>1 & x<=3) * (2*x+1) + ...
(x>3 & x<=5) * -(2*x+1) + ...
(x>5 ) * 0;
plot(x,y)

댓글 수: 1

You will need to use
(x>0 & x<=1) .* 1
and likewise for the other terms. Note the ".*" instead of "*".

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

카테고리

도움말 센터File Exchange에서 Annotations에 대해 자세히 알아보기

태그

질문:

2013년 6월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by