How can I fit a function that takes a range of x as input instead of just one value?

4 Ansichten (letzte 30 Tage)
I have a set of measured data and a function that can be used to simulate that data (found online on a publication). The problem is that the function takes a range of x such as 40:0.01:50 as input, as well as some other parameters. I tried to use the fit function, but since it evaluates the function at each x, it doesn't work since I get the error of "Not enough input arguments". I wanted to use the fit function as it is simple to introduce ranges for the fitting parameters that I want to use with lower and upper. Is there any other way to do this or a solution?
  4 Kommentare
Torsten
Torsten am 6 Jul. 2022
We cannot give advice with the information given.
The usual fit functions use one input for x and parameters to produce one output y. That's what all optimization routines of MATLAB are based on.
Why does the function need a range of x-values as input to produce one (?) output y ?
David
David am 6 Jul. 2022
It doesn't need a range of x to produce only one y, if you input a vector x, the output y is the same length.
https://doi.org/10.1107/S1600576716018379 This is the link to the publication which includes the code in the supplementary material. I certainly lack in my Matlab knowledge, and I cannot completely understand why it needs a range of x instead of using just one value. I am also attaching the code I've written (which is incomplete) and uses the functions in the publication.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 6 Jul. 2022
Bearbeitet: Image Analyst am 6 Jul. 2022
Just make your code prepared to handle vectors, like
x = 40:0.01:50;
y = MyFun(x)
y = 1×1001
1.0e+03 * 1.6000 1.6008 1.6016 1.6024 1.6032 1.6040 1.6048 1.6056 1.6064 1.6072 1.6080 1.6088 1.6096 1.6104 1.6112 1.6120 1.6128 1.6136 1.6144 1.6152 1.6160 1.6168 1.6176 1.6185 1.6193 1.6201 1.6209 1.6217 1.6225 1.6233
function y = MyFun(x)
y = x .^ 2;
end
  1 Kommentar
David
David am 6 Jul. 2022
My code takes vectors, the problem is that, from what I'm understanding, the "fit" function does not use the whole x vector I provide at once, and instead evaluates the function at each x independently, which doesn't work fot the code I am using.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Interpolation finden Sie in Help Center und File Exchange

Produkte


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by