Filter löschen
Filter löschen

how to plot y=ax+b for different equations

5 Ansichten (letzte 30 Tage)
Vivi
Vivi am 25 Jun. 2013
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

Antworten (2)

Titus Edelhofer
Titus Edelhofer am 25 Jun. 2013
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 Kommentare
Vivi
Vivi am 25 Jun. 2013
is that use'if' fuction to set up X?
Titus Edelhofer
Titus Edelhofer am 25 Jun. 2013
Not really. You can either compute one vector for plotting like Iain did, or plot the different pieces separately using "hold on" in between.

Melden Sie sich an, um zu kommentieren.


Iain
Iain am 25 Jun. 2013
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 Kommentar
Titus Edelhofer
Titus Edelhofer am 25 Jun. 2013
You will need to use
(x>0 & x<=1) .* 1
and likewise for the other terms. Note the ".*" instead of "*".

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by