cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X

Can't use Polyfit on time vector (Getting error 'This Vector has the wrong number of Elements')

SD_9202574
2-Guest

Can't use Polyfit on time vector (Getting error 'This Vector has the wrong number of Elements')

The premise of this problem is I am using a spectrometer to collect data through a sample, and am trying to relate those values to data I am pulling off another measuring instrument (Called M1 here).  I am using polyfit to generate a line of best fit through the data pulled from M1, and then trying to use that function on the time vector for the data collected from the spectrometer to relate the scope data to the M1 data (making it time independent)

 

The issue here is I am struggling to use polyfit the way I would like to.  I want to use the  function I generate from polyfit on a vector from an excel sheet, not a vector I have to generate myself in MathCAD.  The reason for this being that if I have an error in data collection, the time vector for my spectrometer may be non-continuous. 

 

I've attached a MathCAD sheet that hopefully clarifies the problem further. And if you're reading this, thanks!

1 ACCEPTED SOLUTION

Accepted Solutions

  1.  You write "Time vector to apply equation over (Equation doesn't work when using OptiTime, why?)"
    I am not quite sure what you mean. Maybe you confuse "range variable" and "vector". They are quite different things even though Prime unfortunately displays them the same ways. What you get from Excel is a vector. What you define by tt:=t0,t1 .. t2 is a range!
  2. Werner_E_1-1607553098040.png

    Whenever you feed a vector as argument into a function and expect Prime not to make vector calculations but to take every single element of that vector, apply the function to it and collect the results in a result vector, you should use vectorization (shown by the arrow over the expression).

    Werner_E_2-1607553306144.png

     


  3. This mix of range and vector does not work.
    Guess you are looking for something like this:
    Werner_E_5-1607554436062.png
    Again you have to use vectorization. Additionally it seems to me that you have to divide those unitless (why?) values by 3600 as they look to me like values of seconds and M1Eqn expects unitless values in hours.
    You may consider adding units to ScopeData.1
    Werner_E_6-1607554693136.png

     

Afterthought: ad 1)

Werner_E_0-1607555294377.png

 

Prime 6 sheet attached

 

EDIT: Obviously it took me too long to formulate my answer and edit the sheet. When I started my answer there were no other answers yet. Guess I am getting slow.

 

View solution in original post

7 REPLIES 7
LucMeekes
23-Emerald III
(To:SD_9202574)

What you defined as tt is NOT a vector, it's a Range.

You can check that by trying to index into tt. If you type:

tt[1=

You get an error message.

You have to define tt as a vector. There are several ways to do that. Here's one:

tt:matrix(3.4/0.002,1,max)*0.002hr

(This creates tt as a matrix of 3.4/0.002= about 1700 rows, and 1 column, with element values starting at 0, which is multiplied by the step size of 0.002 hr. Make sure you label max as a function and hr as a unit)

 

Success!

Luc

Thanks so much for the help!  What you said about vectors vs. ranges makes a lot of sense.

My main issue here was that tt actually did work as a range for the polyfit, but the vector M1Time doesn't work with polyfit 

 

When I used the below code I can change tt to a vector, but then the polyfit doesn't work anymore, returning the error 'This vector has the wrong number of elements'.

 

Is the explanation here that polyfit only works with ranges, or am I missing something? (I suspect the latter)

I uploaded an updated file with the errors using tt as a vector below. 

  1.  You write "Time vector to apply equation over (Equation doesn't work when using OptiTime, why?)"
    I am not quite sure what you mean. Maybe you confuse "range variable" and "vector". They are quite different things even though Prime unfortunately displays them the same ways. What you get from Excel is a vector. What you define by tt:=t0,t1 .. t2 is a range!
  2. Werner_E_1-1607553098040.png

    Whenever you feed a vector as argument into a function and expect Prime not to make vector calculations but to take every single element of that vector, apply the function to it and collect the results in a result vector, you should use vectorization (shown by the arrow over the expression).

    Werner_E_2-1607553306144.png

     


  3. This mix of range and vector does not work.
    Guess you are looking for something like this:
    Werner_E_5-1607554436062.png
    Again you have to use vectorization. Additionally it seems to me that you have to divide those unitless (why?) values by 3600 as they look to me like values of seconds and M1Eqn expects unitless values in hours.
    You may consider adding units to ScopeData.1
    Werner_E_6-1607554693136.png

     

Afterthought: ad 1)

Werner_E_0-1607555294377.png

 

Prime 6 sheet attached

 

EDIT: Obviously it took me too long to formulate my answer and edit the sheet. When I started my answer there were no other answers yet. Guess I am getting slow.

 

Thanks!  The two big things I was missing here were the difference between range variables vs. vectors and using the vectorization symbol when evaluating M1Eqn.  

 

Everything works now!  This is the graph I was looking for in the end for those interested; it can relate scope data to the measured variable in the experiment.  

SD_9202574_0-1607555325052.png

 

Not using units on the X or Y axis does hurt my heart, but the X-Axis units here are Analog-Digital Scope Counts (or bits, which I don't think is a unit in mathCAD) and the Y-Axis units are % saturation of the sample.  

 

In any case, thank you so much for the help, I really appreciate it.  

 


Not using units on the X or Y axis does hurt my heart, but the X-Axis units here are Analog-Digital Scope Counts (or bits, which I don't think is a unit in mathCAD) and the Y-Axis units are % saturation of the sample. 

Look at the sheet I had attached in my last answer. Here you should see how to work with the units as well. You may also add units to M1 and the polyfit function will still work OK and return the appropriate units.

You may also define your own unit "bits" by writing bits:=1 and label "bits" as unit. So you can use it in the plots.

percent (%) is already available in Prime as sort of pseudo unit.

 

Is there a way you would be able to save it as Prime 5?  I unfortunately can't view it as a Prime 6 file:)

 

Update: No need, your suggestion for writing things out as bits is exactly what I did and I implemented % as well.  I also saw that saving to Prime 5 in Prime 6 is not really a thing.  One final thank you for the help!


@SD_9202574 wrote:

Is there a way you would be able to save it as Prime 5?  I unfortunately can't view it as a Prime 6 file:)


Unfortunately PTC in its "wisdom" has decided that there should be no way back and provides no way to convert Prime sheets to older Prime versions.

I opened an empty Prime 5 sheet in parallel with the Prime 6 file (I have both versions installed) and tried to copy the regions from one sheet to the other. This procedure worked with versions prior of P6 ("converting" P5 files to P4) but it fails no with error messages.

So the best I can do is to make a pdf print for you to look at.
I am not sure but it may be that Luc had found a way to convert sheets to older Prime format.
So I attach the pdf along with an updated P6 file. I added the plot you were looking after at the end.

BTW, I was wrong when i stated that % would be a pseudo unit in Prime - its rather a number format you can select for the axis values and not a unit which you type into the unit place holder.

Werner_E_0-1607557768861.png

 

BTW, you may also consider defining ScopeData.1:=submatrix(....)^T. That way you can avoid to always use the transpose operator when using ScopeData.1.

 

You may also define a unit kbit:=1000 and use it in the plot

 

Top Tags