Matlab fit with function returning vector with all the y(x_i) values - matlab

I am working on something and I haven't found any solution, maybe I didn't know how to correctly search for it...
I have two arrays of experimental data (x and y). x is a list of certain energies (512 values from 0 to 100 kev) and I want to fit them to a function which returns a vector of values of y for every x in the list (the energies are always the same, 512 certain values). This is because my function model contains several matrix and other functions.
So, I can't evaluate my function as f(x,a,b,c...) (with a,b,c the parameters to fit) and expect a single scalar, but I have to evaluate f(a,b,c...), and it returns a vector of y(x1),y(x2)...
Now, I want to fit my data to my model. But lsqcurvefit needs a function of the form f(x), I suppose that it evaluates every f(x). I could write my function so that every time it is called it evaluates the vector result, and then returns y for the given x, but it would be quite inefficient... And I'm sure there must be another way.
Any idea?

Maybe you can do an fminsearch on the sum of square errors? It is best to put all fitting parameters into one vector. Here I call it p.
f = #(x,p) (p(3)+p(1)*x.^p(2)).^(1/p(4); %example function with four free parameters
sqerr = #(x,y,p) sum((y-f(x,p)).^2); %sum of squared errors
p = [1,1,1,1]; %four starting conditions
p = fminsearch(#(p) sqerr(x,y,p),p); %fit
Then you can find your y(x_i) values by calling the function with the fitted paramters
f(x,p)

Related

Create a function handle within a structure, given some coefficients

I have a vector of coefficients available, which are the coefficients of an interpolating polynomial. I want to create a field in a structure as follows:
p.val =#(y) polynomial
Here polynomial is the polynomial with my coefficients in the indeterminate y. I'm not sure how to do this. The field has to contain a function handle like this.
The user has to be able to call this as follows:
pval = p.val(y)
where, y can be a vector of any length, supplied by the user (which contains values where the polynomial needs to be evaluated). So, this output has to be a column vector, such that every cell contains the specific function value.
Any help on how this can be done is appreciated!
I am not sure what function you want to implement, but the length of the vector you are including does not matter.
example.a = 3; % Any variable
example.b = #(s) mean(s);
The function mean here can be anything from a built-in function to a custom function you just made.
You can call it by:
result = example.b(rand(1000,1);

How can I use NxM matrix to be my initial condition in `pdepe`

I solved a PDE using Matlab solver, pdepe. The initial condition is the solution of an ODE, which I solved in a different m.file. Now, I have the ODE solution in a matrix form of size NxM. How I can use that to be my IC in pdepe? Is that even possible? When I use for loop, pdepe takes only the last iteration to be the initial condition. Any help is appreciated.
Per the pdepe documentation, the initial condition function for the solver has the syntax:
u = icFun(x);
where the initial value of the PDE at a specified value of x is returned in the column vector u.
So the only time an initial condition will be a N x M matrix is when the PDE is a system of N unknowns with M spatial mesh points.
Therefore, an N x M matrix could be used to populate the initial condition, but there would need to be some mapping that associates a given column with a specific value of x. For instance, in the main function that calls pdepe, there could be
% icData is the NxM matrix of data
% xMesh is an 1xM row vector that has the spatial value for each column of icData
icFun = #(x) icData(:,x==xMesh);
The only shortcoming of this approach is that the mesh of the initial condition, and therefore the pdepe solution, is constrained by the initial data. This can be overcome by using an interpolation scheme like:
% icData is the NxM matrix of data
% xMesh is an 1xM row vector that has the spatial value for each column of icData
icFun = #(x) interp1(xMesh,icData',x,'pchip')';
where the transposes are present to conform to the interpretation of the data by interp1.
it is easier for u to use 'method of line' style to define different conditions on each mesh rather than using pdepe
MOL is also more flexible to use in different situation like 3D problem
just saying :))
My experience is that the function defining the initial conditions must return a column vector, i.e. Nx1 matrix if you have N equations. Even if your xmesh is an array of M numbers, the matrix corresponding to the initial condition is still Nx1. You can still return a spatially varying initial condition, and my solution was the following.
I defined an anonymous function, pdeic, which was passed as an argument to pdepe:
pdeic=#(x) pdeic2(x,p1,p2,p3);
And I also defined pdeic2, which always returns a 3x1 column vector, but depending on x, the value is different:
function u0=pdeic2(x,extrap1,extrap2,extrap3)
if x==extrap3
u0=[extrap1;0;extrap2];
else
u0=[extrap1;0;0];
end
So going back to your original question, my guess would be that you have to pass the solution of your ODE to what is named 'pdeic2' in my example, and depending on X, return a column vector.

Find minimum of following function in Matlab

I am trying to minimize sum((A-r*B).^2) in Matlab where A and B are matrices and r is the scalar I am manipulating. I tried the following code:
f = #(r) sum((A-r*B).^2);
Answer = fminbnd(f,lowrange,highrange);
But I get an error.
If A and B are matrices, then sum((A - r*B).^2) will not give you a single value. This will give you an array of values. If the input to sum is a matrix, the output of sum will give you an array where each element is the sum along the rows for each column in your matrix.
The function you are specifying to fminbnd must evaluate to a single value. I'm assuming you want to determine the sum of squared differences in your function, and so you need to wrap your sum with another sum. As such, try this instead:
f = #(r) sum(sum((A-r*B).^2));
Answer = fminbnd(f,lowrange,highrange);
The function f will now find the difference between matrices A and B which is weighted by r, square these differences and then add up all of these differences together.
Try that and see if it works.
If you don't need to impose limits on the possible values of the optimized scalar r, then you should be able to solve this equation directly without searching for the minimum. If A and B are vectors, use:
ropt=(B'*B)^(-1)*B'*A;
If A and B are arrays and you want to minimize the sum of squared residuals of all elements of the arrays then I believe the following will work (same formula, but convert A and B to vectors).
ropt=(B(:)'*B(:))^(-1)*B(:)'*A(:);
Examples:
b=[1;2;3]
a=2*b;
ropt=(b'*b)^(-1)*b'*a
returns ropt=2.0000, as desired
Similarly, for a matrix:
b=magic(3);
a=2*b;
ropt=(b(:)'*b(:))^(-1)*b(:)'*a(:)
also returns ropt=2.0000. This should work fine even if there isn't a perfect solution, as there is in the examples above.

Using a Function handle in Matlab

I would like to use knnimpute to fill some missing values in my dataset. Thing is, I would like to use my own distance function, instead of the typical ones (Euclidean, Manhattan...).
For what I've read, knnimpute allows me to use a function handle, that calculates the distance according to Heterogeneous Euclidean-Overlap Metric (HEOM)
I've implemented this function as a regular function, but not as a handle function. So, I cannot use the distance matrix from my "normal" function, because this has to be done inside knnimpute, somehow, as a handler...
I'm confuse, can someone help me understand what I need to do?
As long as your implementation of a distance function has the same signature as the standard distance functions, then you should be able to easily pass your function in.
From the knnimpute documentation (matlab knnimpute) it states that you can pass "A handle to a distance function, specified using #, for example, #distfun." It then refers the reader to the pdist function which provides more details (matlab pdist) about the custom distance function:
A distance function specified using #:
D = pdist(X,#distfun)
A distance function must be of form
d2 = distfun(XI,XJ)
taking as arguments a 1-by-n vector XI, corresponding to a single row of X, and an m2-by-n matrix XJ, corresponding to multiple rows of X. distfun must accept a matrix XJ with an arbitrary number of rows. distfun must return an m2-by-1 vector of distances d2, whose kth element is the distance between XI and XJ(k,:).
So as long as your distance function, as defined in your *.m file matches this signature and so can support these inputs, then there shouldn't be any problems.
Suppose that your distance function is in the mydistFunc.m file, and it's signature matches the above requirements, then all you should need to do is:
% call knnimpute with the data and your function
knnimpute(inputData,'Distance',#mydistFunc);

How can I integrate a function which needs to have a matrix calculation first?

I am doing my dissertation now. I stuck with a integral. My function is defined as
myfun =(exp(t*Q)*V*x)(j);
where Q and V are a matrix (n*n), x is a vector which elements are 1, then after calculation we get the j_th element of that vector then I need to integrate the function against t.
I want to use the quad in the matlab. However the point is that it will report the inner matrix is not the same size. Since A here is not a number ?....
How can I do this. Otherwise I could only write a loop against t itself, which is extremely slow.
Thanks
You can use SUBSREF for this (you still neet to loop over all j's, though):
myfunOfT = #(t)(subsref(exp(t*Q)*V*x,struct('type','()','subs',j);
This returns the value of the jth element of the array at time t.