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
Related
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?
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.
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)
As a homework given by my Robotics subject, I'm supposed to make functions that convert between coordinate systems. Specifically we are supposed to do this:
Function parameters are Xo = mySYSTEM1toSYSTEM2(Xi), where:
Xi is matrix 3 * N.
Every column presents one point, elements of the column are then the coordinates of that point in SYSTEM1.
I know the equations for conversion but I need help with matlab syntax. I was imagining something like this:
%Takes matrix as arguments
% - loops through it and calls mySphericToCarthesian(alpha, beta, ro) for every column
function [Xo] mySphericToCarthesian[Xi]
%Converts one spheric point to another
function [x, y, z] mySphericToCarthesian[alpha, beta, ro]
Matlab doesn't seem to like this.
How can I establish these two function so that I can actually start with the homework itself?
Well, probably the simplest option would be to define two different functions. In Matlab, function overloading shadows all functions but one (i.e. only one function with a given name can be used at a time), which is not very interesting in practice.
However, if you absolutely want only one function with two different behaviors, then it can be achieved by using the nargin and nargout variables. Inside a function, they indicate the number of inputs and outputs specified by the calling script/function. You can also use varargin, which places all the inputs in a cell.
In practice, this gives:
function [x, y, z] = mySphericToCarthesian(varargin)
% Put your function description here.
switch nargin
case 1
Xi = varargin{1};
% Do you computation with matrix Xi
% Define only x, which will be used as the only output
case 3
alpha = varargin{1};
beta = varargin{2};
rho = varargin{3};
% Do you computation with alpha, beta and rho
% Define x, y and z for the outputs
end
I want to use ezplot in MATLAB, and because the function I want to plot consists of a large number of terms I may split it into smaller functions. Let me give an example of a small number of terms and it can be generalized to a large number of terms. To plot the function:
y2+xy+xy3+x+1=0
I let y1=x+1 and I write the following in MATLAB:
x=[0:1:5]
y1=x+1
ezplot('y.^2+x*y+x*y.^3+y1')
But there is an error. Please tell me how can I correct the error. Is it possible to use this feature (splitting the equation or function into a number of terms)?
Your error is caused by trying to replace x+1 with y1. ezplot requires that symbolic expressions are functions of only 2 symbolic variables. However, there are 3 symbolic variables (x, y, and y1) in your call to ezplot:
ezplot('y^2+x*y+x*y^3+y1');
If you use your original equation, everything should work fine:
ezplot('y^2+x*y+x*y^3+x+1');
EDIT: In case you were curious...
If you want to plot an equation with 3 variables, you will first need to solve the equation for one of them and then use the function ezsurf (this is illustrated in this answer I gave to another SO question). Technically, y1 is a dependent variable the way you have defined it (since it depends on the variable x). However, for the sake of the following example, let's assume it's an independent variable. The equation:
y^2 + x*y + x*y^3 + y1 = 0
would be solved for y1 to get the following:
y1 = -y^2 - x*y - x*y^3
and y1 would be plotted in the following way:
ezsurf('-y^2-x*y-x*y^3');