How to call a function with vector input in the fit type function?
10 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Matt J
il 1 Giu 2022
Modificato: Matt J
il 1 Giu 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)
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
Risposte (1)
Catalytic
il 31 Mag 2022
Modificato: Catalytic
il 31 Mag 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 Commenti
Catalytic
il 2 Giu 2022
Modificato: Catalytic
il 2 Giu 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.
Vedere anche
Categorie
Scopri di più su Linear and Nonlinear Regression in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!