LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

How to plot y=mx+c

Hi,

 

I'm trying to complete a line of best fit generator for a university assignment.

I realise LabView already has the ability to plot it for me, but my assignment requires me to do it myself with my own code.

 

The problem I am stuck on right now is how to plot a straight line on a graph; given y, m and x.

 

I have a graph, and I have the code to calculate the y, m and x of my line, but I cant understand how to plot this line on my graph.

 

 

I'm sorry this is such a mundane problem, but I've been looking for days now and I still can't find how, and I must be thinking about it entirely the wrong way.

 

Attached is a quick screenshot and my VI if it helps. Also attached is a portion of the document I'm following that my professor made to help me.

 

Thank you in advance, you clever people 🙂

Download All
0 Kudos
Message 1 of 8
(4,947 Views)

Once you calculate your m and c, just use your x values to calculate the y values based on the line you calculated.  That will give you and array of x values and an array of y values to put into your graph.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 2 of 8
(4,910 Views)

Oh, and you can simplify your code A LOT by letting LabVIEW work on the arrays directly.  There is actually no reason here to use Index Array.


GCentral
There are only two ways to tell somebody thanks: Kudos and Marked Solutions
Unofficial Forum Rules and Guidelines
"Not that we are sufficient in ourselves to claim anything as coming from us, but our sufficiency is from God" - 2 Corinthians 3:5
0 Kudos
Message 3 of 8
(4,907 Views)

The main problem is that your document is wrong. The sums actually need to be taken from i=0 to i=N-1 (The document says i=1 to i=N-1, which is incorrect).

 

You need to make the VI scalable, so the code works no matter how many points there are in the input arrays. You don't want to write different subVIs for each array size, right? Also remember that "index array" is resizable, so would only need one instance to get all elements.

 

As Tim said, you can get N for each array by using "array size" (you might even include error handling and return an error if the X and Y arrays are of different size or if both are size=0, for example.). The rest of the code can be done by operating directly on arrays.

 

Yes, as you pointed out, there is a linear fit built into LabVIEW. I would still place it on the diagram, just to see if your own code returns about the same results. Two opinions are better than one! (most likely they will not be identical to the last bit, but that is to be expected).

 

Before saving an example VI, you should fill the values from the document into the x and y arrays and make the values the default so you don't need to enter anything next time you work on the VI.

 

And, yes follow your advisors suggestion and build the code into a subVI. I would recommend to use the same connector pattern as the built-in linear fit subVI, just leave out the stuff that you don't use.

 

Here's one possibility how the flat code (not subVI yet) would look like. Study it in detail and make sure you understand every single part of it, then do your own. Good luck!

 

 

0 Kudos
Message 4 of 8
(4,805 Views)

Thank you both so much for your helpful replies! I've started studying your suggestions and making the appropraite changes.
As a side note, have you any ideas as to how to make the line of best fit a curve? As in polynomial regression. I understand the basic idea but again, I can't seem to translate my intentions into LabVIEW.

0 Kudos
Message 5 of 8
(4,537 Views)

Mathematics >> Fitting >> General Polynomial Fitting.vi.

 

Lynn

0 Kudos
Message 6 of 8
(4,534 Views)

SafetyCarrot wrote:

As a side note, have you any ideas as to how to make the line of best fit a curve? As in polynomial regression.


Well, yes. However, earlier you said that:


@SafetyCarrot wrote:

I realise LabView already has the ability to plot it for me, but my assignment requires me to do it myself with my own code.


Your linear fit IS already a polynomial regression (order=1), so all you need is add higher orders. Are you now allowed to use the built-in functions?

 

Just replace the part where you get the line coefficients with "general polynomial fit". I would also recommend to generate the fit curve with a denser series of x-values over a slightly wider range in order to better show the curvature. Simply create a new x-ramp (e.g. using "ramp pattern" and, together with the best fit polynomial coefficients, use "polynomial evluation" to generate the new curve for display.

 

Now make the code scalable and make the polynomial order a control. Now you can do linear fit or any order of polynomial fit all with exactly the same code! 😄

 

See how far you get....

 

 

 

0 Kudos
Message 7 of 8
(4,463 Views)

Here's what I had in mind. Maybe it will give you some ideas....

 

 

 

 

0 Kudos
Message 8 of 8
(4,444 Views)