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 
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto 
Site FAQ 
Sitemap 
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQrelated cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 preinstalled distros to choose from, the worryfree installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.


03152006, 08:48 PM

#1

Member
Registered: Sep 2005
Location: California
Distribution: Slackware & Debian
Posts: 264
Rep:

Matlab
Hi,
I have been fiddeling around with matlab for a bit now, and i think it's an amazing program. I am not very strong with it, but i was wondering if there is any way to calculate a sine regression to fit given data.
I have been able to graph the data using [CODEplot(time,hight,'*')][/CODE]
I am trying to fit a sine regression / best fit line to fit the data.
Does anybody here know how to do something like this?



03172006, 06:36 AM

#3

Senior Member
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515
Rep:

Is
http://www.mathworks.com/matlabcentr...bjectType=file
what you're looking for?
Matlab's help mentions multiple regression and fitting functions, but none seem to include "sine regression" (at first sight). Altough that may be included in toolboxes that I don't have installed.
Perhaps there are some alternative functions you can try at Matlab Central's file exchange:
http://www.mathworks.com/matlabcentr...oadCategory.do
in the Mathematics category probably.
Those functions may still be a little buggy though.



03172006, 09:20 AM

#4

Member
Registered: Sep 2005
Location: California
Distribution: Slackware & Debian
Posts: 264
Original Poster
Rep:

Quote:
Originally Posted by timmeke

This seems to be it. I will check it out once i get home today.
I'll keep you posted on how it goes.



03172006, 04:19 PM

#5

Member
Registered: Sep 2005
Location: California
Distribution: Slackware & Debian
Posts: 264
Original Poster
Rep:

I am not sure why i can't get it to work.
I have two lists (variables), one containing the xvalues, an other one containing the yvalues. I have not yet found a way of actually calculating a sine wave which fits these (less than 30) values.
The format for the wave should be something like this "y=a*sin(b*x+c)+c".
Is there any paractical way (without having to get a degree in advanced mathematics first) to calculate something like this??



03172006, 05:11 PM

#6

Member
Registered: Sep 2005
Location: California
Distribution: Slackware & Debian
Posts: 264
Original Poster
Rep:

I believe this kind of function is also called a "sinusoid".
Does anybody know how to get it to work??
EDIT: The TI 83 was able to compute a SinReg within a few seconds.... Why is it that hard to do it in matlab?
Last edited by wwnexc; 03172006 at 05:16 PM.



03202006, 02:33 AM

#7

Senior Member
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515
Rep:

I don't really know how the TI 83 works, so can't help you with that. It may just plot a sine that approximates the given values (not really fitting it as good as possible).
"fitting" (ie least square fitting) is usually an iterative approach. That always involves making slight changes to the variables in a function (such as a sine function) until some error value is minimized.
In your case, the error value could be something like the RMS value of distance between each value and the sine wave. Based on that RMS error indication, you can slowly adjust your sine parameters until the RMS reaches a minimum. Matlab surely has functions for that. Check out least squares fitting, for instance.
Another approach would be to just make a sinusoid that approaches your values, rather than actually "fitting" as closely as possible.
To do this, you simply need to determine 3 things: the sine's amplitude and frequency (or wavelength) and the phase.
In general, a sinusoid is something of the form:
y = A * sin(bx + c)
"A" is your amplitude, "b" is related to the frequency (or 1/wavelenght) in the x direction and "c" is the phase (in radians).
If you plot the values, you should be able to approximate each of the values.
You could also use these values as starting point for the least square fitting.
I don't really get why you included yet another "+c" in your formula for sinusoids (ie "sinelike function").
The +c you've added would probably only be an offset in the y direction (ie if the mean value of your wave isn't at y=0 but at y=c). This "y offset" is however unrelated to the sine's phase, so mathematically speaking you should call them "c1" and "c2" instead of just 2 times "c".
Last edited by timmeke; 03202006 at 02:34 AM.



03202006, 09:16 PM

#8

Member
Registered: Sep 2005
Location: California
Distribution: Slackware & Debian
Posts: 264
Original Poster
Rep:

"y=a*sin(b*x+c)+c" should be "y=a*sin(b*x+c)+d".
I am not sure how i could have ever made that typo.
I think i got the concept now: one has to specify the starting values for each variable. I though that matlab would simply calculate a best fitting line, without any startdata given.



03212006, 01:43 AM

#9

Senior Member
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515
Rep:

No. I believe there is no such thing like "fitting without starting data".
You normally need to have starting data, provided from an approximation, a best guess, etc.
Fitting is typically an iterative approach, starting with some data, that adjust the variables slightly (in each iteration), until some error value is minimized.
The downsides of such an iterative algorithm are
1. performance
2. they can get stuck on "local minima". Let's say that the error is decreasing, reaches a local minimum and then increases slightly before going down again to the real minimum. The iterative algo may stop at the local minimum instead of pursuing the real one. Their are alternative approaches, such as neural networks, to get past this disadvantage.
I recommend you try the least squares fit.



All times are GMT 5. The time now is 11:24 PM.

LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.

Latest Threads
LQ News

