LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-09-2016, 08:13 PM   #1
wagscat123
Member
 
Registered: Jan 2009
Location: Maryland-Pennsylvania border, USA
Distribution: openSUSE 15.1, Tumbleweed, Ubuntu 19.10, Oracle Linux 7, Fedora 30, antiX 19, and Linux Mint 19.1
Posts: 809
Blog Entries: 44

Rep: Reputation: 113Reputation: 113
Easiest Way to Fit Cos^2(x) Curve to Data


Hi all,

I need to fit cos^(x) (or any non-power or polynomial function) data for some lab data, but can't find an intuitive way to do so. What's the most convenient and user friendly software tool for this?

Many thanks!

Last edited by wagscat123; 11-09-2016 at 09:47 PM.
 
Old 11-10-2016, 04:44 AM   #2
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware & Android
Posts: 10,988

Rep: Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250
Can't you throw it into a spreadsheet and graph it that way? Are you looking for something fancy?
 
Old 11-10-2016, 09:08 AM   #3
wagscat123
Member
 
Registered: Jan 2009
Location: Maryland-Pennsylvania border, USA
Distribution: openSUSE 15.1, Tumbleweed, Ubuntu 19.10, Oracle Linux 7, Fedora 30, antiX 19, and Linux Mint 19.1
Posts: 809

Original Poster
Blog Entries: 44

Rep: Reputation: 113Reputation: 113
Like fitting it like a line? There's no function for cos^2(x) offered in Calc (I was so desperate and tried Excel and it couldn't do it either). LabPlot and the thing is general is proving to be taking longer to figure out than the rest of the task, so I'll just throw it in a graph at this point and compare it to a cos^2(x) function.
 
Old 11-11-2016, 04:32 AM   #4
business_kid
LQ Guru
 
Registered: Jan 2006
Location: Ireland
Distribution: Slackware & Android
Posts: 10,988

Rep: Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250Reputation: 1250
In row/column A1, put 0; A2 gets A1+1; A3 gets A2+1, etc.
B1 gets cos^2(A1), and so on down. Formulae are copied intelligently in spreadsheets so it's handy. Then graph Column B
 
Old 11-11-2016, 01:09 PM   #5
wagscat123
Member
 
Registered: Jan 2009
Location: Maryland-Pennsylvania border, USA
Distribution: openSUSE 15.1, Tumbleweed, Ubuntu 19.10, Oracle Linux 7, Fedora 30, antiX 19, and Linux Mint 19.1
Posts: 809

Original Poster
Blog Entries: 44

Rep: Reputation: 113Reputation: 113
I meant like highlighting data, making a chart of it, and having a cos^2(x) line fit to the data, sort of like how you can do it to a line.

I think this isn't absolutely necessary so I've moved on. Thanks for the help though.
 
Old 11-11-2016, 01:38 PM   #6
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,464

Rep: Reputation: 2605Reputation: 2605Reputation: 2605Reputation: 2605Reputation: 2605Reputation: 2605Reputation: 2605Reputation: 2605Reputation: 2605Reputation: 2605Reputation: 2605
there is octave
the matlab ( almost) clone

what is this data ? and what format is it in ?

guessing a CSV file


have you looked at Python ? and use numpy
 
Old 11-12-2016, 07:51 PM   #7
wagscat123
Member
 
Registered: Jan 2009
Location: Maryland-Pennsylvania border, USA
Distribution: openSUSE 15.1, Tumbleweed, Ubuntu 19.10, Oracle Linux 7, Fedora 30, antiX 19, and Linux Mint 19.1
Posts: 809

Original Poster
Blog Entries: 44

Rep: Reputation: 113Reputation: 113
I was just hoping for an overly simplistic solution, and kinda gave up when I saw it involved that much learning for such a small task.

But it was simple intensity measurements of vertically or horizontally polarized light versus the angle of the (polarizer) analyzer it went through if you were curious.
 
Old 11-15-2016, 04:41 PM   #8
tofino_surfer
Member
 
Registered: Aug 2007
Posts: 387

Rep: Reputation: 108Reputation: 108
>>>There's no function for cos^2(x) offered in Calc (I was so desperate and tried Excel and it couldn't do it either)

Since cos^2(x) = (cos(x))^2 = cos(x)*cos(x) or power(cos(x), 2) what is the problem ?

Calc has all of the trigonometic functions and the power function for squaring a base to an exponent. I just tried both forms in Calc. You don't need a function for cos^2(x) directly as it can be derived. This makes spreadsheet interfaces simpler.
 
Old 12-03-2016, 07:06 PM   #9
wagscat123
Member
 
Registered: Jan 2009
Location: Maryland-Pennsylvania border, USA
Distribution: openSUSE 15.1, Tumbleweed, Ubuntu 19.10, Oracle Linux 7, Fedora 30, antiX 19, and Linux Mint 19.1
Posts: 809

Original Poster
Blog Entries: 44

Rep: Reputation: 113Reputation: 113
Quote:
Originally Posted by tofino_surfer View Post
>>>There's no function for cos^2(x) offered in Calc (I was so desperate and tried Excel and it couldn't do it either)

Since cos^2(x) = (cos(x))^2 = cos(x)*cos(x) or power(cos(x), 2) what is the problem ?

Calc has all of the trigonometic functions and the power function for squaring a base to an exponent. I just tried both forms in Calc. You don't need a function for cos^2(x) directly as it can be derived. This makes spreadsheet interfaces simpler.
Is there a way to fit this as a curve?
 
Old 12-06-2016, 11:16 PM   #10
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 524

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
If you measured the light intensity at evenly spaced intervals (for example, every 15 degrees) over a 180 degree or 360 degree range, then you could do a very simple Fourier transform to get the amplitude and phase of the cos^2(x), using the trigonometric identity cos^2(x)=0.5(cos(2*x)+1).

I will try to post a sample program in the near future.

Last edited by Beryllos; 12-06-2016 at 11:32 PM.
 
Old 12-07-2016, 03:31 AM   #11
Beryllos
Member
 
Registered: Apr 2013
Location: Massachusetts
Distribution: Debian
Posts: 524

Rep: Reputation: 316Reputation: 316Reputation: 316Reputation: 316
example program (with data):
Code:
# example-2016-12-07.py
# example program to fit cos^2(x) function with arbitrary phase and DC offset
# to equally spaced data (see comments below) using a two-point Fourier transform.

import math

# conversion factor
radian_per_degree = math.pi/180.0

# For accurate FT results, data must be uniformly distributed in x as follows:
# x spacing = 180/N degrees with N >= 3 points,
# or x spacing = 360/N degrees with N == 3 or N >= 5 points.
# Caution: Program does not check validity of data distribution in x.

# The following example data consists of phase shifted cos^2(x) + DC offset + noise.
# rawdata is given as a list of points. Each point is given as list of [angle, intensity].
rawdata = [[0.0, 13.2], [30.0, 20.8], [60.0, 20.6], [90.0, 11.3], [120.0, 2.9], [150.0, 4.8]]

# initialize sums to zero
num_pts = 0
c2x = 0.0
s2x = 0.0
dc = 0.0

# compute Fourier transform (only the 3 necessary coefficients)
for point in rawdata:
    num_pts += 1
    c2x += math.cos(radian_per_degree*2*point[0])*point[1]
    s2x += math.sin(radian_per_degree*2*point[0])*point[1]
    dc += point[1]
c2x *= 2.0/num_pts
s2x *= 2.0/num_pts
dc *= 1.0/num_pts

# print results to terminal
print("Curve is", c2x, "* cos(2*x) +", s2x, "* sin(2*x) +" , dc)
peak = math.atan2(s2x, c2x)/radian_per_degree/2
if peak < 0:
    peak += 180.0
print("Estimated peak is at", peak, "degree")

# done
Run it in python3:
Code:
$ python3 example-2016-12-07.py
Curve is 0.9833333333333348 * cos(2*x) + 9.728352035845194 * sin(2*x) + 12.266666666666667
Estimated peak is at 42.11409808082147 degree
$
 
Old 12-07-2016, 07:04 AM   #12
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 5,358

Rep: Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971Reputation: 1971
An alternative would be to look at R. Pinched from
Code:
library(nls2)
x <- c(0.00, 0.41, 0.76, 1.20, 1.55, 2.00, 2.34, 2.81, 3.10)
y <- c(0.99, 0.74, 0.49, 0.13, 0.05, 0.14, 0.51, 0.84, 0.98)
m <- nls2(y ~ a * (cos(x) * cos(x)), start=c(a=1))
cor(y,predict(m))
# Uncomment the next line for plotting
#png(file = "plot.png")
plot(x,y)
lines(x,predict(m),lty=2,col="red",lwd=3)
# Uncomment the next line for plotting
#dev.off()
Attached Thumbnails
Click image for larger version

Name:	plot.png
Views:	9
Size:	13.0 KB
ID:	23705  

Last edited by allend; 12-07-2016 at 03:00 PM. Reason: Fixed the link
 
Old 12-07-2016, 09:42 AM   #13
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,078
Blog Entries: 4

Rep: Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179Reputation: 3179
For what it's worth, "R" is a very important tool (open source) that you need to spend time getting to know.

I was recently involved in a project that used both SPSSŪ and SASŪ, and I was very pleasantly surprised to find that both of these now have well-integrated interfaces to "R."

URL: https://www.r-project.org

Quote:
Originally Posted by www.r-project-org:
R, like S, is designed around a true computer language, and it allows users to add additional functionality by defining new functions. Much of the system is itself written in the R dialect of S, which makes it easy for users to follow the algorithmic choices made. For computationally-intensive tasks, C, C++ and Fortran code can be linked and called at run time. Advanced users can write C code to manipulate R objects directly.

Many users think of R as a statistics system. We prefer to think of it of an environment within which statistical techniques are implemented. R can be extended (easily) via packages. There are about eight packages supplied with the R distribution and many more are available through the CRAN family of Internet sites covering a very wide range of modern statistics.
I think that it's popular partly because its approach is just a bit different. An upward-compatible language from (of course) "S," it enables you to build analyses with the full power of a programming language, instead of having to "piece together 'wedgies' that don't quite fit together" (my analogy) as you must do with most mainstream tools. In that project, I was able to use "just a little bit of 'R' juice" to replace some very-many-step processes, and yet I never had to leave the environment of the stats package I was then using because both of them had built "official" support for it.

The web site that the example was "pinched from" is also a staple: https://www.r-bloggers.com.

Last edited by sundialsvcs; 12-07-2016 at 09:49 AM.
 
Old 12-28-2016, 02:12 PM   #14
wagscat123
Member
 
Registered: Jan 2009
Location: Maryland-Pennsylvania border, USA
Distribution: openSUSE 15.1, Tumbleweed, Ubuntu 19.10, Oracle Linux 7, Fedora 30, antiX 19, and Linux Mint 19.1
Posts: 809

Original Poster
Blog Entries: 44

Rep: Reputation: 113Reputation: 113
As I did the project successfully without having to figure out the initial question, I just tried out the Python script now out of personal curiosity, and it was a great demonstration. Thank you to everyone for your answers and help!
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to implement elliptic curve cryptography(EDWARDS Curve) to my Topology surya.prakash Programming 4 07-06-2014 02:09 PM
[SOLVED] Data won't fit in backup disk lucmove Linux - General 4 12-14-2012 09:49 PM
Fit rectangle in sine curve jlinkels Programming 10 09-06-2012 12:04 PM
How much data can fit on a DVD? General General 6 06-25-2006 01:04 AM
Emergency - New H/D to transfer growing data which doesn't fit anymore in old H/D ganninu Linux - Hardware 1 07-22-2003 06:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration