-1

I'm interested in plotting the following function in Matlab, but without succes.

enter image description here

I can't manage to plot the points.

 x = -1:0.1:3;
 if (x<=1)
     y = x*x-x+1
     plot(x,y)
 else
     y = 2*x+3
     plot(x,y)
 end
Adriaan
  • 17,741
  • 7
  • 42
  • 75
Shury
  • 468
  • 3
  • 15
  • 49
  • Look [here](http://math.stackexchange.com/questions/533221/piecewise-function-plot-in-matlab) for some tips. – Benoit_11 Oct 07 '15 at 20:33
  • 1
    What should f(x=1) be equal to? – brodoll Oct 07 '15 at 20:37
  • You're using an `if` statement in conjunction with a vector. What do you expect will happen? – rayryeng Oct 07 '15 at 20:54
  • @brodroll technically both 1 and 5, since both branches of the function contain 1 in their domain. A mathematician would be concerned; I, being a physicist, do not care too much about limit and boundary values in singular points. If it really mattered the domains should be changed. – Adriaan Oct 07 '15 at 20:56
  • Related question+answer: http://stackoverflow.com/questions/30790802/how-to-make-a-graph-of-a-three-branches-function-in-matlab/30845312#30845312 – Dev-iL Oct 07 '15 at 21:00
  • @Adriaan I bet it won't be a problem. It is just something that called my attention when I layed my eyes onto it. Technically you can't even call f a function the way it is written unless, as you said, the domains are changed – brodoll Oct 07 '15 at 21:14
  • If the answer below helped you, please consider accepting it. – Adriaan Oct 12 '15 at 21:17

1 Answers1

3

The if statement you defines takes the condition for the entire array, which means all entries should adhere to the statement. Since only the first 21 adhere to the condition posed, the if statement goes to the else and plots a straight line.

Your equation for the first line is incorrect, since x*x results in an error since MATLAB assumes this to be a matrix multiplication and the sizes are not correct for that. The reason you are not seeing this error is due to the if statement, since, as explained above, that never reaches this line. You should change that equation using the dot-multiplication, which does things element-wise as opposed to array/matrix-wise.

The equation for the second line is correct.

If your if/else statement would be correct your first plot would be overwritten by the second, since you did not specify the hold on switch to figures.

As a note I also used the semicolon ; after each statement, which prevents it from printing the output of a line to the console.

x1 = [-1:0.01:1].';
x2 = [1:0.01:3].';
y1 = x1.^2-x1+1;
y2 = 2*x2+3;

figure;
hold on
plot(x1,y1)
plot(x2,y2)

plot

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • The `else` in the original code is causing major grief, I wouldn't say that it is correct except for `hold on`. – Ben Voigt Oct 07 '15 at 20:38
  • And "you were overwriting your plot of the first part with the second bit" definitely does not apply to the code in the question. – Ben Voigt Oct 07 '15 at 20:40
  • @BenVoigt thanks for pointing this out. I addressed both points in the answer. – Adriaan Oct 07 '15 at 20:47
  • You could also mention that his else statement is never entered for an x value of 1. if(x<= 1) includes the 1, so the else wouldn't be entered for this value. I realize your solution avoids the if/else all together, just something to note. – ballBreaker Oct 07 '15 at 21:02
  • @BenVoigt not true, try running it for yourself. You get a nice straight line from x=-1 to x=3, because *not all* x<=1 it does actually execute the statement within `else`. – Adriaan Oct 07 '15 at 21:16
  • @Adriaan: Yes, I just found the documentation. Indeed it implies an `all()`, not an `any()`. http://www.mathworks.com/help/matlab/ref/if.html – Ben Voigt Oct 07 '15 at 21:17
  • @Dustiny: `else` isn't entered for individual values. Control either flows to the then-part, or the else-part. – Ben Voigt Oct 07 '15 at 21:19