Community Tip - You can change your system assigned username to something more personal in your community settings. X
How do I create a plot that looks like "steps", based on a table like shown in attached table and graph?
Solved! Go to Solution.
As an addendum here is an approach which similarly to Luc's creates two vectors which can be plotted one over the other and also adds the vertical line at the beginning and the horizontal line at the and.
Worksheet in Prime 5 format attached.
Try time=(0.1 0.2 0.2 0.5 0.5 1 1 1.3)^T. y=(1.2 1.2 1.1 1.1 0.9 0.9 0.5 0.5)^T
(Note: the “^T” means take the transpose. Alternatively, enter the data directly as column vectors)
Alan
The function "linterp" is great for drawing straight lines between points. As Alan points out to get steps you need the abcissa (y) to be double valued and the ordinate (x) to match. (For linterp you need x to be lncreasing (no values the same), so
x ;= (0.1 0.1999 0.2 0.2999
and
y:=(1.2 1.2 1.1 1.1
then
Fn(xx):=linterp(x,y,xx)
Here's another way:
Prime 3.1 file attached. Runs in Express!
Success!
Luc
For a logarithmic horizontal axis:
Here are two alternatives for creating the desired step function using just the two vectors you defined.
Thank you so so much, everyone!!! I especially love the last 2 posts which use only the given table and do everything else automatically.
Werner_E, (or anyone else) could you please explain how to get those 2 vertical parallel lines that come right after you define stepfun1 and stepfun2?
To Define a Program:
On the Math tab, in the Operators and Symbols group, click Programming, and then click the Program operator, shown as a symbol with two vertical bars. A program with an empty placeholder appears.
Unless you don't have a full Prime license, but use Prime Express.
Success!
Luc
@vyerokhin wrote:
Thank you so so much, everyone!!! I especially love the last 2 posts which use only the given table and do everything else automatically.
Werner_E, (or anyone else) could you please explain how to get those 2 vertical parallel lines that come right after you define stepfun1 and stepfun2?
As Luc already said those vertical double line indicates that its a program (a feature not available in the free Prime Express version). You can either insert a program via the menu as described by Luc or via the keyboard shortcut "]" (without the quotes).
Here is an even shorter version which does not require creating a program and will work in Prime Express, too ("reverse" and "lookup" fortunately are not considered "premium" functions).
Worksheet in Prime 5 format attached.
Hi. This other setup follows the usual convention for plot finite jumps, as is described in https://en.wikipedia.org/wiki/Step_function
Best regards.
Alvaro.
A few more questions:
Q1) Is there any way to plot a vertical line up, as shown in red, from the first point, using the given data? or would I need to manually enter another point to be the first point?
Q2) If my first point starts with time=0, and I want to use a log scale - Do I need to manually change the "0" to something like "0.001", or does Mathcad have a way to do this automatically?
Q3) Using the method that "Werner_E" showed, the curve gets distorted when using a log scale. Is there a way around this?
Q1: "or would I need to manually enter another point to be the first point?"
Yes, that would be a better idea. Using that you are in control of how large the vertical axis should become.
Q2:
Time on a logarithmic scale?
Unusual, but possible. Mathcad does NOT have a way to do this automatically. When a value can be zero, it might just as easily go negative. Where should it go?
So: If you know you're going to plot on a logartithmic scale, make sure no negative or zero values are present.
Q3:
Is there a way around distorted curves using the autoplot feature?
Yes: Use Luc's method. It does not make use of autoplot, and has the advantage that you do NOT need to add trace expressions to include plot points (symbols) when you want to show them:
(Put a star on every corner)
{On the other side of the coin: If you want to plot something else against time in the same plot and want to have the curves aligned, you need time1 as a separate expression for the x-axis.}
Success!
Luc
Thank you again, Luc and Werner_E!!!
Luc - can you please explain your statement a little more:
"{On the other side of the coin: If you want to plot something else against time in the same plot and want to have the curves aligned, you need time1 as a separate expression for the x-axis.}"
I do want to add more curves to this plot - but I'm not understanding what issue you are talking about.
See attachment. (Prime 4, since Prime 3.1 crashed a few times too often on this sheet)
Success!
Luc
@vyerokhin wrote:
A few more questions:
Q1) Is there any way to plot a vertical line up, as shown in red, from the first point, using the given data? or would I need to manually enter another point to be the first point?
Q2) If my first point starts with time=0, and I want to use a log scale - Do I need to manually change the "0" to something like "0.001", or does Mathcad have a way to do this automatically?
Q3) Using the method that "Werner_E" showed, the curve gets distorted when using a log scale. Is there a way around this?
ad Q1: I'd also vote for adding an appropriate start point to your data
ad Q2: log scale on time axis sounds a bit strange, but even if you have time zero the plot should show up as invalid values simply are ignored by the plot.
ad Q3: Lucs method uses a data structure consisting of the corners. He is plotting one vector over another - this just plots the corners and the plot is setup to connect the points plotted - so this works equally well in a log plot, too.
My variants provide real function which can be evaluated at any time position. You may use step3(0.426)= and you get a result. You have to decide which approach fits your needs best.
The reason for the distortion in your log plot is that quickplot is used in those plots. That means that we don't tell Mathcad what t is and Mathcad automatically chooses values for t at arbitrary steps for the plot. This usually works pretty fine unless you you a logarithmic abscissa. Now the values chosen by Mathcad for t are not close enough (you had chosen 100 as end value for t!) and so the plot gets somewhat distorted (less, when you chose just 100 as an end value).
Two solutions which come to mind are
1) define a range variable for t yourself. Unfortunately ranges only are linear spaced and you may run into the limit of number of points Mathcad is able to plot if you chose a range with too much values:
2) Define a vector with logarithmic spaced values and use this for plotting - Mathcad provides a function for this. Using this method, the function call has to be vectorized (the arrow operator over step1 and step 3):
As an addendum here is an approach which similarly to Luc's creates two vectors which can be plotted one over the other and also adds the vertical line at the beginning and the horizontal line at the and.
Worksheet in Prime 5 format attached.
Werner_E, Is the created "Step" variable - a matrix? I'm not familiar with superscripts - when and how to use them. Could you please explain?
Thanks again.
@vyerokhin wrote:
Werner_E, Is the created "Step" variable - a matrix? I'm not familiar with superscripts - when and how to use them. Could you please explain?
Thanks again.
Correct, "makeSteps" returns a 2-column matrix. You can evaluate Step= to see what it looks like. Personally I prefer having the data compact in one variable ("Step") and in the plot I select first and second column respective by the column selector (the superscript <..>).
Guess its a matter of personal taste and if you prefer having abscissa and ordinate data vectors in two separate variables similar to Lucs approach, you would do like this and return a 1 x 2 matrix whose two entries are the t- and the y-vector:
Not sure if I should start a new thread for this question... Please provide feedback and forgive me if I should have.
I was wondering if there is a way to modify the "makeSteps" function in the "Steps2.mcdx" file that Werner_E provided, so that it is able to take a single point to make an "L" shaped plot?
Right now, it throws back an error stating "this value must be a vector."
Thank you,
vyerokhin
The reason for the error is an inconsistent behaviour of Primes tables. Usually tables create vectors but if there is only one row, a table does not return a 1x1 vector but instead a scalar. This may be convenient in some situations but is an obstacle in case of your problem.
A workaround could be that the function makeSteps checks if the input values are scalars and then turns them into 1x1 vector. In the pic below I assume that if X is a scalar then Y will be a scalar, too, and so both are turned into vectors.
BTW, instead of writing X <- [X] you may also write X[O <- X
Thank you very much!!!
Hi,
Use of the Heaviside step function: