From Friday, April 19th (11:00 PM CDT) through Saturday, April 20th (2:00 PM CDT), 2024, ni.com will undergo system upgrades that may result in temporary service interruption.

We appreciate your patience as we improve our online experience.

# LabVIEW

cancel
Showing results 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 🙂

Message 1 of 8
(4,951 Views)

## Re: How to plot y=mx+c

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.

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
Message 2 of 8
(4,914 Views)

## Re: How to plot y=mx+c

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.

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
Message 3 of 8
(4,911 Views)

## Re: How to plot y=mx+c

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!

Message 4 of 8
(4,809 Views)

## Re: How to plot y=mx+c

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.

Message 5 of 8
(4,541 Views)

## Re: How to plot y=mx+c

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

Lynn

Message 6 of 8
(4,538 Views)

## Re: How to plot y=mx+c

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....

Message 7 of 8
(4,467 Views)

## Re: How to plot y=mx+c

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

Message 8 of 8
(4,448 Views)