How to export fitted curve to 1D vector - matlab

With the use of Curve Fitting toolbox I'm fitting to 11 data points a curve described by a custom equation. As a result I get something like this:
I want to save 1D vector represented bye the red line on the plot above into a matlab variable. I try to use Fit->Save to Workspace... option from the Curve Fitting toolbox menu, but saved variables do not contain any of the fitted data.
How can I save fitted data into matlab variable?

The saved MATLAB-object (default name is fittedmodel) contains the fitted function as a function-handle and the fitted coefficients corresponding to this function-handle. You can then evaluate at the data points of your choice with feval.
In the following example the fitted function will be evaluated at the original datapoints x:
y = feval(fittedmodel, x);
Now you can directly plot the result:
plot(x,y);

Related

Fitting gaussian curve to histogram in MATLAB

I have a data set in excel so I passed it to MATLAB to draw a histogram and append gaussian fitting. My code is below.
vData = xlsread("2.xlsx");
figure(1);
hHist = histogram(vData, -2.7:0.001:-2.4);
As I run my code, I get a histogram like this
histogram of my data
To gaussian fit my histogram, I add some code like this
figure(2);
histfit(vData); % I'm not sure this is the right fitting method
But the result I got is like this
fitting on histogram
I guess the bin size and bin edge is not appropriate for my data because my data is usually clustered around -2.5.
hisfit method doesn't have bin size or bin edge parameter so I think I can't use this method.
I'm wondering how I can get an appropriate gaussian fitting to my histogram. Thank you for you help.

How to extract residuals from curvefit

I'm using curve fit in Matlab R2016a to find the best fit between two arrays. One array represents a certain value at a given latitude and longitude and the other array represents the date that value was collected.
In using the curve fit tool I'm able to find a line of best fit as well as to plot the residuals. The residuals are all I care about-- however, when I export the residuals to the workspace they are represented as one column of numbers. This isn't helpful to me without the identifying information of that residual's relationship to the original arrays (i.e., which X,Y pair does each residual correspond to?)
The data from the residuals graph in the curve fit tool is exactly what I want. Is there a way to export this in a manner that makes it usable?
The cftool uses fit at its heart. What you can do to further explore the fit and its residuals is export the fit to your workspace. Do this through the 'Fit' menu at the top of the Curve Fitting Tool window, then select 'Save to Workspace'. Using this fit object (a cfit for a curve or an sfit for a surface), you can do the same analyses and more as with the curve fitting tool.
Let me illustrate how to obtain a fit, create a plot of the residuals and how to calculate the residuals. The resulting image is shown below. In the code, the residuals variable contains the residuals of the fit with each element belonging to each sample pair in x and y.
% Generate data
rng default
x = sort(rand(10, 1));
y = randn(size(x)) - 3*x;
% Fit a line
fitted = fit(x, y, fittype('poly1'));
% Plot fitted line with data
figure
subplot 311
plot(fitted, x, y)
% Plot residuals
subplot 312
plot(fitted, x, y, 'residuals)')
ylabel residuals
% Get residuals
residuals = y - fitted(x);
% Create stem plot of residuals
subplot 313
stem(x, residuals)
legend residuals
xlabel x
ylabel residuals

How to extract fitted data from normal probability density function

If I fit a uni-variate data with normal distribution, how can i get back the fitted values in MATLAB.
I am using this simple example
load hospital % data
x = hospital.Weight;
[mu sigma]=normfit(x) %normal fitting
%To visualize the pdf
xval=min(x):0.1:max(x)
yval=normpdf(xval,mu,sigma)
plot(xval,yval)
yval is giving the probabilities of xval values. Now, If I would like to extract the fitted values of 'x' after approximating it with the above normal distribution, how do I do that?. As can be seen in the picture the y-axis values are the pdf and lies between 0 and 1, however I want the corresponding fitted values from the data that follows normal distribution.
Would the fitted values be x_fitted = yval*sigma + mu? !I think I am missing some basic maths here.
normfit simply gives you the mu and sigma of the fitted normal pdf. From those you build that pdf with normpdf. So the desired y values for your input x would be
y = normpdf(x,mu,sigma)
which you could plot with
hold on
plot(x,y,'ro')
Note that, with this procedure, the data lie exactly on the normal pdf, even if those data do not actually follow a normal distribution.

Extract data from curve fit toolbox

I am using matlab and I have a certain set of x and y data,
x=[0,1.25,1.88,2.5,5,6.25,6.88,7.19,7.5,10,12.5,15,20];
y=[-85.93,-78.82,-56.95,-34.56,-33.57,-39.64,-41.96,-49.28,-66.6,-66.61,-59.16,-48.78,-41.53];
I want to use the curve fitting toolbox which has the spline function to generate a graph, so i did this,
cftool
It would bring me to the toolbox which i can then choose the spline fit. I was thinking if its possible that i extract the data points from the spline graph generated. Which means i probably would have more x and y data points than those that i input in, since the spline graph is sort of a continuous graph. Anyone could give me some advice on this? Thanks!
You can perform the equivalent of the spline fit performed with cftool with fit, see for instance here, here, or here:
% perform spline fit without cftool
ft = fittype('cubicspline');
coeff=fit(x,y,ft);
% use plot to display the interpolating polynomial
% (relying on internal plot settings)
figure
h=plot(coeff,x,y,'o');
% extract the *interpolated* curve from the figure
xi=get(h,'XData');
yi=get(h,'YData');
Replot it just to show that we can:
But if you just want to interpolate do as Fraukje explained here. Define a finer grid on x and use the interp1 function, as in the following example (same x,y input data as before):
% interpolate
Ni = 100;
xi = linspace(x(1),x(end),Ni);
yi = interp1(x,y,xi,'spline');
Now xi,yi is the interpolated data:

How to plot a 3D figure in matlab based on a function like f(x,y,z)=0?

How to plot a 3D figure in MATLAB based on a function like f(x,y,z)=0?
And this complicated function can not be written as z = f(x,y).
f(x,y,z)=sum(a.*exp(sv(:,1)-x).^2+sv(:,2)-y).^2+sv(:,3)-z).^2)-b=0
where a is a known vector, sv is a known matrix, b is a known value. x,y,z are three variables. How to draw this surface in 3D way in matlab?
I just solve this question by this tool from the Matlab File Exchange:
Ezimplot3: implicit 3D functions plotter
your function only contains 1D vectors( I am assuming they are of equal lengths), if summed it will give you a constant; therefore, there is really nothing to plot.