Path: news.mathworks.com!not-for-mail
From: <HIDDEN>
Newsgroups: comp.soft-sys.matlab
Subject: Re: solution/fit for f(x)=A*cos(B*x)
Date: Wed, 9 Jul 2008 07:29:02 +0000 (UTC)
Organization: Inst of Thermomechanics AS CR
Lines: 53
Message-ID: <g51pbu$5ln$1@fred.mathworks.com>
References: <24427446.1215576592399.JavaMail.jakarta@nitrogen.mathforum.org>
Reply-To: <HIDDEN>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1215588542 5815 172.30.248.37 (9 Jul 2008 07:29:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 9 Jul 2008 07:29:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 79193
Xref: news.mathworks.com comp.soft-sys.matlab:478327



Leaf Man <imiller25@live.com> wrote in message
<24427446.1215576592399.JavaMail.jakarta@nitrogen.mathforum.org>...
> Hello,
> 
> I'm trying to solve f(x)=A*cos(B*x) to get estimates of
the parameters A and B where I have a vector for f(x),x. I'm
also looking to give a confidence interval for f(x)=Acos(Bx)
given f(x),x and report errors for A and B. Any help would
be much appreciated!
> 
> Thanks.

Hi

If nobody answers, I am sending you a dirty solution of a
simulated example:

% acosbx.m
% Model parameters:
a = 10;
b = 3;
x = (0:.125:5)';
y = a*cos(b*x) + randn(size(x)); % cosine with noise
plot(x,y,'or')
% Solution:
ae = max(y);   % Initial guess of a
w  = acos(y/ae)./x;
I  = find(diff(w)<0);
J  = find(diff(I)==1);
be = mean(w(I(2:round(end/3))));   % Initial guess of b
fx = @(c) c(1)*cos(c(2)*x) - y;
[c,ssq,cnt] = LMFnlsq(fx,[ae,be])  % from FEX, Id 17534
c = real(c);
hold on
plot(x,fx(c)+y)
hold off

The solution c = [a; b] has been obtained in 5 iterations:

>> acosbx
ae =
   11.0337
be =
    3.0964
c =
    9.7358
    2.9971
ssq =
   45.5168
cnt =
     5

Mira