Fitting of multiple input multiple output function with MATLAB - matlab

I have a function that can be written as following:
[y1, y2] = function(x1,x2,a,b)
And I have some experimental data, is there any function in MATLAB that can help me in the fitting procedure? The function is non-linear and it is defined in an external file (matlab function). I have some experimental data that, given x1 and x2, provides the outputs y1 and y2. I only have to determine the parameters "a" and "b".
I've tried using the curve fitting app, but it works only for single-output function. Same holds for the "fit" MATLAB function.

Related

lsqcurve fit function of two variables

I have a function y that is function of two variables, so y=y(x1,x2). This function has 5 parameters that I want to estimate. I have data for y, x1, x2.
How can I do it using lsqcurve fit? I tried but it didn't work

Matlab set range of inputs and step

I have this signal:
x(t) = t*sin(m*pi*t)*heaviside(-m*pi-t)+t*cos(k*pi*t)*heaviside(t-k*pi)+sin(k*pi*t)*cos(m*pi*t)*(heaviside(t+m*pi)-heaviside(t-k*pi));
and I want to calculate the values only from -5pi to 5pi with a step of pi/100 using Matlab. How could I do it?
Provided you have defined m and k somewhere, and you have the matlab symbolic toolbox which provides the heaviside function, this is how it is done:
% First we define the function
x = #(t) t*sin(m*pi*t)*heaviside(-m*pi-t)+t*cos(k*pi*t)*heaviside(t-k*pi)+sin(k*pi*t)*cos(m*pi*t)*(heaviside(t+m*pi)-heaviside(t-k*pi));
% Then we define the values for which we want to compute the function
t_values = -5*pi:pi/100:5*pi;
% Finally we evaluate the function
x_values = x(t_values)
Details
First line we define your function as an anonymous function which is a handy tool in matlab.
Then we create a vector of values from -5pi to 5*pi with steps of pi/100. For this we use the matlab colon syntax. It makes it short and efficient.
Finally we evaluate the function on each of the t_values by passing the vector to the anonymous function.
Note: If you don't have the symbolic toolbox, you could easily implement heaviside yourself.

How to Fit a decay exponential function in Matlab

I have to fit the dots, results of measurements, by an exponential function on Matlab. My profesor asked me to use only
fminsearch
polyval
polyfit
One of them or both. I have to find the parameters a and b (the value) which are fitting it.
There is the lines I wrote :
x=[1:10:70]
y=[0:10:70]
x=[12.5,11.8,10.8,10.9,6.5,6.2,6.1,5.423,4.625]
y=[0,0.61,1.3,1.4,14.9,18.5,20.1,29.7,58.2]
xlabel('Conductivité')
ylabel('Inductance')
The function has the form a*e^(-b*x) +c
Well polyfit and polyval are only usefull for working with polynomials. So you would have to write a minimization problem of the form min(f(x)).
functionToMinimize = #(pars, x, y)(norm(pars(1).*exp(-pars(2).*x) - y));
targetFunctionForFminseardch = #(pars)(functionToMinimize(pars, x, y));
minPars = fminsearch(targetFunctionForFminseardch, [0, 1])
Read up on anonymous functions and the use of vector norms if you have questions how to construct such a minimization problem.
Your code also has some flaws. Why are you defining x and y twice when you only want to use the actual measured data?

Levenberg-Marquardt algorithm for 3D data

I have a point cloud in 3D whose coordinates are stored in a 3D vector, and I would like to fit a nonlinear function to the point cloud.
Do you know if the lsqcurvefit algorithm implemented in MATLAB works for 3D data as well?
Do you have any example data that uses 'levenberg-marquardt' for 3D data using MATLAB?
options = optimoptions('lsqcurvefit','Algorithm','levenberg-marquardt');
Yes, you can still use lsqcurvefit in 3D, but if you want to keep your code as simple as possible (see edit) I suggest the lsqnonlin function for multivariate nonlinear data fitting. The linked documentation page shows several examples, one of which uses the Levenberg-Marquardt algorithm in 2D. In 3D or higher, the usage is similar.
For instance, suppose you have a cloud of points in 3D whose coordinates are stored in the arrays x, y and z. Suppose you are looking for a fitting surface (no longer a curve because you are in 3D) of the form z = exp(r1*x + r2*y), where r1 and r2 are coefficients to be found. You start by defining the following inline function
fun = #(r) exp(r(1)*x + r(2)*y) - z;
where r is an unknown 1x2 array whose entries will be your unknown coefficients (r1 and r2). We are ready to solve the problem:
r0 = [1,1]; % Initial guess for r
options = optimoptions(#lsqnonlin, 'Algorithm', 'levenberg-marquardt');
lsqnonlin(fun, r0, [], [], options)
You will get the output on the command window.
Tested on MATLAB 2018a.
Hope that helps.
EDIT: lsqcurvefit vs lsqnonlin
lsqcurvefit is basically a special case of lsqnonlin. The discussion on whichever is better in terms of speed and accuracy is wide and is beyond the scope of this post. There are two reasons why I have suggested lsqnonlin:
You are free to take x, y, z as matrices instead of column vectors, just make sure that the dimensions match. In fact, if you use lsqcurvefit, your fun must have an additional argument xdata defined as [x, y] where x and y are taken in column form.
You are free to choose your fitting function fun to be implicit, that is of the form f(x,y,z)=0.

Using cv.remap (mexopencv) instead of interp2 (MATLAB)

I am experimenting with the mexopencv project that allows using the
OpenCV library from MATLAB .m files in order to compare the performance
of the native MATLAB functions with the OpenCV functions
I would like to substitute a call to the MATLAB interp2 function:
Vq = interp2(X,Y,V,Xq,Yq) returns interpolated values of a function of two variables at specific query points using linear interpolation. The results always pass through the original sampling of the function. X and Y contain the coordinates of the sample points. V contains the corresponding function values at each sample point. Xq and Yq contain the coordinates of the query points.
The substitute shall be a call to the cv.remap function.
Applies a generic geometrical transformation to an image
dst = cv.remap(src, map1, map2)
dst = cv.remap(src, map1)
dst = cv.remap(..., 'OptionName',optionValue, ...)
The three SO questions Similar OpenCV Api for interp2 in Matlab,
How to do grid interpolation interp2 in OpenCV
and cv::remap (in opencv) and interp2 (matlab)
state that the OpenCV function remap can be used instead of the native MATLAB
function interp2, but I have no idea how-to correctly interpret/transform the
arguments (I have no experience regarding MATLAB and computer vision).
How can I use the mexopencv function cv.remap to get the same effect as if one
would call Vq = interp2(X,Y,V,Xq,Yq)?