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

Fitting to a custom function

442 views
Skip to first unread message

Jonathan

unread,
Feb 25, 2010, 11:06:05 AM2/25/10
to
Hi there,

New Matlab user here. Trying to define a function that I want to try and fit my data to and I'm struggling to get it to work. Here's what I have:

I have two 801x1 array, "Freq" and "data", the x and y data respectively. I want to try and fit to the equation:

Y = K1 * 1 / (1 - K2*j*omega*tau), where omega = 2*pi*Freq, and tau is a constant I define explicitly in the code. K1 and K2 are coefficients that I want Matlab to compute. j = sqrt(-1).

I have tried to define the fitting function using 'fittype' as follows:
g = fittype('(K1/(1 - (1i*2*pi*K2*Freq*tau)))',...
'independent',{'Freq'},...
'coefficients',{'K1','K2'});

FittedData = fit(Freq,data,g);

But I get an error message saying:
"??? Error using ==> fittype.fittype>fittype.fittype at 477
Expression (C1/(1 - (1j*2*pi*C2*Freq*tau))) is not a valid MATLAB
expression,
has non-scalar coefficients, or cannot be evaluated:
Error in fittype expression ==> (C1./(1 - (1j.*2.*pi.*C2.*Freq.*tau)))
??? Undefined function or variable 'tau'.

Error in ==> Bandwidth_single at 101
g = fittype('(C1/(1 - (1j*2*pi*C2*Freq*tau)))',..."

Which is where I'm totally lost. I don't really know what's gone wrong, or how to fix it. Any help would be appreciated.

Jonathan

unread,
Feb 25, 2010, 11:08:05 AM2/25/10
to

Alan Weiss

unread,
Feb 25, 2010, 11:51:07 AM2/25/10
to

I don't know if this is a complete answer, but your variable named 1j is
not a valid MATLAB variable name. Call it j1 or something else.

Alan Weiss
MATLAB mathematical toolbox documentation

Jonathan

unread,
Feb 25, 2010, 12:06:05 PM2/25/10
to
I tried re-naming it as you suggested but I get the same error. If I remove that variable completely I get this errror:

"Not enough inputs to FITTYPE function."

Steven Lord

unread,
Feb 25, 2010, 1:41:42 PM2/25/10
to

"Alan Weiss" <awe...@mathworks.com> wrote in message
news:hm69pr$mf$1...@fred.mathworks.com...

The "1i" in the fittype expression isn't a variable name, but the
recommended way to refer to sqrt(-1). "i" could be sqrt(-1) or it could be
a reference to a preexisting variable named i; "1i" is always 1*sqrt(-1).

Anyway, if you take a look at the message, Jonathan, you can see clearly the
cause of the problem.

>> "??? Error using ==> fittype.fittype>fittype.fittype at 477
>> Expression (C1/(1 - (1j*2*pi*C2*Freq*tau))) is not a valid MATLAB
>> expression,
>> has non-scalar coefficients, or cannot be evaluated:
>> Error in fittype expression ==> (C1./(1 - (1j.*2.*pi.*C2.*Freq.*tau)))
>> ??? Undefined function or variable 'tau'.

Note that the last line says "Undefined function or variable 'tau'." and
that this message occurs while trying to evaluate the fit. So let's look at
your fit expression:

>> g = fittype('(K1/(1 - (1i*2*pi*K2*Freq*tau)))',...

You tell FITTYPE that K1 and K2 are coefficients and that Freq is an
independent, and MATLAB knows what 1i and pi are (since pi is a built-in
function), but it doesn't know what tau is. You defined it in the code
before creating the fittype object, but that doesn't mean the fittype object
knows what tau is. To tell the fittype object and the FIT function what tau
is, you need to specify it as a problem parameter.

g = fittype('(K1/(1 - (1i*2*pi*K2*Freq*tau)))',...
'independent',{'Freq'},...

'coefficients',{'K1','K2'}, ...
'problem', 'tau');

and specify the 'problem' option when you call FIT to tell it to use the
value of tau you defined in your code when performing the fitting.

--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


Adam A

unread,
Feb 26, 2010, 6:27:04 AM2/26/10
to
Hi Jonathan,

You mention that...


> tau is a constant I define explicitly in the code.

Yet Matlab complains that tau is undefined:


> ??? Undefined function or variable 'tau'.

It's worth solving this problem and making sure that tau is defined properly before you try anything else.

Hope this helps,
Adam

0 new messages