How to call a function with vector input in the fit type function?

12 views (last 30 days)
I am trying to fit an equation to a model and I need to call a function "kkrebook2" in my fit type funtion (For "kkrebook2", the inputs are two vectors and its output is a vector). This is the snippet of my fit type function code where I call "kkrebook2" function. But it doesn't work when I call my fit type function "SCR" in my fit code. Could you please let me know what is the issue of my code?
I think it is related to the way I am calling "kkrebook2" in my fit type function "SCR" but I don't figure out how to fix it. I have attached the "kkrebook2" function as well.
Thank you in advance!
function p = SCR(nu,numGaussians,a,center,sigma)
for j = 1:length(nu)
for i = 1:length(nu)
for k = 1 : numGaussians
if k==1
b = Start;
else
b = Start + (center * (k-1));
end
thisGaussian(i) = a.*exp(-((nu(i)-b).^2)/(2.*(sigma.^2)));
% Add into accumulator array:
gaussEqn1(i) = gaussEqn1(i) + thisGaussian(i);
z(i)= gaussEqn1(i);
end
end
Rn(:) = kkrebook2(nu,z,0);
hi2(j) = (4*pi*n.*nu(j)*L)/c;
hi1(j)=(Rn(j));
f(j)=(-r1+r2*exp(-(z(j))).*exp(-1i*hi1(j)).*exp(-1i*hi2(j)));
g(j)=(1-r1*r2*exp(-(z(j))).*exp(-1i*hi1(j)).*exp(-1i*hi2(j)));
h(j)= f(j)./g(j);
m(j)= abs(h(j));
p(j)= (m(j).^2);
end
end
  7 Comments
Matt J
Matt J on 1 Jun 2022
Edited: Matt J on 1 Jun 2022
It is hard to comment without being sure what code you are actually working with. The code you have posted for us does not run:
nu0=1;
SCR(nu0,3,1,1,1)
Not enough input arguments.

Error in solution>SCR (line 10)
b = Start;
Matt J
Matt J on 1 Jun 2022
Edited: Matt J on 1 Jun 2022
Even when I give your code an input value for the missing "Start" Parameter and fix the missing declaration of gaussEqn1, it still does not produce an output at the given nu0, which I believe is what @Catalytic is talking about in his answer below.
nu0=1;
SCR(nu0,3,1,1,1,0)
Index exceeds the number of array elements. Index must not exceed 1.

Error in kkrebook2 (line 19)
deltaomega=omega(2)-omega(1);

Error in solution>SCR (line 22)
Rn(:) = kkrebook2(nu,z,0);
function p = SCR(nu,numGaussians,a,center,sigma,Start)
[gaussEqn1,thisGaussian]=deal(zeros(1,length(nu)));
for j = 1:length(nu)
for i = 1:length(nu)
for k = 1 : numGaussians
if k==1
b = Start;
else
b = Start + (center * (k-1));
end
thisGaussian(i) = a.*exp(-((nu(i)-b).^2)/(2.*(sigma.^2)));
% Add into accumulator array:
gaussEqn1(i) = gaussEqn1(i) + thisGaussian(i);
z(i)= gaussEqn1(i);
end
end
Rn(:) = kkrebook2(nu,z,0);
hi2(j) = (4*pi*n.*nu(j)*L)/c;
hi1(j)=(Rn(j));
f(j)=(-r1+r2*exp(-(z(j))).*exp(-1i*hi1(j)).*exp(-1i*hi2(j)));
g(j)=(1-r1*r2*exp(-(z(j))).*exp(-1i*hi1(j)).*exp(-1i*hi2(j)));
h(j)= f(j)./g(j);
m(j)= abs(h(j));
p(j)= (m(j).^2);
end
end

Sign in to comment.

Answers (1)

Catalytic
Catalytic on 31 May 2022
Edited: Catalytic on 31 May 2022
SCR has to be a 1D function of the independent variable nu, meaning it has to give valid output when nu is a scalar. That is not the case for your function.
Essentially, you appear to be fitting some N-dimensional surface function , given only a single sample, .
  5 Comments
Shaily_T
Shaily_T on 2 Jun 2022
My "SCR" function inputs are a vector of "nu" which is the independent variable and some scalar parameters like numGaussians, a center, and sigma, and the output of "SCR" is a vector. I think for fitting a function to data we have a vector of the dependent variable and a vector of the independent variable for data. And so the function to be fitted should also get a vector of independent variables and give back a vector of dependent variables. The way I have written it seems to do the same but when I call "SCR" it doesn't work.
Catalytic
Catalytic on 2 Jun 2022
Edited: Catalytic on 2 Jun 2022
"I think for fitting a function to data we have a vector of the dependent variable and a vector of the independent variable for data."
That's true in general, but the Curve Fitting Toolbox doesn't support general N-dimensional fitting. The Curve Fitting Toolbox only supports the fitting of functions with a 1-dimensional or 2-dimensional domain, whereas your function has an N-dimensional domain.
To put it more formally, if your code implements a mapping y=f(x): , then for f() to be considered a 1D curve, each y(i) can depend only on the corresponding x(i). However, in your SCR function each y(i) depends on all of the x(j) simultaneously.
For more general function fitting problems, you could look at lsqnonlin.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!