Tuesday, December 6, 2022

Plotting Basic Functions in C/C++


Plotting Basic Functions in C/C++

If you would like a video tutorial walking you step-by-step through how to install gnuplot, follow this link!

https://arizona.zoom.us/rec/share/jYW1eye3tC01Fh51Z4ZqPG1u-jqWfn6xGIVOv7AWAbjMKaiL5u03f7nGc3uAe-0e.pLHzTTMbRRYEhI3U?startTime=1670621330000

In order to plot basic functions in C/C++ through Visual Studio Code (VSC), we need a library!

• A library in programming languages is a collection of prewritten code that users can use in order to optimize their code beyond what is available in VSC. Some examples include NumPy and Matplotlib, which is commonly used in Python programming language.

• They also work for a variety of programming languages, so it is highly versatile.




https://designenterprisestudio.com

For plotting data, we will install the library called gnuplot by simply going to this link:

https://sourceforge.net/projects/gnuplot/files/gnuplot/


In the video tutorial, we go through a more extensive process, but we have put the direct link here for your convenience :)

You will then scroll down and press the folder labeled 5.2.0. This is not the latest version, so you are welcome to download the latest version with a few more bug fixes, but this one will work fine for our purposes.

Press the gp520-win64-mingw.exe towards the top of the list, which will then download and appear near the bottom left corner of your screen. Click that, and it will download. Let it to make changes to your computer, and just keep pressing Continue and Ok until the download is complete.

Once it is completely installed, you will open File Explorer on your computer, and go to Downloads, and the most recent download named gp520-win64-mingw will be there. This shows that the download was successful.

As you did when you downloaded the compiler, we need to edit the Environment Variables on your computer. You will do this by going to This PC and going to the drive, whatever it may be named on your device, and click on the folder labeled Program Files. From there, you will see a folder labeled gnuplot, and open that. As before, open the bin folder, and go to the bar above it and copy the property name in its entirety.

Go back to This PC, and right click anywhere on there without an icon being selected, and open Properties. In the settings search bar, type Advanced System Settings and select View Advanced System Settings.

A pop up will appear, and you will then press Environment Variables. Under System Variables (the most bottom box), select Path and then Edit. Another pop up will appear, and you will press New and paste the property name you copied from earlier, and then press Ok.

At this point, your computer should now recognize gnuplot! In order to test that, in the search bar for your computer, type in Command Prompt and open it. A black box pop up will appear, and you will type gnuplot right where it wants you to type when it first opens. Press Enter on your keyboard, and if there is no error, gnuplot properties will show up, showing your computer recognizes it.

Congratulations, you have successfully downloaded and installed gnuplot onto your device! Let's test out some simple functions to see if it plots graphs.

After you have typed gnuplot and pressed Enter, after gnuplot>, type in plot sin(x)/x, making sure to have the parentheses there. Press Enter on your keyboard, and you should have a pop up like the one below show up.




One thing that is important to note is that plotting data is pretty difficult in C/C++ and complicated compared to plotting data on other programs such as Excel or MatLab. Plotting in C/C++ works best for basic functions such as trigonometric and x. Probably the most effective use for plotting in C/C++ is the ability to hover your cursor over any point on the curve and be able to have precise x and y coordinates pop up in the far left bottom corner of the graph window. You can also change where you are looking into on the graph by zooming in and out of it using your mouse. Plotting data is possible by having a text file or a data file created in C/C++, but that is far beyond a beginner's ability, and can be done faster and easier through Excel.


Some Extra Practice

Mess around with different elementary functions, and altering those from standard conditions. Try plotting:

cos(x)

(x+2)-4

cos(x)/x

10sin(x)

7x+9

Also, find the coordinates of the top of the third peak on the graph sin(x)/x


Answers

Syntax -


gnuplot>plot cos(x)


Graph -




Syntax -


gnuplot>plot (x+2)-4


Graph -






Syntax -


gnuplot>plot cos(x)/x


Graph -






Syntax -


gnuplot>plot sin(x)*10


Graph -






Syntax -


gnuplot>plot (x*7)+9


Graph -





Coordinates of the top of the third peak on the graph sin(x)/x -

x = 7.69321

y= 0.127998

See! You can see the x and y coordinates in the bottom left corner of the window by hovering over the top of the third peak!

































No comments:

Post a Comment