From: Jonathan on 25 Feb 2010 11:08 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.
From: Alan Weiss on 25 Feb 2010 11:51 Jonathan wrote: > 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. 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
From: Jonathan on 25 Feb 2010 12:06 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."
From: Steven Lord on 25 Feb 2010 13:41 "Alan Weiss" <aweiss(a)mathworks.com> wrote in message news:hm69pr$mf$1(a)fred.mathworks.com... > Jonathan wrote: >> 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. > > 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. 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 slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
From: Adam A on 26 Feb 2010 06:27 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
|
Pages: 1 Prev: Fitting to a custom function Next: Simulink, compilation warning |