# Problem in Calling a function in the custom equation for curve fitting

1 visualización (últimos 30 días)
Shaily_T el 21 de Mayo de 2022
Comentada: Shaily_T el 1 de Jun. de 2022
I have defined a custom function to use it in curve fitting toolbox. In my defined function, I need to call another function "kkrebook2" which is in the file exchange and its inputs are vectors. But for defining a custom function we have a for loop of our x value and everything is defined in that for loop which means the input for "kkrebook2" will be elements of vector of x. So, it doesn't work when I try to fit because it is getting elements instead of vectors. Does anyone has a suggestion how can I write my custom function such that I can use "kkrebook2" and also use it for curvefitting?
This is related part of my code for the defined custom function for curve fitting and the "kkrebook2" function.
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)));
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
##### 1 comentarioMostrar -1 comentarios más antiguosOcultar -1 comentarios más antiguos
Bala Tripura Bodapati el 26 de Mayo de 2022
Hi Shaily,
It is my understanding that an error is being encountered while fitting data using the custom defined function. Could you attach the code snippet where you are using curve fitting , along with information regarding the inputs that are being passed to the custom function like the dimensions and value?

Iniciar sesión para comentar.

### Respuestas (1)

Bala Tripura Bodapati el 24 de Mayo de 2022
Hi Shaily,
It is my understanding that you would like to call the function "kkrebook2" from custom defined function and the input to the function according to your code is an element of a vector rather than a vector.
The following code will illustrate how to call the function "kkrebook2" from your custom function with vector as input argument,assuming that 'nu' input argument in your custom defined function is a single-dimensional aray:
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)));
gaussEqn1(i) = gaussEqn1(i) + thisGaussian(i);
d(i)= gaussEqn1(i);
end
end
Imag=(-log(d)*c)./(nu();
Real=kkrebook2(nu,Imag,0);
f=(-r1+r2*exp(-d).*exp(-1i*Real));
g=(1-r1*r2*exp(-(d)).*exp(-1i*Real));
p= abs(f./g);
end
##### 3 comentariosMostrar 1 comentario más antiguoOcultar 1 comentario más antiguo
Bala Tripura Bodapati el 1 de Jun. de 2022
Editada: Bala Tripura Bodapati el 1 de Jun. de 2022
Hi Shaily,
From the code you attached, it is my understanding that the following error is encountered when running the 'fitcode.m' file:
NaN computed by model function, fitting cannot continue.
After debugging, I found that there is a value returned when the 'kkrebook2' function is called inside the 'SRC.m' file, which when used in further calculations is returning 'Inf/NaN' value.So there is no error in calling the 'kkrebook2' function, but the error occurs due to the data and the calculations on the data returned by the 'kkrebook2' function.
Please note that the 'kkrebook2.m' from 'Tools for Data Analysis in Optics, Acoustics, Signal Processing' is a third-party submission authored by Valerio Lucarini . I recommend you to contact the contributing author regarding any questions, issues, or complaints related to the submission.
Shaily_T el 1 de Jun. de 2022
Thank you!

Iniciar sesión para comentar.

### Categorías

Más información sobre Linear Prediction en Help Center y File Exchange.

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by