Problem in defining the fit type function for curve fitting tool

I have defined a custom function for use in curve fitting toolbox. In my defined function, I need to call another function "kkrebook2" and its inputs are vectors. But for defining a fit type function we have a for loop of our "nu" value and everything is defined in that for loop which means the input for "kkrebook2" will be elements of vector of "nu". So, it doesn't work when I try to fit because it is getting elements instead of vectors.
But I do want to pass a vector of "nu" to "kkrebook2" in my fit type function. Could you please let me know how can I write my fit type function such that I can call "kkrebook2" in my fit type function properly?
This is related part of my code for the defined fit type function for curve fitting and the "kkrebook2" function.
Thank you in advance!
function p = SCR(nu,numGaussians,a,sigma)
for i = 1:length(nu)
for k = 1 : numGaussians
if k==1
b = Start;
else
b = Start+(24E6 * (k-1));
end
thisGaussian(i) = a.*exp(-((nu(i)-b).^2)/(2.*(sigma.^2)));
% Add into accumulator array:
gaussEqn1(i) = gaussEqn1(i) + thisGaussian(i);
d(i)= gaussEqn1(i);
end
Imag(i)=(-log(d(i))*c)./(nu(i));
Real(i)=kkrebook2(nu(i),Imag(i),0);
f(i)=(-r1+r2*exp(-(d(i)).*exp(-1i*Real(i)));
g(i)=(1-r1*r2*exp(-(d(i))).*exp(-1i*Real(i)));
p(i)= abs(f(i)./g(i));
end
end

2 Kommentare

There is no variable called 'x' in the code that you've shown.
Shaily_T
Shaily_T am 24 Mai 2022
Bearbeitet: Shaily_T am 24 Mai 2022
Yes, you are correct. Thanks for capturing that! By "x" I mean "nu" in my code. I edditted my question.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Matt J
Matt J am 24 Mai 2022
Bearbeitet: Matt J am 24 Mai 2022
fittype( @(nu,numGaussians,a,sigma) SCR(nu,numGaussians,a,sigma),'independent','nu')

10 Kommentare

Thanks for your answer. It is not clear for me where should I add this code. I added it into my SCR function but it gives an error when I run my fit code. Also, "nu" is an independent variable. "p" is a dependent parameter and others are coefficients. This is the fit code where I call "SCR" function as a fit type for my fitting problem.
clear
load('42nsC.mat')
[freq,RF] = prepareCurveData(freq,RF);
nu = freq;
p = RF;
myoptions = fitoptions('Method','NonlinearLeastSquares','MaxFunEvals',5000,'MaxIter',1000,'StartPoint',[0.3, 3.4E6],'Lower', [0 1E6],'Upper',[1.6 8E6]);
ft = fittype('SCR(nu,numGaussians,a,sigma)','independent','nu', 'dependent', 'p','coefficients',{'a','sigma'},'problem','numGaussians','options',myoptions);
coeffs = coeffnames(ft)
fo = fitoptions(ft)
[myfittype,gomyfit] = fit( nu, p, ft,'problem',9)
plot(myfittype, nu, p)
Without 42nsC.mat, I couldn't test this, but I think it should work.
fo = fitoptions('Method','NonlinearLeastSquares','MaxFunEvals',5000,'MaxIter',1000,...
'StartPoint',[0.3, 3.4E6],'Lower', [0 1E6],'Upper',[1.6 8E6]);
ft = fittype('SCR(nu,numGaussians,a,sigma)','independent','nu', 'dependent', 'p',...
'coefficients',{'a','sigma'},'problem','numGaussians','options',fo);
[myfittype,gomyfit] = fit( nu, p, ft, fo)
plot(myfittype, nu, p)
Shaily_T
Shaily_T am 24 Mai 2022
Bearbeitet: Shaily_T am 24 Mai 2022
Thanks for your answer but when I add it to my fit code I get the following error.
And this is where I add.
I am not sure but I think what should be changed is something in my fit type custom function when I call "kkrebook2" rather than the fit code.
I am not sure but I think what should be changed is something in my fit type custom function when I call "kkrebook2" rather than the fit code.
No, the error messages you've posted don't even mention your custom function.
It's saying that, if you're going to use an anonymous function, the argument order must have the coefficients first,
@(a,sigma,numGaussians,nu) SCR(nu,numGaussians,a,sigma)
See also,
Thanks. I just try that. It still says the same error.
I don't get any error messages when I run the code.
Thanks. "I don't get any error messages when I run the code."
Do you mean for the files I have attached? Have you run "Sfit"?
Because I have changed the "SCR" function in that files.
Yes, running the Sfit that you have attached throws no error messages.
Thanks for checking out. This is what I get from the "Sfit" code that I have. The red figure is the result of the fit and the yellow one is just the plot of the fit result. They are not the same! Which is strange! It runs but it seems it is not working properly. I have attached the figure that I have from running "Sfit" (red one) and then putting the obtained fitting parameters in the function (the yellow one). Thanks in advance!
Matt J
Matt J am 26 Mai 2022
Bearbeitet: Matt J am 26 Mai 2022
Here is what I see.
Whether or not this is what you expect is a separate issue from your posted question. Your post is asking how to make fit() invoke your model function and receiving ouput of the proper size. It now appears to be doing that, since no error messages are thrown.
As for why you are not seeing fit results that you expect, there are a number of possible reasons. Your start point could be bad. Or, your model and data may be inconsistent with one another.
Yes, that is correct. It is a different issue than what I posted. I posted the question then I was able to make some changes to the fit type function and I see it is working like this which doesn't make sense as the main problem is if I put the obtained fitting parameters in the fit type function I don't see the same result as the fitted curve. I will post another question for it.
Thanks for your time and consideration!

Melden Sie sich an, um zu kommentieren.

Kategorien

Gefragt:

am 24 Mai 2022

Kommentiert:

am 26 Mai 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by