Size an style of data points in cftool - matlab

Is there any way to control size, color and style of the black data points in the figure below?
Image
Code
%% Fit: 'untitled fit 1'.
[xData, yData, zData] = prepareSurfaceData( x_lim, y_lim, z_lim );
% Set up fittype and options.
ft = 'linearinterp';
% Fit model to data.
[fitresult, gof] = fit( [xData, yData], zData, ft, 'Normalize', 'on' );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, [xData, yData], zData );
legend( h, 'untitled fit 1', 'z_lim vs. x_lim, y_lim', 'Location', 'NorthEast' );
% Label axes
xlabel( 'x_lim' );
ylabel( 'y_lim' );
zlabel( 'z_lim' );
grid on
view( -253.5, 42.0 );

From your code, the handle returned when you plot, h should be of length 2. h(1) has the details for the surface, and h(2) for the points. Therefore, you can use set on various properties of this handle:
set(h(2),'Marker','o');
(Other properties you may want to set: MarkerSize, MarkerEdgeColor, MarkerFaceColor).

Related

Generated code from matlab curve fitting tool doesn't match original curve

I used matlab's CFtool to generate a fit based on a custom function, however, when I generate that code and call it to my main script file the curves are slightly different. My generated code does not cross the y -int, although it does in CFtool. Does any body know what the issue is? I have tried setting different start points and it didn't change anything.
Suggestions
Generated code:
%% Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( I100M, P100M );
% Set up fittype and options.
ft = fittype( 'a*tanh(-b*x/300)+c', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( 'Method', 'NonlinearLeastSquares' );
opts.Display = 'Off';
opts.StartPoint = [0.990128346443291 0.106912965297856 0.94637904214048];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, xData, yData );
CFtool fit
generated code

Getting y values back from spline fit

I used the MATLAB curve fitting tool to do a spline smoothing fit and created a function from it. How can I access the Y fit values so I can output them to a file? Seems I am only seeing the x values, and all of the coefs from fitresult. Here is the matlab code. Thanks!
function [fitresult, gof] = createFit(Freq, AmplNew)
%CREATEFIT(FREQ,AMPLNEW)
% Create a fit.
%
% Data for 'untitled fit 1' fit:
% X Input : Freq
% Y Output: AmplNew
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
%% Fit: 'untitled fit 1'.
[xData, yData] = prepareCurveData( Freq, AmplNew );
% Set up fittype and options.
ft = fittype( 'smoothingspline' );
opts = fitoptions( 'Method', 'SmoothingSpline' );
opts.SmoothingParam = 0.998;
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
Simply use feval:
y = feval(fitresult,x);
or just use
y = fitresult(x);

MATLAB cftool surface plot matrix

I have a irregular set of data points in the form of cartesian coordinates which using the MATLAB cftool can be turned into a surface (see below).
Does anyone know a way to access the matrix of cartesian coordinates that MATLAB generates in order for it to plot this surface?
The code generated for this graph (seen below) provides no access to the any additional interpolated points which must be produced to fit the surface.
%% Fit: 'untitled fit 1'.
[xData, yData, zData] = prepareSurfaceData( x1, y1, z1 );
% Set up fittype and options.
ft = 'linearinterp';
% Fit model to data.
[fitresult, gof] = fit( [xData, yData], zData, ft, 'Normalize', 'on' );
% Plot fit with data.
figure( 'Name', 'untitled fit 1' );
h = plot( fitresult, [xData, yData], zData );
legend( h, 'untitled fit 1', 'z1 vs. x1, y1', 'Location', 'NorthEast' );
% Label axes
xlabel x1
ylabel y1
zlabel z1
grid on
Thanks in advance
As a possible workaround (potentially not very efficient) is to plot the output of the fit (fitresult) and fetch the XData, YData and ZData properties of the plotted surface.
For example, after performing a dummy fit:
hP = plot(fitresult)
yields those properties for hP:
Surface (curvefit.gui.FunctionSurface) with properties:
EdgeColor: [0 0 0]
LineStyle: '-'
FaceColor: 'flat'
FaceLighting: 'flat'
FaceAlpha: 1
XData: [51x49 double]
YData: [51x49 double]
ZData: [51x49 double]
CData: [51x49 double]
So you can retrieve them.
ALTERNATIVE
as an alternative, you could use the code generated by cftool to provide additional output arguments to the function (called createFit or somethinbg like this). Therefore, as you call the function with enough arguments you will obtain those coordinates directly.
Example:
change the header of the generated function like so:
[fitresult, gof,a,b,c] = createFit1(x, y, z)
and then in the function body:
a = xData;
b = yData;
c = zData;
then calling the function in the Command Window, for example, yields the right coordinates in a,b and c.

MATLAB - Plot multiple surface fits in one figure

I have 3 sets of 3D co-ordinates and I have fitted planes to each set. Now, I want to plot all the data points and the 3 planes in one figure.
So far, I have the following function:
function [fitresult, gof] = create_fit(xx, yy, zz, grp)
[xData, yData, zData] = prepareSurfaceData( xx, yy, zz );
ft = fittype( 'poly11' );
opts = fitoptions( ft );
opts.Lower = [-Inf -Inf -Inf];
opts.Upper = [Inf Inf Inf];
hold on;
% figure( 'Name', 'fit1' );
[fitresult, gof] = fit( [xData, yData], zData, ft, opts );
h = plot( fitresult, [xData, yData], zData);
if grp == 1
colormap(hot);
elseif grp == 2
colormap(cool);
else
colormap(grey);
end
legend( h, 'fit1', 'zz vs. xx, yy', 'Location', 'NorthEast' );
xlabel( 'xx' );
ylabel( 'yy' );
zlabel( 'zz' );
grid on
However, there are 2 problems with this:
The planes, when plotted individually are scaled according to the data points which are also plotted with them. When the planes are plotted together, they scale badly and the data points are converge to a small blob (very small scale compared to the planes)
I tried fixing the problem with axis([-0.04 0.04 -0.04 0.04 -0.04 0.04 -1 1]);, but it is hard coded and still looks a little off.
The colormap command seems to work only when called for the first time. Hence, all the planes turn out to be blue. How can I color each plane and the points fitted for that plane differently?
Is this the best way to plot multiple planes?
Edit
Here is an edited version of my initial answer. The output of plot is a two-element graphical object, so you have to call separately h(1) and h(2) to set the properties of the plane and of the data points.
Here is the code for the function:
function [fitresult, gof, h] = create_fit(xx, yy, zz, color)
[xData, yData, zData] = prepareSurfaceData( xx, yy, zz );
ft = fittype( 'poly11' );
opts = fitoptions( ft );
opts.Lower = [-Inf -Inf -Inf];
opts.Upper = [Inf Inf Inf];
hold on;
[fitresult, gof] = fit( [xData, yData], zData, ft, opts );
h = plot( fitresult, [xData, yData], zData);
set(h(1), 'FaceColor', color);
set(h(2), 'MarkerFaceColor', color, 'MarkerEdgeColor', 'k');
and here is a sample script that calls the function:
% Define sample data
N = 20;
x = rand(1,N);
y = rand(1,N);
z = rand(1,N);
% Call the function, specify color
[f1, gof1, h1] = create_fit(x, y, z, 'r');
[f2, gof2, h2] = create_fit(x, y.^10, z, 'g');
[f3, gof3, h3] = create_fit(x.^10, y, z, 'b');
% Figure adjustments
xlabel( 'xx' );
ylabel( 'yy' );
zlabel( 'zz' );
view(3)
grid on
xlim([min(x) max(x)]);
ylim([min(y) max(y)]);
zlim([min(z) max(z)]);
and the result:

Matlab curve fitting tool, cftool, generate code function does not give the same fit

I am using Matlab's curve fitting tool, cftool, to fit a set of points which I have. The problem I am facing is that the generate code function will not give me the same fit as produced in the cftool.
This is not what I want because I want to be able to retrieve the data from the residual plot. I could also just copy the function from cftool and do it manually. But I do not understand why the generated code will not just give me the same curve fit.
The cftool session file: http://dl.dropbox.com/u/20782274/test.sfit
The generated code from Matlab:
function [fitresult, gof] = createFit1(Velocity, kWhPerkm)
%CREATEFIT1(VELOCITY,KWHPERKM)
% Create a fit.
%
% Data for 'untitled fit 3' fit:
% X Input : Velocity
% Y Output: kWhPerkm
% Output:
% fitresult : a fit object representing the fit.
% gof : structure with goodness-of fit info.
%
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 02-Dec-2012 16:36:19
%% Fit: 'untitled fit 3'.
[xData, yData] = prepareCurveData( Velocity, kWhPerkm );
% Set up fittype and options.
ft = fittype( 'a/(0.008*x) + c*x^2 + d*90', 'independent', 'x', 'dependent', 'y' );
opts = fitoptions( ft );
opts.DiffMaxChange = 0.01;
opts.Display = 'Off';
opts.Lower = [-Inf -Inf -Inf];
opts.MaxFunEvals = 1000;
opts.MaxIter = 1000;
opts.StartPoint = [0 0 0];
opts.Upper = [Inf Inf Inf];
% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );
% Create a figure for the plots.
figure( 'Name', 'untitled fit 3' );
% Plot fit with data.
subplot( 2, 1, 1 );
plot( fitresult, xData, yData, 'predobs' );
% Label axes
xlabel( 'Velocity' );
ylabel( 'kWhPerkm' );
grid on
% Plot residuals.
subplot( 2, 1, 2 );
plot( fitresult, xData, yData, 'residuals' );
% Label axes
xlabel( 'Velocity' );
ylabel( 'kWhPerkm' );
grid on
The curve I get with the generated code:
http://i.stack.imgur.com/65d1P.jpg
The curve I need:
http://i.stack.imgur.com/p3Egp.jpg
So does anyone know what goes wrong?
-edit-
And the Velocity and WhPerkm data file: http://dl.dropbox.com/u/20782274/data.mat
RE: I want to be able to retrieve the data from the residual plot
One way to do this is:
Select "Save to Workspace..." from the Fit menu
Ensure that "Save fit output to MATLAB struct named" is checked.
Note the name of variable. By default, it is output.
Click "OK" to send data to the MATLAB workspace.
In the MATLAB workspace, the residuals will be in output.residuals. For your example, you can plot the residuals via, e.g.,
>> plot( Velocity, output.residuals, '.' )