Question

When using the "basic fitting" tool, one has the opportunity once the "fitting" done to evaluate/estimate a value at certain points. I was only able to reproduce this until the plotting part. I cannot figure out how to reproduce the "Evaluate" function programmatically so that I can estimate the value of certain points and use them in my code. The only way I can achieve this for now, is through the GUI i.e from the Figure window's main menu: "tools >> basic fitting"

screenshot

I am not sure I am making myself clear enough, but please do not hesitate to ask if you need more information.

Was it helpful?

Solution

The answer to your question depends on the specific type of model you are fitting. It is not clear from your question if you are just interested in polynomial fitting or something more complicated. For polynomials you can use the polyfit function to get the coefficients and the polyval function to evaluate at certain points.

%construct a test signal
x = linspace(0,1,100)';
signal = 5*x.^2 + x + 0.5;
noise = 0.1*rand(100,1);
y = signal + noise;

%Plot function
plot(x,[signal,y]);

%Polynomial fitting
n = 2; % order of polynomial
coeff = polyfit(x,y,n) % I get 5.0295, 0.9786, 0.5512

%Evaluate at a certain set of points
x1 = 2.3;
polyval(coeff,x1)

If you are fitting a more complicated model, then you will have to use cfit to do the fitting which will give you a fit object. You have to pass that fit object to the function feval to evaluate the function at a specific point. Check out the documentation for these functions to learn more.

OTHER TIPS

Further to Karthik's answer, if you want to continue using the interactive fitting but simply want to evaluate things programmatically later, you should press 'Save to workspace...' on the left of your screenshot to save the fit information to the workspace. By default, this will create a variable named fit which is a MATLAB struct with a field coeff containing the polynomial coefficients. You can then use polyval to evaluate the polynomial, like so:

polyval(fit.coeff, 5); % get the value at 5
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top