Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

complex function fitting?

93 views
Skip to first unread message

Ned Lieb

unread,
Apr 6, 2009, 5:04:48 AM4/6/09
to
Does anyone know how I could extend the domain of Mathematica's
function-fitting functions so I can use complex-valued data?

Ned Lieb

unread,
Apr 8, 2009, 2:43:58 AM4/8/09
to
Just a clarification of my original question: I'm referring to a specific
issue I'm having using the NonlinearModelFit function, which fits lists of
data-points to functions of a type specified by the user (for example, say I
knew that my data fit an exponential equation (it doesn't, by the way, this
is just an example) with x as an independent variable: NonLinearModelFit
could find the constant A in the equation A*e^x that would give the function
that most closely approximated my data). My list of data is complex-valued
(with non-zero imaginary components). Mathematica returned an error message
saying I could only use real numbers. I was wondering whether there was a
way to get around this.

Thanks

-----Original Message-----
From: dh [mailto:d...@metrohm.com]
Sent: Monday, April 06, 2009 6:48 AM
To: Ned Lieb
Subject: Re: complex function fitting?

Hi Ned,
Mathematica works by default with complex numbres. E.g. using "Fit":
d = Table[{x + I y, Exp[x + y I ]}, {x, 0, 1, .1}, {y, 0, 1, .1}];
d = Flatten[d, 1];
pol = Fit[d, {1, x, x^2}, x]
yc = pol /. x -> d[[All, 1]];
ListPlot[{Re[#], Im[#]} & /@ (yc - d[[All, 2]]), PlotRange -> All]
Daniel

dh

unread,
Apr 8, 2009, 2:47:30 AM4/8/09
to

Hi Ned,

Mathematica works by default with complex numbres. E.g. using "Fit":

d = Table[{x + I y, Exp[x + y I ]}, {x, 0, 1, .1}, {y, 0, 1, .1}];

d = Flatten[d, 1];

pol = Fit[d, {1, x, x^2}, x]

yc = pol /. x -> d[[All, 1]];

ListPlot[{Re[#], Im[#]} & /@ (yc - d[[All, 2]]), PlotRange -> All]

Daniel

dh

unread,
Apr 8, 2009, 2:47:41 AM4/8/09
to
Hi Ned,
what I said is still valid, by default Mathematica calculates with
complex numbers. You must have made another error. Why do you not give a
simple complete example? I can not help you without knowing the problem.
Here is my working example using NonlinearModelFit:

d = Table[{x + I y, Exp[x + y I]}, {x, 0, 1, .1}, {y, 0, 1, .1}];
d = Flatten[d, 1];
pol = NonlinearModelFit[d, {c0 + c1 x + c2 x^2}, {c0, c1, c2}, x]
yc = pol[x] /. x -> d[[All, 1]];

ListPlot[{Re[#], Im[#]} & /@ (yc - d[[All, 2]]), PlotRange -> All]
Daniel

Ned Lieb wrote:
> Just a clarification of my original question: I'm referring to a specific
> issue I'm having using the NonlinearModelFit function, which fits lists of
> data-points to functions of a type specified by the user (for example, say I
> knew that my data fit an exponential equation (it doesn't, by the way, this
> is just an example) with x as an independent variable: NonLinearModelFit
> could find the constant A in the equation A*e^x that would give the function
> that most closely approximated my data). My list of data is complex-valued
> (with non-zero imaginary components). Mathematica returned an error message
> saying I could only use real numbers. I was wondering whether there was a
> way to get around this.
>
> Thanks
>
> -----Original Message-----
> From: dh [mailto:d...@metrohm.com]
> Sent: Monday, April 06, 2009 6:48 AM
> To: Ned Lieb
> Subject: Re: complex function fitting?
>

> Hi Ned,
> Mathematica works by default with complex numbres. E.g. using "Fit":
> d = Table[{x + I y, Exp[x + y I ]}, {x, 0, 1, .1}, {y, 0, 1, .1}];
> d = Flatten[d, 1];
> pol = Fit[d, {1, x, x^2}, x]
> yc = pol /. x -> d[[All, 1]];
> ListPlot[{Re[#], Im[#]} & /@ (yc - d[[All, 2]]), PlotRange -> All]
> Daniel
>
> Ned Lieb wrote:

>> Does anyone know how I could extend the domain of Mathematica's
>> function-fitting functions so I can use complex-valued data?
>>
>>
>>
>
>
>
>
>


--

Daniel Huber
Metrohm Ltd.
Oberdorfstr. 68
CH-9100 Herisau
Tel. +41 71 353 8585, Fax +41 71 353 8907
E-Mail:<mailto:d...@metrohm.com>
Internet:<http://www.metrohm.com>


Sjoerd C. de Vries

unread,
Apr 8, 2009, 2:49:50 AM4/8/09
to
Ned,

It looks like Mathematica already can do this:

In[673]:=
data = Table[{y = RandomComplex[],
RandomReal[{0.9, 1.1}] y^2 + RandomReal[{0.9, 1.1}] y + 1 +
I}, {20}];

In[672]:= Fit[data, {1, x, x^2}, x]

Out[672]= (0.988738 +
0.984148 I) + (1.0509 - 0.0357287 I) x + (1.0156 + 0.0497595 I) x^2

Cheers -- Sjoerd

0 new messages