leantore.blogg.se

Plotting piecewise functions in matlab
Plotting piecewise functions in matlab




plotting piecewise functions in matlab plotting piecewise functions in matlab

Hold off % good habit to hold off so that you don't accidentally plot on this fig later Plot(x, f1(x)) % x,y syntax, more apparent where the points will be plotted Then your demo would be called like so x = -1.5:0.1:1.5 % or could use linspace(-1.5, 1.5, 100) etc

plotting piecewise functions in matlab

PLOTTING PIECEWISE FUNCTIONS IN MATLAB CODE

If fplot determines the x values and calls the function as if for single points, then your code should work.Ī way to edit the task to make things clearer may be to just use the normal plot function, which I think is more useful for students to be familiar with anyway. In my above examples, I'm assuming x is being passed as a vector (which may have 1 or more elements). I currently can't access the docs, which may contain the answer. For me, your code all works anyway (2015b)! My guess is that this is something to do with how fplot is calling your functions. So, with a similar (but even simpler) approach to f2 as well, function y = f1(x)Īs for your mysteries when changing part of the piecewise function and everything working. Y = coef.*x + 2 % Coeff can be done in-line without being declared coef = (x < 0)*2 - 1 % For the above example, coef = Now it can be seen how this can be done in a one liner. Y(x<0) = x + 2 % Assign values of y, where x<0, to x + 2 Y = -x + 2 % Initialise variable y, assign its values to -x + 2 by default If the course material can be changed / extended, then a much better option in Matlab is to leverage logical indexing. % "if x < 0" is the same as "if all(x < 0)" = false, so if statement skipped This is because otherwise you may have the following issue: x = Y = zeros(size(x)) % Initialise y to the correct sizeįor ii = 1:numel(x) % Loop through elements of x (and so y) You say that you want to specifically use an if structure, in which case you will have to evaluate each element of the input vector in turn function y = f1( x ) Is there a way we can edit the exercises such that it never poses any problems but is still accessible to students having their first experiences with Matlab?.With this definition both functions are plotted correctly and Matlab seems to have no problem dealing with the conditionals. Things get stranger when we change the -x + 2 in the else part of f1 to -x^2 + 2. Then again, f2 seems to be evaluating correctly without any issues. It seems vectorization issues lie at the heart of our problem since f1(-1) evaluates correctly to 1 but f1() evaluates to. In plotting f1, however, it seems Matlab decided the first branch of the if-clause didn't matter as only the line -x+2 is plotted. The function f2 is plotted without problems. The first function, f1(x), evaluates to x+2 when x 1įinally, the plotting code should draw both functions on the interval using fplot like so ) The goal of the exercise is to draw a house in the plotting window by plotting two piecewise functions. I would like to know why these things happen so we are prepared for any difficulties the students might experience in these sessions. In the section covering the plotting of piecewise functions, we found some inconsistencies in the way Matlab handles an if condition. We are working through some material from the previous years. All computers are equipped with Matlab version R2016b. For a mathematics course for first year university science students we (the teaching assistants) need to prepare material for pc-sessions using Matlab.






Plotting piecewise functions in matlab