How to call a function with vector input in the fit type function?
조회 수: 7 (최근 30일)
이전 댓글 표시
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
Matt J
2022년 6월 1일
편집: Matt J
2022년 6월 1일
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)
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
답변 (1개)
Catalytic
2022년 5월 31일
편집: Catalytic
2022년 5월 31일
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
Catalytic
2022년 6월 2일
편집: Catalytic
2022년 6월 2일
"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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!